I need to transform this XML input:
<root>
<node id="a">
<section id="a_1" method="run">
<item id="0" method="a">
<attribute>
<color>Red</color>
<status>1</status>
<condition>good</condition>
</attribute>
</item>
<item id="0" method="a">
<attribute>
<color>Red</color>
<status>1</status>
<condition>good</condition>
</attribute>
</item>
</section>
<section id="a_2" method="run">
<item id="0" method="a">
<attribute>
<color>Red</color>
<status>1</status>
<condition>good</condition>
</attribute>
</item>
</section>
</node>
<node id="b">
<section id="b_1" method="create">
<user id="b_1a" method="x">
<attribute>
<origin>us</origin>
</attribute>
</user>
<user id="b_1a" method="x">
<attribute>
<origin>us</origin>
</attribute>
</user>
<user id="b_1b">
<attribute>a</attribute>
</user>
</section>
<section id="b_2">
<user id="b_1a" method="x">
<attribute>
<name>John</name>
<origin>us</origin>
</attribute>
</user>
</section>
</node>
</root>
Here is the expected output:
<root>
<node id="a">
<section id="a_1" method="run">
<item id="0" method="a">
<attribute>
<color>Red</color>
<status>1</status>
<condition>good</condition>
</attribute>
</item>
</section>
<section id="a_2" method="run">
<item id="0" method="a">
<attribute>
<color>Red</color>
<status>1</status>
<condition>good</condition>
</attribute>
</item>
</section>
</node>
<node id="b">
<section id="b_1" method="create">
<user id="b_1a" method="x">
<attribute>
<origin>us</origin>
</attribute>
</user>
<user id="b_1b">
<attribute>a</attribute>
</user>
</section>
<section id="b_2">
<user id="b_1a" method="x">
<attribute>
<name>John</name>
<origin>us</origin>
</attribute>
</user>
</section>
</node>
</root>
Note: the duplicate means all the child/children is having the same value, the node can have 1 or more children as long as it is the same parent (id and method are the same) and we can assume that it always in the same section (id and method are the same).
is this possible to be done? please enlighten me
Thanks very much.
cheers,
John
I. This XSLT 1.0 transformation:
when applied to the provided source XML document:
produces the wanted, correct result:
Explanation: Proper use of the Muenchian method for grouping, using a composite key:
The identity rule copies every node “as-is”.
The
xsl:keydefinition associates groups of elements with a string key-value. Any group so defined consists of all elements that have both anidand amethodattributes and that (all in the group) have the same parent, the same name, the same string value of theidattribute and the same string value of themethodattribute.There is a single template overriding the identity template. It matches any elements that have both an
idand amethodattributes and are not the first (in document order) element in their respective group). Because this template has no body, all such matched elements are not processed at all and aren’t copied to the output (we could say they are “deleted”).Because of 3. above, only elements that are the first element of their group aren’t matched by the overriding template. Thus these elements are matched by the identity template and copied to the output — exactly as required.
II. XSLT 2.0 Solution:
Explanation: Proper use of
xsl:for-each-groupwith thegroup-byattribute.