简体   繁体   中英

How to select each child node of a parent in a for-each xslt statement?

I want to select all child nodes from the adviceRow element (see xml data hereunder) in a for each loop.

I want to avoid having to write an xslt file like this:

<xsl:for-each select="Tables/routeAdviceTable/routeAdviceRows/adviceRow">
   <xsl:value-of select="cog" />
   <xsl:value-of select="current" />
   <xsl:value-of select="distance" />
   <xsl:value-of select="dtg" />
   etc..
</xsl:for-each>

Because a row can have many cells (and i have lots of similar but contentwise different tables)

Can I work with a nested for-each loop? Something like:

for-each advice row:
    for-each child of advicerow?

<Tables>
<routeAdviceTable>
    <routeAdviceRows>
        <adviceRow>
            <cog>313</cog>
            <current>0.3</current>
            <distance>58.8</distance>
            <dtg>1374786000000</dtg>
            <latitude>????</latitude>
            <longitude>????</longitude>
            <pressure>22.300001</pressure>
            <sea>0.2</sea>
            <sog>14.7</sog>
            <stw>???</stw>
            <swell>1.7</swell>
            <waves>1.711724324567694</waves>
            <wind>0.8</wind>
        </adviceRow>
    </routeAdviceRows>
</routeAdviceTable>

Thank you

Yes, you can nest for-each loops.

Try this

<xsl:for-each select="Tables/routeAdviceTable/routeAdviceRows/adviceRow">
  <xsl:for-each select="./*">
    <!-- 
      Your code i.e.
      <td class="{name(.)}">
        <xsl:value-of select="."></xsl:value-of>
      </td>
    -->
  </xsl:for-each>
</xsl:for-each>

The xpath "." refers to the current node (aka the “context node”), "/*" select all the child nodes of the context node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM