If I have this input:
<root>
<library id="L1">
<shelf1 id="1">
<book id="1" category="science">
<attributes>
<year>2000</year>
</attributes>
<attributes>
<author>xx</author>
<year>2010</year>
<isbn>001</isbn>
</attributes>
<attributes>
<author>yy</author>
<publisher>zz</publisher>
<isbn>002</isbn>
</attributes>
<other>y</other>
</book>
<book id="2" category="science">
...
</book>
</shelf1>
<shelf2>...</shelf2>
</library>
</root>
expetced output:
<root>
<library id="L1">
<shelf1 id="1">
<book id="1" category="science">
<attributes>
<author>yy</author>
<publisher>zz</publisher>
<isbn>002</isbn>
<year>2010</year>
</attributes>
<other>y</other>
</book>
<book id="2" category="science">
...
</book>
</shelf1>
<shelf2>...</shelf2>
</library>
</root>
I need to combine the element ‘attributes’ together. If two or more attributes exist,
when the child of the attributes never exist before, we keep the child as new information, but if it exists previously we simply use the latest value.
How to do such transformation in XSLT 1.0 or 2.0?Thanks very much for the help.
John
This transformation (XSLT 2.0):
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
Proper use and overriding of the identity rule.
Proper use of the XSLT 2.0 instruction
xsl:for-each-groupwith attributegroup-by.Proper use of the XSLT 2.0 function
current-grouping-key()and the XPath functionname().