XSLT 中可变索引处的数组
Array At a Variable Index in XSLT
我正在尝试根据 XML 文件中当前日期的值访问数组中的特定元素。
例如,在 XML
<CurrentMonth>5</CurrentMonth>
然后,在 XSLT 中 - 这被设置为变量
<xsl:variable name="current-month">
xsl:value-of select="//CurrentMonth" />
</xsl:variable>
我也已将 "Month names" 的数组声明为
<xsl:variable name="array" as="element()*">
<Item>Jan</Item>
<Item>Feb</Item>
<Item>Mar</Item>
<Item>Apr</Item>
<Item>May</Item>
<Item>Jun</Item>
<Item>Jul</Item>
<Item>Aug</Item>
<Item>Sept</Item>
<Item>Oct</Item>
<Item>Nov</Item>
<Item>Dec</Item>
</xsl:variable>
在 XSLT 中是否可以通过使用变量作为数组的索引来 return 月份名称(例如 "Jan")?
示例:
<xsl:value-of select="$array[$current-month]">
上面的代码让我很困惑
[FATAL]: Error checking type of the expression 'filter-expr(variable-ref(array/result-tree)
提前致谢。
定义变量为<xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/>
,然后就可以使用$array[$current-month]
(虽然你索引的是序列而不是数组)。使用您的代码,您需要 $array[position() = $current-month]
.
一个最小但完整的样式表,运行适合我使用 Saxon 9.6.0.7 HE
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:variable name="array" as="element()*">
<Item>Jan</Item>
<Item>Feb</Item>
<Item>Mar</Item>
<Item>Apr</Item>
<Item>May</Item>
<Item>Jun</Item>
<Item>Jul</Item>
<Item>Aug</Item>
<Item>Sept</Item>
<Item>Oct</Item>
<Item>Nov</Item>
<Item>Dec</Item>
</xsl:variable>
<xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/>
<xsl:template match="/">
<xsl:value-of select="$array[$current-month]"/>
</xsl:template>
</xsl:stylesheet>
并在 运行 对输入 <CurrentMonth>5</CurrentMonth>
时输出 May
。
您有几个语法错误:
<xsl:variable name="current-month">
xsl:value-of select="//CurrentMonth" />
</xsl:variable>
需要:
<xsl:variable name="current-month">
<xsl:value-of select="//CurrentMonth" />
</xsl:variable>
或者最好是:
<xsl:variable name="current-month" select="//CurrentMonth" />
接下来你有:
<xsl:value-of select="$array[$current-month]">
需要关闭的:
<xsl:value-of select="$array[$current-month]"/>
并且,如果您使用第一种形式定义变量,则需要:
<xsl:value-of select="$array[number($current-month)]">
我正在尝试根据 XML 文件中当前日期的值访问数组中的特定元素。
例如,在 XML
<CurrentMonth>5</CurrentMonth>
然后,在 XSLT 中 - 这被设置为变量
<xsl:variable name="current-month">
xsl:value-of select="//CurrentMonth" />
</xsl:variable>
我也已将 "Month names" 的数组声明为
<xsl:variable name="array" as="element()*">
<Item>Jan</Item>
<Item>Feb</Item>
<Item>Mar</Item>
<Item>Apr</Item>
<Item>May</Item>
<Item>Jun</Item>
<Item>Jul</Item>
<Item>Aug</Item>
<Item>Sept</Item>
<Item>Oct</Item>
<Item>Nov</Item>
<Item>Dec</Item>
</xsl:variable>
在 XSLT 中是否可以通过使用变量作为数组的索引来 return 月份名称(例如 "Jan")?
示例:
<xsl:value-of select="$array[$current-month]">
上面的代码让我很困惑
[FATAL]: Error checking type of the expression 'filter-expr(variable-ref(array/result-tree)
提前致谢。
定义变量为<xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/>
,然后就可以使用$array[$current-month]
(虽然你索引的是序列而不是数组)。使用您的代码,您需要 $array[position() = $current-month]
.
一个最小但完整的样式表,运行适合我使用 Saxon 9.6.0.7 HE
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:variable name="array" as="element()*">
<Item>Jan</Item>
<Item>Feb</Item>
<Item>Mar</Item>
<Item>Apr</Item>
<Item>May</Item>
<Item>Jun</Item>
<Item>Jul</Item>
<Item>Aug</Item>
<Item>Sept</Item>
<Item>Oct</Item>
<Item>Nov</Item>
<Item>Dec</Item>
</xsl:variable>
<xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/>
<xsl:template match="/">
<xsl:value-of select="$array[$current-month]"/>
</xsl:template>
</xsl:stylesheet>
并在 运行 对输入 <CurrentMonth>5</CurrentMonth>
时输出 May
。
您有几个语法错误:
<xsl:variable name="current-month">
xsl:value-of select="//CurrentMonth" />
</xsl:variable>
需要:
<xsl:variable name="current-month">
<xsl:value-of select="//CurrentMonth" />
</xsl:variable>
或者最好是:
<xsl:variable name="current-month" select="//CurrentMonth" />
接下来你有:
<xsl:value-of select="$array[$current-month]">
需要关闭的:
<xsl:value-of select="$array[$current-month]"/>
并且,如果您使用第一种形式定义变量,则需要:
<xsl:value-of select="$array[number($current-month)]">