This is an extension to the question I asked earlier - XSLT 1.0 Grouping with multiple elements with same name The output format has changed and hence reposting.
I have an XML that looks like -
<resultset>
<hit>
<content>
<ITEM>
<TITLE>Office Cleaning1</TITLE>
<DESCRIPTION>blah blah blah</DESCRIPTION>
<Hierarchy>level1A~level2A~level3A</Hierarchy>
<Hierarchy>level1B~level2B~level3B</Hierarchy>
</ITEM>
</content>
</hit>
<hit>
<content>
<ITEM>
<TITLE>Office Cleaning2</TITLE>
<DESCRIPTION>blah blah blah</DESCRIPTION>
<Hierarchy>level1A~level2A~level3B</Hierarchy>
</ITEM>
</content>
</hit>
<hit>
<content>
<ITEM>
<TITLE>Office Cleaning3</TITLE>
<DESCRIPTION>blah blah blah</DESCRIPTION>
<Hierarchy>level1A~level2B~level3C</Hierarchy>
</ITEM>
</content>
</hit>
<hit>
<content>
<ITEM>
<TITLE>Office Cleaning4</TITLE>
<DESCRIPTION>blah blah blah</DESCRIPTION>
<Hierarchy>level1A~level2B~level3B</Hierarchy>
<Hierarchy>level1A~level2B~level3C</Hierarchy>
</ITEM>
</content>
</hit>
<hit>
<content>
<ITEM>
<TITLE>Office Cleaning5</TITLE>
<DESCRIPTION>blah blah blah</DESCRIPTION>
<Hierarchy>level1B~level2B~level3B</Hierarchy>
</ITEM>
</content>
</hit>
</resultset>
Note that there are multiple hierarchy elements which is a concatenated string of level1~level2~level3 I am looking to transform this into something like this -
<TREE>
<LEVELS>
<LEVEL1 name="level1A">
<LEVEL2 name="level2A">
<LEVEL3 name="level3A">
<ITEM Name="Office Cleaning1"/>
</LEVEL3>
<LEVEL3 name="level3B">
<ITEM Name="Office Cleaning2"/>
</LEVEL3>
</LEVEL2>
<LEVEL2 name="level2B">
<LEVEL3 name="level3B">
<ITEM Name="Office Cleaning4"/>
</LEVEL3>
<LEVEL3 name="level3C">
<ITEM Name="Office Cleaning3"/>
<ITEM Name="Office Cleaning4"/>
</LEVEL3>
</LEVEL2>
</LEVEL1>
<LEVEL1 name="level1B">
<LEVEL2 name="level2B">
<LEVEL3 name="level3B">
<ITEM Name="Office Cleaning1"/>
<ITEM Name="Office Cleaning5"/>
</LEVEL3>
</LEVEL2>
</LEVEL1>
</TREE>
Basically each item has multiple hierarchy associated with it. I need to group them together and each levels to be grouped together as well.
I can actually change the values in the HIERARCHY element in the input XML to any format that might be easier to extract. For e.g. I can make it look like LEVEL1:level1A~LEVEL2:level2A~LEVEL3:level3A but I cannot add new elements.