XSLT 2.0 中的映射在从参数传递键值时不起作用?



我在从地图获取数据时遇到问题:键:

<?xml version="1.0" encoding="UTF-8"?>
<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:output indent="yes"/>
<xsl:variable name="apos">'</xsl:variable>
<xsl:template match="/">
<root>
<xsl:variable name="mnth" select="3"/>
<xsl:variable name="months" select="map{ '1': 'January', '2': 'February', '3': 'March'}"/>
<xsl:variable name="pos" select="concat($apos, string($mnth), $apos)"/>
<pos>
<xsl:value-of select="$pos"/>
</pos>
<correctval>
<xsl:value-of select="$months('3')"/>
</correctval>
<valuenotcoming>
<xsl:value-of select="$months($pos)"/>
</valuenotcoming>
</root>
</xsl:template>

</xsl:stylesheet>

在下面的代码中,$pos返回'3'并符合等于$months('3')$months($pos),但$months($pos)没有返回其相应的值:

<xsl:variable name="pos" select="concat($apos, string($mnth), $apos)"/>
<pos><xsl:value-of select="$pos"/></pos>
<correctval><xsl:value-of select="$months('3')"/></correctval>
<valuenotcoming><xsl:value-of select="$months($pos)"/></valuenotcoming>

所需输出:

<root>
<pos>'3'</pos>
<correctval>March</correctval>
<valuenotcoming>March</valuenotcoming>
</root>

电流输出:

<root>
<pos>'3'</pos>
<correctval>March</correctval>
<valuenotcoming/>
</root>

$pos包含长度为 3 个字符的字符串;字面意思是'3'

当您执行$months('3')撇号时,撇号将指示使用字符串文本(而不是数字(。字符串本身只是一个字符3

您需要将$pos的声明更改为 this,以便将其设置为仅包含单个字符的字符串文本3

<xsl:variable name="pos" select="string($mnth)"/>

另请注意,map函数仅在 XSLT 3.0 中可用。如果您使用的是 XSLT 3.0 处理器(如 Saxon 9.8 HE(,即使您将version="2.0"放入电子表格中,它仍将处理映射函数。

最新更新