Im working with PHP5, and I need to transform XML in the following form:
<list>
<item label="(1)">some text</item>
<item label="(2)">
<anotherNode>some text</anotherNode
<item label="a">some text</item>
<item label="b">some text</item>
</item>
</list>
Into something like this:
<list>
<item label="(1)">some text</item>
<item label="(2)">
<anotherNode>some text</anotherNode>
<list> <!-- opening new wrapper node-->
<item label="a">some text</item>
<item label="b">some text</item>
</list> <!-- closing new wrapper node-->
</item>
</list>
As you can see above I need to add a wrapper node to any 'item' nodes that are not wrapped by the 'list' node already.
What are possible solutions for transforming source xml to the target xml?
UPDATED:
Note 1: Any single or group of <item>
nodes needs to be wrapped by a <list>
node if its not wrapped already.
Note 2: Order of the content needs to be maintained.
Note 3:
If there are <item>
nodes before and after <anotherNode>
.
It should transform this:
<list>
<item label="(1)">some text</item>
<item label="(2)">
<item label="a">some text</item>
<item label="b">some text</item>
<anotherNode>some text</anotherNode>
<item label="c">some text</item>
<item label="d">some text</item>
</item>
</list>
into this:
<list>
<item label="(1)">some text</item>
<item label="(2)">
<list> <!-- opening new wrapper node-->
<item label="a">some text</item>
<item label="b">some text</item>
</list> <!-- closing new wrapper node-->
<anotherNode>some text</anotherNode>
<list> <!-- opening new wrapper node-->
<item label="c">some text</item>
<item label="d">some text</item>
</list> <!-- closing new wrapper node-->
</item>
</list>
Thanks,