I need to move the <sectionid/>
and the <sponsor/>
elements inside the corresponding <amendment/>
.
Here is an example:
From :
<root>
<sectionid>A</sectionid>
<sponsor>john</sponsor>
<sponsor>paul</sponsor>
<amendment>
<id>1</id>
<text>some text</text>
</amendment>
<sectionid>B</sectionid>
<sponsor>peter</sponsor>
<amendment>
<id>5</id>
<text>some text</text>
</amendment>
<amendment>
<id>4</id>
<text>some text</text>
</amendment>
<sponsor>max</sponsor>
<amendment>
<id>6</id>
<text>some text</text>
</amendment>
<amendment>
<id>7</id>
<text>some text</text>
</amendment>
</root>
to:
<root>
<amendment>
<sectionid>A</sectionid>
<sponsor>john</sponsor>
<sponsor>paul</paul>
<id>1</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>peter</sponsor>
<id>5</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>peter</sponsor>
<id>4</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>6</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>7</id>
<text>some text</text>
</amendment>
</root>
Note 1: the <section/>
element applies to all the <amendments/>
before the next <sectionid/>
Note 2: the <sponsor/>
element applies to all the <amendments/>
before the next <sponsor/>
list.
Note 3: The values of //amendment/id
are not sequential.
How can this transformation be done with XSLT 1.0.
This transformation:
when applied on the provided XML document:
produces the wanted, correct results:
This stylesheet:
Output:
Note: Fine grained traversal. Linear complexity.
Edit: New input sample.