XSLT - 使用嵌套for-each循环的内部循环访问外部元素

时间:2014-12-05 20:18:03

标签: xml xslt

我是XSLT的新手,并且有一个问题需要我从内部循环中嵌套for-each的外部循环中的元素访问值。我的XML如下所示

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

我的XSLT文档的摘录如下所示

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

如何从内部for-each循环访问外部循环中货币票价节点的值。

3 个答案:

答案 0 :(得分:7)

您可以相对访问父级:

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

或者使用变量捕获外部循环当前节点,然后在其中访问它:

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

答案 1 :(得分:3)

使用父轴,也就是..运算符。

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

答案 2 :(得分:1)

使用<xsl:value-of select="../currency"/>向上导航。