How do I access elements from the outer loop from within nested loops?

前端 未结 3 793
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 09:28

I have nested xsl:for loops:


    
        
    

        
相关标签:
3条回答
  • 2020-12-09 09:49

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

     <ns:attribute>
          <ns:name>name</ns:name>
          <!--1 or more repetitions:-->
          <ns:value>Rahul</ns:value>
          <ns:value>Sushovan</ns:value>
    </ns:attribute>
    
    0 讨论(0)
  • 2020-12-09 10:04

    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.

    <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>
    
    0 讨论(0)
  • 2020-12-09 10:08

    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:

    <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

    0 讨论(0)
提交回复
热议问题