2

Below an example of my current XML:

<products>
    <product>
        <id>1</id>
        <name>Name 1</name>
        <price>10.00</price>
        <size>M</size>
        <link>http://link1.com</link>
        <stock>1</stock>
    </product>
    <product>
        <id>1</id>
        <name>Name 1</name>
        <price>10.00</price>
        <size>L</size>
        <link>http://link1.com</link>
        <stock>3</stock>
    </product>
    <product>
        <id>1</id>
        <name>Name 1</name>
        <price>10.00</price>
        <size>XL</size>
        <link>http://link1.com</link>
        <stock>2</stock>
    </product>
    <product>
        <id>2</id>
        <name>Name 2</name>
        <price>30.00</price>
        <size>M</size>
        <link>http://link2.com</link>
        <stock>3</stock>
    </product>
    <product>
        <id>2</id>
        <name>Name 2</name>
        <price>30.00</price>
        <size>L</size>
        <link>http://link2.com</link>
        <stock>2</stock>
    </product>
</products>

I want to combine each product based on the id and remove the duplicates with XSLT.

If the id is the same, the nodes name, price and link are always the same.

The nodes sizes and stock can be different if the id is the same. For these nodes (size and stock) these values should be summed up into the node (comma separated).

The result should look like this:

<products>
    <product>
        <id>1</id>
        <name>Name 1</name>
        <price>10.00</price>
        <size>M,L,XL</size>
        <link>http://link1.com</link>
        <stock>1,3,2</stock>
    </product>
    <product>
        <id>2</id>
        <name>Name 2</name>
        <price>30.00</price>
        <size>M,L</size>
        <link>http://link2.com</link>
        <stock>3,2</stock>
    </product>
</products>

I've searched and tried out several solutions but can't figure this one out.

Is there anyone who can help me out?

Thanks!

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • 6
    Please add the XSLT version tag to your question, since it's relevant in grouping. Search for *grouping in XSLT 2.0* and you will find many examples. Search for *Muenchian grouping* if you are restricted to XSLT 1.0. – helderdarocha May 15 '14 at 14:25
  • 1
    Similar questions/answers. **XSLT 1.0**: [Grouping parent node based on child elements...](http://stackoverflow.com/questions/4670775/grouping-parent-node-based-on-child-elements-values-and-sequence-number), [Grouping XML nodes by attribute value...](http://stackoverflow.com/questions/7243965/grouping-xml-nodes-by-attribute-value-in-xslt), [XLST transformation to XML grouping by key](http://stackoverflow.com/questions/4443948/xslt-tranformation-to-xml-grouping-by-key), **XSLT 2.0**: [How to use for-each-group...](http://stackoverflow.com/questions/19115109/how-to-use-for-each-group-in-xsl) – helderdarocha May 16 '14 at 02:00
  • Thanks for the replies. In the end I found this solution which helped me out perfectly: http://stackoverflow.com/questions/9768402/xslt-concat-fields-together – user3640968 May 19 '14 at 12:17

0 Answers0