I wonder if there´s a way to search an xml for duplicates, then when found to aggregate all the duplicates to one node. F.eks
<car name="one">
<person>john</person>
<person>Jay</person>
</car>
<car name="two">
<person>kim</person>
<person>chris</person>
</car>
<car name="one">
<person>jane</person>
<person>liz</person>
</car>
Should be:
<car name="one">
<person>john</person>
<person>Jay</person>
<person>jane</person>
<person>liz</person>
</car>
<car name="two">
<person>kim</person>
<person>chris</person>
</car>
All help is much appreciated!
Br Kim
In XSLT 1.0 you have to use something called the Muenchian Method which is a process of grouping elements using the
key
andgenerate-id
functions.The process goes something like this:
You first define a key that represents the data that you want to group with.
Then you you use that key in a template match by generating an id based on that key.
Now that you have your nodes grouped, all you do is use that key again to grab all of the nodes within that key.
So now to show it all together, when you take this XML (added
document
to make it well-formed).And apply this XSLT
It produces this result.
If you are using XSLT 2.0 you can make use of
xsl:for-each-group
as follows:When applied on the following input:
It produces the correct ouput: