pau*_*doo 20 xslt scope for-loop
我嵌套了xsl:for循环:
<xsl:for-each select="/Root/A">
<xsl:for-each select="/Root/B">
<!-- Code -->
</xsl:for>
</xsl:for>
Run Code Online (Sandbox Code Playgroud)
在内部循环中,如何从外部循环中的当前节点访问属性?
我一直在发现自己写这样的代码:
<xsl:for-each select="/Root/A">
<xsl:variable name="someattribute" select="@SomeAttribute"/>
<xsl:for-each select="/Root/B">
<!-- Now can use $someattribute to access data from 'A' -->
</xsl:for>
</xsl:for>
Run Code Online (Sandbox Code Playgroud)
这不能很好地扩展,因为有时我需要访问几条信息并最终为每个部分创建一个变量.有没有更简单的方法?
Wel*_*bog 26
您可以将整个/ Root/A结构存储在变量中,并引用该变量,而不是为您需要访问的每个属性和子元素创建新变量.
<xsl:for-each select="/Root/A/">
<xsl:variable name="ROOT_A" select="."/>
<xsl:for-each select="/Root/B/">
<!-- Variable is accessed like this: $ROOT_A/@someAttribute
Just like a normal XML node -->
</xsl:for-each>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
Welbog已经很好地回答了 - 但是只是要注意到你似乎正在进行卡车(交叉)加入 - 这是故意的吗?如果你试图进行常规连接(使用谓词等),那么你想要查看<xsl:key/>- 即声明一个键:
<xsl:key name="BIndex" match="/Root/B" use="SomeChildNode"/>
Run Code Online (Sandbox Code Playgroud)
然后消耗你的谓词:
<xsl:for-each select="/Root/A/">
<xsl:variable name="ROOT_A" select="."/>
<xsl:for-each select="key('BIndex', LocalNode)">
<!-- -->
</xsl:for-each>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
这应该等同于(但比谓词快得多):
<xsl:for-each select="/Root/B[SomeChildNode = current()/LocalNode]">
Run Code Online (Sandbox Code Playgroud)
如果要对数据进行分组,请查看Muenchian分组