raf*_*ian 7 xslt date saxon xslt-2.0
需要将文本值"2012-03-19"转换为日期类型,然后提取年份组件.
<xsl:variable name="dateValue" select="2012-03-19"/>
<xsl:variable name="year" select="year-from-date(date($dateValue))"/>
Run Code Online (Sandbox Code Playgroud)
我正在使用Saxon 2.0,但它的抱怨date功能不存在; 我查看了Saxon的文档并找不到该功能,所以这显然是问题,但我找不到合适的替代品.
我不认为date()应该是一个函数,你想要的xs:date()数据类型.
添加xs命名空间然后添加前缀xs:date().
以下样式表使用任何格式良好的XML输入,将生成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>
Run Code Online (Sandbox Code Playgroud)
请注意,您还必须select在"dateValue"中引用您的内容xsl:variable.