Consider XML input like this:
<root>
<sub>
<p att1=0 att2=1><i>foo</i></p>
<p att1=1 att2=1><i>bar</i></p>
<p att1=0 att2=0><i>baz</i></p>
<p att1=0 att2=1><i>bazz</i></p>
</sub>
</root>
Which should be transformed to:
<root>
<sub>
<p att1=0 att2=1><i>foo</i><i>bazz</i></p>
<p att1=1 att2=1><i>bar</i></p>
<p att1=0 att2=0><i>baz</i></p>
</sub>
</root>
(Because both p
parent elemets of <i>foo</i>
and <i>bazz</i>
are siblings and have the same attributes.)
How to do such a transformation with HXT arrows?
Ok, there is my try: The code first collects all attribute lists at the parent of the siblings and then does a merge for all attribute lists that are different:
Output for the example
Nice to have
The above version puts the merged children in front and the none-matching ones in the new children list of the parent node: A nice variation would be: insert each merged child at the old position of the first old sibling node and don't change the order of non-merged nodes. For example that
is transformed to
and not to:
Disclaimer
Since I am new to HXT and arrows - I wouldn't be surprised if there are more concise/HXT0idiomatic/elegant answers.