I have string in below pattern
author~time~assignedAuthor~assignedAuthor/id~assignedAuthor/addr~assignedAuthor/telecom~assignedAuthor/assignedPerson/name~
All the time the first element is root (In above string pattern it is author) and rest of them are child elements delemeted with character ‘~’. I would like to create XML using XSLT 2.0 by making use of above string pattern and I want to produce below XML.
<author>
<time/>
<assignedAuthor>
<id/>
<addr/>
<telecom/>
<assignedPerson>
<name/>
</assignedPerson>
</assignedAuthor>
</author>
Can I have the possible solutions for this.
This transformation:
when applied on the following XML document (using a slightly more complicated string to make this even more challenging):
produces the wanted, correct result:
Explanation:
Tokenization of the sequence of “segments”. Use of
tokenize().Grouping (
xsl:for-each-group) with the attributegroup-adjacentusing as grouping key the first “sub-segment”.For every group building the XML subtree recursively. Use of
current-grouping-key()andcurrent-group()