Letterpart
asked on
adding structure to flat-ish XML file via XSLT
I have some data that, when simplified, looks like this:
A type="1" attribute on the section element indicates the start of a group that ends when either another type="1" is found, or when a nonsection element is found.
I thought that putting some structure into the XML would help me with some work I need to do at the group level.
And so I would like to be able to transform the above to this:
And that would then enable me to 'do something' in sectiongroup.
Can anyone help with the XSLT that would transform the data to add in sectiongroup elements?
<data>
<section>A..</section>
<section type="1">B..</section>
<section>C..</section>
<section>D..</section>
<section type="1">E..</section>
<section>F..</section>
<section>G..</section>
<section>H..</section>
<nonsection>J..</nonsection>
<nonsection>K..</nonsection>
</data>
A type="1" attribute on the section element indicates the start of a group that ends when either another type="1" is found, or when a nonsection element is found.
I thought that putting some structure into the XML would help me with some work I need to do at the group level.
And so I would like to be able to transform the above to this:
<data>
<section>A..</section>
<sectiongroup>
<section type="1">B..</section>
<section>C..</section>
<section>D..</section>
</sectiongroup>
<sectiongroup>
<section type="1">E..</section>
<section>F..</section>
<section>G..</section>
<section>H..</section>
</sectiongroup>
<nonsection>J..</nonsection>
<nonsection>K..</nonsection>
</data>
And that would then enable me to 'do something' in sectiongroup.
Can anyone help with the XSLT that would transform the data to add in sectiongroup elements?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, thanks Geert.
The XSLT2 solution should be fine for me - but very interesting to compare them.
Thanks.
The XSLT2 solution should be fine for me - but very interesting to compare them.
Thanks.
welcome
but a bit of recursive processing on the following axis,
kind of hard to grasp
Open in new window