1

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.

1 Answer 1

1

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"/>
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the result correctly,But need an extra info.How can I take this values in to a variable.(all comma seperated)
Now I am getting each one seperately printed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.