I am using XSLT for converting a XML derived from web and convert the same on the fly into the target xml file denoted as output .
I am still unable to do so even after trying a lot , Can anyone please help me out with this conversion .
Source XML
<allocelement>
<hd1>12</hd1>
<hd2>14</hd2>
<hd3>87</hd3>
<alc>1</alc>
<amount>4587</amount>
<code>1111</code>
</allocelement>
<alloclement>
<hd1>12</hd1>
<hd2>14</hd2>
<hd3>87</hd3>
<alc>2</alc>
<amount>80000</amount>
<code>1111</code>
</alloclement>
<alloclement>
<hd1>875</hd1>
<hd2>455</hd2>
<hd3>455</hd3>
<alc>2</alc>
<amount>80000</amount>
<code>1112</code>
</alloclement>
Output Desired
<allocelement>
<Codeheader>
<code>1111</code>
<hd1>12</hd1>
<hd2>14</hd2>
<hd3>87</hd3>
<alc>1</alc>
<amount>4587</amount>
<alc>2</alc>
<amount>80000</amount>
</codeHeader>
<CodeHeader>
<code>1112</code>
<hd1>875</hd1>
<hd2>455</hd2>
<hd3>455</hd3>
<alc>2</alc>
<amount>80000</amount>
</CodeHeader>
</allocelement>
The Grouping is on the basis of Code,[hd1,hd2,hd3] such that the different elements within which have the same Code and [hd1,hd2,hd3] will be merged and only show fields which are different viz. the and .
Also I am using xslt 1.0 .
A significantly shorter and simpler XSLT 1.0 solution:
When this transformation is applied on the following XML document (a single top element wrapping the provided XML fragment):
the wanted, correct result is produced:
Explanation: Proper use of the Muenchian grouping method.
II. XSLT 2.0 solution — again shorter, and much more importantly — syntactically and semantically correct:
UPDATE: The OP has changed the requirements for the outpu.
Here is the corresponding modified XSLT 1.0 solution: