如何从日期变量中提取年份并在XSL中减少它

时间:2015-06-12 06:16:00

标签: xml xslt

从以下XML中,我必须提取1989年并将其减去2并显示年份。

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>10/10/1989</year>
    </cd>
</catalog>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
     <xsl:variable name="num1" select="catalog/cd/year" />
     <xsl:variable name="num2" select="2" />
    --1989-2 value should be displayed here-----
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
      <body>
        <h2>My CD Collection</h2>
         <xsl:variable name="num1" select="substring(catalog/cd/year,string-length(catalog/cd/year) - 3)" /> 
I want last 4 characters, so I did a minus 3. If its last n characters, do minus n-1 
         <xsl:variable name="num2" select="2" />
        <xsl:value-of select="$num1 - $num2" />
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>