I have nested xsl:for loops:
我嵌套了xsl:for循环:
<xsl:for-each select="/Root/A">
<xsl:for-each select="/Root/B">
<!-- Code -->
</xsl:for>
</xsl:for>
From within the inner loop, how can I access attributes from the current node in the outer loop?
从内部循环中,如何从外部循环中的当前节点访问属性?
I keep finding myself writing code like this:
我一直在发现自己编写这样的代码:
<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>
This doesn't scale very well, as sometimes I need to access several pieces of information and end up creating one variable for each piece. Is there an easier way?
这不能很好地扩展,因为有时我需要访问几条信息并最终为每个部分创建一个变量。有没有更简单的方法?
3 个解决方案
#1
You can store the entire /Root/A structure in a variable, and make reference to that variable rather than creating a new variable for every attribute and subelement you need to access.
您可以将整个/ 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>
#2
Welbog has answered it well - but just to note you appear to be doing a cartesion (cross) join - is that intentional? If you are trying to do a regular join (with a predicate etc), then you want want to look into <xsl:key/>
- i.e. declare a key:
Welbog已经很好地回答了 - 但是只是要注意到你似乎正在进行一场推特(交叉)加入 - 这是故意的吗?如果您尝试进行常规连接(使用谓词等),那么您希望查看
<xsl:key name="BIndex" match="/Root/B" use="SomeChildNode"/>
then consume in your predicate:
然后消耗你的谓词:
<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>
This should be equivalent to (but much faster than) the predicate:
这应该等同于(但比谓词快得多):
<xsl:for-each select="/Root/B[SomeChildNode = current()/LocalNode]">
If you are grouping the data, then look at Muenchian grouping
如果要对数据进行分组,请查看Muenchian分组
#3
The following could also be used :
以下也可以使用:
<xsl:for-each select="ns:attribute">
<name><xsl:value-of select="ns:name" /></name>
<xsl:for-each select="ns:value">
<value><xsl:value-of select="."/></value>
</xsl:for-each>
</xsl:for-each>
For parsing the XML document ..
用于解析XML文档..
<ns:attribute>
<ns:name>name</ns:name>
<!--1 or more repetitions:-->
<ns:value>Rahul</ns:value>
<ns:value>Sushovan</ns:value>
</ns:attribute>
#1
You can store the entire /Root/A structure in a variable, and make reference to that variable rather than creating a new variable for every attribute and subelement you need to access.
您可以将整个/ 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>
#2
Welbog has answered it well - but just to note you appear to be doing a cartesion (cross) join - is that intentional? If you are trying to do a regular join (with a predicate etc), then you want want to look into <xsl:key/>
- i.e. declare a key:
Welbog已经很好地回答了 - 但是只是要注意到你似乎正在进行一场推特(交叉)加入 - 这是故意的吗?如果您尝试进行常规连接(使用谓词等),那么您希望查看
<xsl:key name="BIndex" match="/Root/B" use="SomeChildNode"/>
then consume in your predicate:
然后消耗你的谓词:
<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>
This should be equivalent to (but much faster than) the predicate:
这应该等同于(但比谓词快得多):
<xsl:for-each select="/Root/B[SomeChildNode = current()/LocalNode]">
If you are grouping the data, then look at Muenchian grouping
如果要对数据进行分组,请查看Muenchian分组
#3
The following could also be used :
以下也可以使用:
<xsl:for-each select="ns:attribute">
<name><xsl:value-of select="ns:name" /></name>
<xsl:for-each select="ns:value">
<value><xsl:value-of select="."/></value>
</xsl:for-each>
</xsl:for-each>
For parsing the XML document ..
用于解析XML文档..
<ns:attribute>
<ns:name>name</ns:name>
<!--1 or more repetitions:-->
<ns:value>Rahul</ns:value>
<ns:value>Sushovan</ns:value>
</ns:attribute>