Combine 2 nodes into one in XML using XSLT

Here should go questions about transforming XML with XSLT and FOP.
AAS
Posts: 1
Joined: Wed May 20, 2020 10:31 pm

Combine 2 nodes into one in XML using XSLT

Post by AAS » Wed May 20, 2020 10:35 pm

Hello, I am new to XSLT. Tried hard but want help to combine two nodes using XSLT.
My XML has complex structure.

My Input XML:-

<Root id='1'>
<Nodes>
<Node id="D1">
<name>A1</name>
<Nodes>
<Node id="D1_B1">
<name>ABC</name>
<Nodes>
<Node id="D1_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D1_B2">
<name>ABC2</name>
<Nodes>
<Node id="D1_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
</Node>
<Node id="D2">
<name>B1</name>
<Nodes>
<Node id="D2_B1">
<name>ABC</name>
<Nodes>
<Node id="D2_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D2_B2">
<name>ABC2</name>
<Nodes>
<Node id="D2_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
</Node>
<Node id="D3">
<name>C1</name>
<Nodes>
<Node id="D3_B1">
<name>ABC</name>
<Nodes>
<Node id="D3_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D3_B2">
<name>ABC2</name>
<Nodes>
<Node id="D3_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
</Node>
</Nodes>
</Root>

My desired Output XML should be -:

<Root id='1'>
<Nodes>
<Node id="D2">
<name>B1</name>
<Nodes>
<Node id="D2_B1">
<name>ABC</name>
<Nodes>
<Node id="D2_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D2_B2">
<name>ABC2</name>
<Nodes>
<Node id="D2_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
</Node>
<Node id="D3">
<name>C1</name>
<Nodes>
<Node id="D3_B1">
<name>ABC</name>
<Nodes>
<Node id="D3_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D3_B2">
<name>ABC2</name>
<Nodes>
<Node id="D3_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
<Node id="D1">
<name>A1</name>
<Nodes>
<Node id="D1_B1">
<name>ABC</name>
<Nodes>
<Node id="D1_B1_BL1">
<name>ABCL</name>
</Node>
</Nodes>
</Node>
<Node id="D1_B2">
<name>ABC2</name>
<Nodes>
<Node id="D1_B2_BL2">
<name>ABC23</name>
</Node>
</Nodes>
</Node>
</Nodes>
</Node>
</Node>
</Nodes>
</Root>


I am not able to understand how to group and merge.

Martin Honnen
Posts: 69
Joined: Tue Aug 19, 2014 12:04 pm

Re: Combine 2 nodes into one in XML using XSLT

Post by Martin Honnen » Sat May 23, 2020 1:09 pm

Consider to indent your XML samples so that we can identify the structure. And even if don't know how to write the necessary XSLT you will need to explain in plain English which transformation you want to achieve. Just dumping two samples doesn't allow us to see which transformation you want to implement.

Post Reply