XSLT - accessing outer element with inner loop nested for each loop

I am new to XSLT and have a problem that requires me to access values ​​from elements in the outer loop nested for each one inside the inner loop. My XML looks like this

<searchresults>
    <journeygroup>
        <journeygroupnum>1</journeygroupnum>
        <journeydetails>
            <flightsegments>1</flightsegments>
            <journeyid>1</journeyid>
            <currency>USD</currency>
            <fare>399.00</fare>
            <taxes>99.00</taxes>
            <flights>
                <segmentid>1</segmentid>
                <legid>1</legid>
                <marketingcarrier>DL</marketingcarrier>
                <operatingcarrier>DL</operatingcarrier>
                <flightnum>9695</flightnum> 
            </flights>
        </journeydetails>
        <journeydetails>
            <flightsegments>1</flightsegments>
            <journeyid>2</journeyid>
            <currency>USD</currency>
            <fare>459.00</fare>
            <taxes>129.00</taxes>
            <flights>
                <segmentid>1</segmentid>
                <legid>1</legid>
                <marketingcarrier>AA</marketingcarrier>
                <operatingcarrier>AA</operatingcarrier>
                <flightnum>5070</flightnum> 
            </flights>
        </journeydetails>
    </journeygroup>
</searchresults>

      

The extract of my XSLT document looks like this

<table>
<xsl:for-each select="searchresults/journeygroup/journeydetails">       
    <xsl:for-each select="flights[segmentid='1']">
    <tr>
        <td><xsl:value-of select="marketingcarrier"/></td>
        <td><xsl:value-of select="operatingcarrier"/></td>
        <td><xsl:value-of select="flightnum"/></td>

        <!-- Here I would like to add columns with the currency and fare from the outer loop -->
        <td>currency</td>
        <td>fare</td>
    </tr>
    </xsl:for-each>
</xsl:for-each>
<table>

      

How do I access the values ​​from the currency and rate nodes in the outer loop from the inner for-each loop.

+3


source to share


3 answers


You can access the parent object relatively:

<xsl:value-of select="../currency"/>

      



Or grab the current of the outer loop node with a variable and then access it internally:

<table>
  <xsl:for-each select="searchresults/journeygroup/journeydetails">
    <xsl:variable name="journeyDetails" select="."/>
    <xsl:for-each select="flights[segmentid='1']">
      <tr>
        <td><xsl:value-of select="marketingcarrier"/></td>
        <td><xsl:value-of select="operatingcarrier"/></td>
        <td><xsl:value-of select="flightnum"/></td>

        <!-- Here I would like to add columns with the 
             currency and fare from the outer loop -->
        <td><xsl:value-of select="$journeyDetails/currency"/></td>
        <td><xsl:value-of select="$journeyDetails/fare"/></td>
      </tr>
    </xsl:for-each>
  </xsl:for-each>
</table>

      

+7


source


Use the parent axis as well as the ..



<table>
<xsl:for-each select="searchresults/journeygroup/journeydetails">       
<xsl:for-each select="flights[segmentid='1']">
<tr>
    <td><xsl:value-of select="marketingcarrier"/></td>
    <td><xsl:value-of select="operatingcarrier"/></td>
    <td><xsl:value-of select="flightnum"/></td>

    <!-- Here I would like to add columns with the currency and fare from the outer loop -->
    <td><xsl:value-of select="../currency"/></td>
    <td><xsl:value-of select="../fare"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
<table>

      

+3


source


Navigate up using <xsl:value-of select="../currency"/>

.

+1


source







All Articles