循环通过xalan:nodeset获得的节点集时,xsl:key不起作用

时间:2013-08-06 09:28:33

标签: xslt xslt-1.0 xalan xslkey

我找到了一个xsl:key似乎无法正常工作的情况 我正在使用XSLT 1和Xalan(已编译),这就是正在发生的事情:

1.-这样做:名为 test1 的密钥正常工作:

<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

获得的元素
<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不为空,因此循环实际上是迭代的,它是在其中使用的密钥,它什么都不做。

非常感谢你。

PS :在Martin的回答之后我发布了这个替代代码,但这个代码也不起作用:

<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。 此代码将起作用:

<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>

1 个答案:

答案 0 :(得分:1)

我很惊讶您将xsl:key放入for-each并没有收到错误。在http://www.w3.org/TR/xslt#key中,您可以看到xsl:key被定义为<!-- Category: top-level-element -->顶级元素,因此您需要将其设为xsl:stylesheetxsl:transform的子项。< / p>