Reputation: 5670
I got an XML like this on va variable prdxml
<category numberofproducts="0">
<id>catering</id>
<url>/products/kantine</url>
<name>Kantine</name>
<category numberofproducts="0">
<id>madembal</id>
<url>/products/mad-og-emballage</url>
<name>Mad og emballage</name>
<category numberofproducts="9">
<id>stanniol</id>
<url>/products/stanniol</url>
<name>Stanniol</name>
<category numberofproducts="9">
<id>stankd</id>
<url>/products/stankd</url>
<name>stankd</name>
</category>
</category>
<category numberofproducts="5">
<id>termo</id>
<url>/products/termobakker</url>
<name>Termobakker</name>
</category>
</category>
</category>
Now I want to get all the Id's present in this xml to a string.Tried usual for-each loop but failed,because of the structure of this xml. Now I thinks there may be a smarter way to achieve this ,that i never known.Can any one give me that light.
Upvotes: 1
Views: 204
Reputation: 107357
If you mean flattening all id
's irrespective of their nesting, level, then this should work with a ',' delimiter in between.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//id"/>
</xsl:template>
<xsl:template match="id">
<xsl:value-of select="concat(./text(), ',')"/>
</xsl:template>
</xsl:stylesheet>
Returns : catering,madembal,stanniol,stankd,termo,
If you need a more elegant mechanism for handling the last element delimiter, have a look at Dimitre's answer here
Update: To put the result into a variable:
<xsl:variable name="someVar">
<xsl:apply-templates select="//id"/>
</xsl:variable>
Result: <xsl:value-of select="$someVar"/>
Upvotes: 1