问题描述
我发现了一个 xsl:key 似乎不起作用的情况.
我正在将 XSLT 1 与 Xalan(已编译)一起使用,这就是正在发生的事情:
I have found a case where xsl:key seems not to be working.
I am using XSLT 1 with Xalan (compiled) and this is what is happening:
1.- 这有效:名为 test1 的键工作正常:
1.- This works: key named test1 works fine:
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<!-- now the key: when defined and used here it works fine: -->
<xsl:key name="test1" match="//promo" use="'anytext'" />
<xsl:for-each select="key('test1','anytext')/*">
loop through elements in key... ok, works fine
</xsl:for-each>
<xsl:for-each select="$promos/*">
..loop through elements in variable $promos ...it is not empty so the loop iterates several times
</xsl:for-each>
2.- 这不起作用:键 test1 现在已定义并使用(我猜这是重点)在一个循环中遍历使用 xalan:nodeset
2.- This doesn't work: key test1 is now defined and used (which is the important point, I guess) inside a loop that iterates through elements obtained with xalan:nodeset
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<xsl:for-each select="$promos/*">
<!-- now the key: when defined and used (or just used) inside this for-each loop it does nothing: -->
<xsl:key name="test1" match="//promo" use="'anytext'" />
<xsl:for-each select="key('test1','anytext')/*">
loop through elements in key... NOTHING HERE, it does not work :(
</xsl:for-each>
..loop through elements in variable $promos ...it is not empty so iterates several times
</xsl:for-each>
有人知道发生了什么吗?请注意,变量 $promos 不是空的,所以循环确实在迭代,它是其中使用的键,它什么也不做.
Does anybody know what is happening? Notice that variable $promos is not empty, so the loop really iterates, it is the key used inside it which does nothing.
非常感谢您.
PS:在 Martin 的回答之后,我发布了这个替代代码,它也不起作用:
PS: after Martin's answer I post this alternative code, which doesn't work either:
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<!-- now the key: defined as a first level element: -->
<xsl:key name="test1" match="//promo" use="'anytext'" />
<xsl:for-each select="$promos/*">
<!-- but used inside this for-each loop it does nothing: -->
<xsl:for-each select="key('test1','anytext')/*">
loop through elements in key... NOTHING HERE, it does not work :(
</xsl:for-each>
..loop through elements in variable $promos ...it is not empty so iterates several times
</xsl:for-each>
解决方案: Martin 的评论是问题的关键:结果树片段中的节点被视为不同文档中的节点.
Martin 也指出了解决方法:在 XSLT 1.0 中,您需要使用 for-each 更改上下文节点,然后在 for-each 中调用 key. 此代码将起作用:
Solution: In Martin's comments was the key to the problem: nodes in a result tree fragment are treated as nodes in a different document.
Martin pointed the workaround too: In XSLT 1.0 you need to change the context node using a for-each and then call key inside the for-each. This code will work then:
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<!-- now the key -->
<xsl:key name="test1" match="//promo" use="'anytext'" />
<xsl:for-each select="$promos/*">
<!-- inside this for-each we are in a different *document*, so we must go back: -->
<xsl:for-each select="/">
<xsl:for-each select="key('test1','anytext')/*">
loop through elements in key... and now IT WORKS, thanks Martin :)
</xsl:for-each>
</xsl:for-each>
..loop through elements in variable $promos ...it is not empty so iterates several times
</xsl:for-each>
推荐答案
我很惊讶将 xsl:key
放入 for-each.在 http://www.w3.org/TR/xslt#key 你可以看到
xsl:key
被定义为 顶级元素,所以你需要把它作为子元素
xsl:stylesheet
或 xsl:transform
.
I am surprised that you don't get an error by putting the
xsl:key
into the for-each
. In http://www.w3.org/TR/xslt#key you can see that xsl:key
is defined as a <!-- Category: top-level-element -->
top level element so you need to put it as a child of xsl:stylesheet
or xsl:transform
.
这篇关于循环遍历使用 xalan:nodeset 获得的节点集时 xsl:key 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!