Need to convert a text value `2012-03-19' into a date type, then extract the year component.
<xsl:variable name="dateValue" select="2012-03-19"/>
<xsl:variable name="year" select="year-from-date(date($dateValue))"/>
I'm using Saxon 2.0, but it's complaining date
function does not exist; I looked around Saxon's documentation and could not find the function, so that's clearly the issue, but I can't find a suitable replacement.
To extract the year from a cell containing a date, type =YEAR(CELL) , replacing CELL with a cell reference. For instance, =YEAR(A2) will take the date value from cell A2 and extract the year from it.
1. Select a blank cell adjacent to the cell you want to display year of date only, and then type formula =YEAR(A1) into the Formula Bar, and then press the Enter key to get the result.
I don't think date()
should be a function, you want the xs:date()
data type.
Add the xs
namespace and then prefix xs:date()
.
The following stylesheet, using any well-formed XML input, will produce 2012
:
<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 also have to quote your select
in your "dateValue" xsl:variable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With