我经常在xsl中使用棘手的哈希映射,但这次…我不知道该如何解决这个问题…
相关XML摘录:
<DOCUMENT>
<PANO_LIV_MIN>True</PANO_LIV_MIN>
<PANO_LIV_INTER>True</PANO_LIV_INTER>
<!-- PANO_LIV_MATIN voluntarily ommited -->
</DOCUMENT>
XSL的相关摘录:
[...]
<xsl:variable name="hashmap">
<entry key="PANO_LIV_MATIN">Matin</entry>
<entry key="PANO_LIV_MIN">Ministre</entry>
<entry key="PANO_LIV_INTER">International</entry>
</xsl:variable>
<xsl:for-each select="PANO_LIV_MATIN[text() = 'True'] | PANO_LIV_MIN[text() = 'True'] | PANO_LIV_INTER[text() = 'True']">
<span class="panorama">
<xsl:value-of select="exsl:node-set($hashmap)/entry[@key=name()]" />*
#<xsl:value-of select="name()"/>#
</span>
</xsl:for-each>
[...]
结果:
<span class="panorama">* #PANO_LIV_MATIN#</span> - <span style="" class="panorama">* #PANO_LIV_MIN#</span>
预期结果:
<span class="panorama">Matin * #PANO_LIV_MATIN#</span> - <span style="" class="panorama">Ministre * #PANO_LIV_MIN#</span>
什么有效:
<xsl:value-of select="exsl:node-set($hashmap)/entry[@key='PANO_LIV_MATIN']" />
<!-- gives me Matin as expected -->
什么不工作,但我不知道为什么:
<xsl:value-of select="exsl:node-set($hashmap)/entry[@key=name()]" />
<!-- gives me nothing, but the name() print exactly the good key if I put it in a xsl:value-of -->
name()
方法有什么问题?
在您的示例中name()
的问题是,它是在错误的上下文节点,因为它在您的$hashmap
上的选择中使用,您必须使用变量代替:
<xsl:for-each select="PANO_LIV_MATIN[text() = 'True'] | PANO_LIV_MIN[text() = 'True'] | PANO_LIV_INTER[text() = 'True']">
<xsl:variable name="name" select="name()"/>
<span class="panorama">
<xsl:value-of select="exsl:node-set($hashmap)/entry[@key=$name]" />*
#<xsl:value-of select="name()"/>#
</span>
你需要<xsl:value-of select="exsl:node-set($hashmap)/entry[@key = name(current())]" />