Reputation: 50
I have a xml structures like below,
<NameList>
<Name>name01</Name>
<Name>name02</Name>
<Name>name03</Name>
<Name>name04</Name>
</NameList>
how to iterate over the child tags of NameList and display them using XSLT's xsl:for-each? My ouput should be like
name01
name02
name03
name04
Thanks
Upvotes: 0
Views: 64
Reputation: 70618
There is no real need to use xsl:for-each. You can do this with template matching which is usually the favoured approach in XSLT.
You would need one template to match your NameList element, where you can output any 'containing' elements you want, and then start selecting the child elements
<xsl:template match="NameList">
<table>
<xsl:apply-templates select="Name" />
</table>
</xsl:template>
Then you have a template actually matching the Name element, where you output it in whatever format you want. For example
<xsl:template match="Name">
<tr>
<td>
<xsl:value-of select="." />
</td>
</tr>
</xsl:template>
Try this XSLT for starters:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="NameList">
<table>
<xsl:apply-templates select="Name" />
</table>
</xsl:template>
<xsl:template match="Name">
<tr>
<td>
<xsl:value-of select="." />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
If you did want any more help with formatting or outputting elements, you really need to have mentioned this in your question. Thanks!
Upvotes: 1
Reputation: 49
I am not completly sure what you want, but maybe like this?
<xsl:template match="/">
<xsl:for-each select="NameList">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
It puts out
name01 name02 name03 name04
Upvotes: 1