Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT - Looping through all child nodes

Tags:

xml

xslt-1.0

Don't shoot I'm just the messenger here, but I have some xml that looks like this

<XMLSnippet>
    <data>
        <stuff value="stuff" />
        <stuff value="more stuff" />
        <stuff value="even more stuff" />
        <widget value="you expected stuff didn't you" />
        <stuff value="great, we've got stuff again" />
    </data>
</XMLSnippet>

And I would like to loop through all the data child nodes and output the following

stuff
more stuff
even more stuff
you expected stuff didn't you
great, we've got stuff again

Should it matter I am limited to using XSLT 1.0

Thanks!

like image 478
dscl Avatar asked Sep 09 '13 21:09

dscl


People also ask

How to loop through XML nodes in XSLT?

You can format your XSLT stylesheet to go to a specific node, and then loop through the given node set. You create an XSLT loop with the <xsl:for-each> tag. The value of the select attribute in this tag is an XPath expression that allows you to specify the data element to loop through.

How to iterate array in XSLT?

Anyway, for a start, I would suggest to use <xsl:variable name="listData" select="met:getData($Id)"/> , that is all you can do on the XSLT side to bind the variable to the result from the function call, your current attempt makes the variable hold a tree fragment containing a text node with the string value of the ...


2 Answers

Thanks to Phil and the suggestions of Alexandre here is the code I got working

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/XMLSnippet">
       <xsl:for-each select="data/*">
          <xsl:value-of select="@value" />
       </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
like image 168
dscl Avatar answered Oct 17 '22 14:10

dscl


This is a basic XSLT question, so I am assuming you have little experience with xsl by your post. You need to understand how xslt processes a XML document which is beyond the scope of this post. Nevertheless, this should get you started. Please note, there are several ways to get the output you want, this is only one of them:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="XMLSnippet">
    <xsl:for-each select="data/stuff">
        <xsl:value-of select="@value"/>
    </xsl:for-each>
</xsl:template>

For starters, the template match="/" is your entry point. The apply-templates is an xslt instruction that tells the xslt processor to apply the template of the node in context. In this case your root node "XMLSnippet".

The for-each select="data/stuff" should be self explanatory as well as the value-of select="@value", except the @ is used to select an attribute.

Good Luck. May I suggest you read this book XSLT 2.0. A great book on XSLT.

like image 26
PhillyNJ Avatar answered Oct 17 '22 15:10

PhillyNJ