I’ve been trying to transform some simple XML into another simple XML using XSLT. I am new to XSLT, so if someone could give me an example I will expand on it.
I have arbitrary XML files: e.g
<element>
<child_element>
<grandchild_element>
only one
</grandchild_element>
</child_element>
<child_element>
<grandchild_element>
one
</grandchild_element>
<grandchild_element>
two
</grandchild_element>
</child_element>
</element>
From which I want to produce:
<tree>
<item class="element" id="1">
<item class="child_element" id="11">
<item class="grandchild_element" id="111" value="only one"/>
</item>
<item class="child_element" id="12">
<item class="grandchild_element" id="121" value="only one"/>
<item class="grandchild_element" id="122" value="only one"/>
</item>
</item>
</tree>
Thanks!
One of the simplest/shortest solutions (only 3 templates no modes, no axes, no
count(), only a singlexsl:attribute) that is at the same time most generic (works with any element names):when this transformation is applied on the provided XML document:
the wanted, correct result is produced: