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
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.
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.
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>.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With