我在xsl文档中有一个内部定义的映射表,我想使用xml中的xpath作为键来访问一个值。在XSL2中,我被认为这很容易,但我似乎遇到了错误。
<xsl:variable name="map">
<entry key="One">testOne</entry>
<entry key="Two">testTwo</entry>
<entry key="Three">testThree</entry>
</xsl:variable>
<xsl:variable name="testVariable">
<value-of select="$map/entry[@key=A/B]"/>
</xsl:variable>
如果xml文档类似于:
<A>
<B>Three</B>
<A>
xsl文档中的testVariable应解析为“testThree”&#39;
答案 0 :(得分:1)
这里有两个问题。首先它应该是 xsl:value-of 而不仅仅是值 -
其次,在xpath条件中使用A/B
的位置,这将与您搜索的条目元素相关,而不是原始XML中的A/B
。
请改为尝试:
<xsl:variable name="key" select="A/B"/>
<xsl:variable name="testVariable">
<xsl:value-of select="$map/entry[@key=$key]"/>
</xsl:variable>
或者更好的是,写下这个
<xsl:variable name="key" select="A/B"/>
<xsl:variable name="testVariable" select="$map/entry[@key=$key]"/>
实际上,您根本不必使用键变量,只需使用 current()函数来获取XML文档中的当前上下文< / p>
<xsl:variable name="testVariable" select="$map/entry[@key=current()/A/B]"/>