How to convert the text to date, and then get the value of the year?

You must convert the text value "2012-03-19" to a date type, then extract the component of the year.

<xsl:variable name="dateValue" select="2012-03-19"/> <xsl:variable name="year" select="year-from-date(date($dateValue))"/> 

I am using Saxon 2.0, but the date complaint does not exist; I looked at the Saxon documentation and could not find this function, so the problem is clear, but I cannot find a suitable replacement.

+7
source share
1 answer

I don't think date() should be a function, you need the xs:date() data type.

Add the xs namespace and then the xs:date() prefix.

In the following stylesheet, 2012 will be created using any valid XML input:

 <xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:variable name="dateValue" select="'2012-03-19'"/> <xsl:variable name="year" select="year-from-date(xs:date($dateValue))"/> <xsl:value-of select="$year"/> </xsl:template> </xsl:stylesheet> 

Note that you must also quote your select in your "dateValue" xsl:variable .

+8
source

All Articles