Supposed I have the following XML file with nodes I would like to rearrange based on rules:
<root>
<subsection key="KeyR">Some text</subsection>
<subsection key="KeyC">Some text</subsection>
<subsection key="KeyE">Some text</subsection>
<subsection key="KeyG">Some text</subsection>
<subsection key="KeyH">Some text</subsection>
<subsection key="KeyI">Some text</subsection>
<subsection key="KeyF">Some text</subsection>
<subsection key="KeyJ">Some text</subsection>
<subsection key="KeyL">Some text</subsection>
<subsection key="KeyA"/>
<subsection key="KeyM">Some text</subsection>
<subsection key="KeyN">Some text</subsection>
<subsection key="KeyO">Some text</subsection>
<subsection key="KeyS">Some text</subsection>
<subsection key="KeyP">Some text</subsection>
<subsection key="KeyQ">Some text</subsection>
<subsection key="KeyD">Some text</subsection>
<subsection key="KeyB"/>
<subsection key="KeyT">Some text</subsection>
<subsection key="KeyK">Some text</subsection>
<subsection key="KeyZ">Some text</subsection>
</root>
and rules for the rearrangement like the following ones:
section01
KeyA
KeyM
KeyZ
section02
KeyL
KeyN
KeyP
section03
..
..
section04
..
These rules assign the key of a subsection to a new parend section. So that the following XML file would result:
<root>
<section1>
<subsection key="KeyA"/>
<subsection key="KeyM">Some text</subsection>
<subsection key="KeyZ">Some text</subsection>
</section1>
<section2>
<subsection key="KeyL">Some text</subsection>
<subsection key="KeyN">Some text</subsection>
<subsection key="KeyP">Some text</subsection>
</section2>
<section3>
...
</section3>
...
</root>
Would be an XSL transformation the appropriate means? How would such a transformation look? What would be an appropriate manifestation for the rules so that the rules can be maintained easily?
If you could have a rules.xml file living (for example) in the same directory as the XSLT stylesheet file, in (for example) the following format:
rules.xml
you could then apply the following stylesheet to your input:
XSLT 1.0
to get the following result:
Edit:
To suppress section rules that do not have matching subsections in the input document, try: