I'm trying to use XSLT to create a hierarchical XML file from a flat XML file, and not sure what the best approach is.
e.g. I need to convert
<root>
<inventory bag="1" fruit="apple"/>
<inventory bag="1" fruit="banana"/>
<inventory bag="2" fruit="apple"/>
<inventory bag="2" fruit="orange"/>
</root>
to
<inventory>
<baglist>
<bag id="1"/>
<bag id="2"/>
</baglist>
<bag id="1">
<fruit id="apple"/>
<fruit id="banana"/>
</bag>
<bag id="2">
<fruit id="apple"/>
<fruit id="orange"/>
</bag>
</inventory>
for N bags/fruits
Group
inventory
elements based on the value of theirbag
attribute:xsl:for-each your nodes twice, or use xsl:template with different modes.