Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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

like image 452
jorrebor Avatar asked Jul 29 '13 08:07

jorrebor


People also ask

How do I select all child elements in xpath?

For the div element with an id attribute of hero //div[@id='hero'] , these XPath expression will select elements as follows: //div[@id='hero']/* will select all of its children elements. //div[@id='hero']/img will select all of its children img elements. //div[@id='hero']//* will select all of its descendent elements.

What is the correct syntax of for each in XSLT?

The <xsl:for-each> element selects a set of nodes and processes each of them in the same way. It is often used to iterate through a set of nodes or to change the current node. If one or more <xsl:sort> elements appear as the children of this element, sorting occurs before processing.

What is the use of for each in XSLT?

Introduction of XSLT for each. XSLT for each is defined as the iteration process that allows retrieving data from the specified nodes. Based on the selection criteria defined with these functions, it makes a loop through multiple nodes. This works in adding up with the function <xsl:value-of>.


1 Answers

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 (a.k.a the “context node”), "/*" select all the child nodes of the context node.

like image 166
They call me Trinity Avatar answered Oct 13 '22 15:10

They call me Trinity