I have a simple XML with two levels (Header and Line) of tags such as:
<?xml version="1.0"?> <Header> <line>Line 1</line> <line>Line 2</line> <line>Line 3</line> <line>Line 4</line> <line>Line 5</line> <line>Line 6</line> <line>Line 7</line> <line>Line 8</line> <line>Line 9</line> </Header>
I need to group the lines on sets of X (X=3 for example) lines so that my output is the following:
<?xml version="1.0"?> <Header> <set> <line>Line 1</line> <line>Line 2</line> <line>Line 3</line> </set> <set> <line>Line 4</line> <line>Line 5</line> <line>Line 6</line> </set> <set> <line>Line 7</line> <line>Line 8</line> <line>Line 9</line> </set> </Header>
How do I write a XSLT that can do this kind of transformation?
The following transformation produces the required result:
When applied on the provided XML document:
the result is:
Do note the following:
The use of the XPath
modoperator to find out the firstlineelement in every group ofvNelements.The use of modes, in order to be able to process different
lineelements by different templates