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