如何将文本转换为日期并获取年份?

7

需要将文本值`2012-03-19'转换为日期类型,然后提取年份组件。

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

我正在使用Saxon 2.0版本,但它报错说date函数不存在;我查看了Saxon的文档,但没有找到该函数,因此这显然是问题所在,但我找不到合适的替代方案。

1个回答

8

我认为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>

请注意,在“dateValue”xsl:variable中,您还需要引用select

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接