I want to create as many <Group>
elements as they are in the Main xml dynamically using XSLT as <Group folder ="Group1">
in the new Xml. Also, the <Data>
element added as the last child should be added only once inside.
Main Xml
<Root>
<ClassA>
<Groups>
<Group1>
<Group2>
<Group3>
............
</Group3>
</Group2>
</Group1>
</Groups>
<Data>
<Name>George</Name>
<Class>A</Class>
</Data>
</ClassA>
</Root>
I need an Xml like this
<Data>
<ClassA>
<Group folder = "Group1">
<Group folder = "Group2">
<Group folder = "Group3">
............
<Data>
<Name>George</Name>
<Class>A</Class>
</Data>
</Group>
</Group>
</Group>
</ClassA>
</Data>
Try if is benifial for you.
Actually, you need the following templates:
Matching
Root
. Generate<Data>
element and inside it apply templates to the whole own content.Matching
ClassA
. Copy the own element and inside if apply templates to the content of the childGroups
element.Matching elements with name containing
Group
and then a digit. CreateGroup
element with properfolder
attribute. Apply templates to its content (if any). If this element does not contain any child element, copyData
element from ancestorClassA
.The identity template.
So the whole script can look like below:
For a working example see http://xsltransform.net/nb9MWtw