I would like to create a new XML parents at the highest possible levels for elements which are between two boundary elements in the JDOM2 structure.
Practically there are no limitations to the placement of the <start> and <stop> elements.
Simplified example:
<root>
<a>
<ax>
<start></start>
</ax>
<ay></ay>
<az></az>
</a>
<b>
<bx></bx>
</b>
<c>
<cx></cx>
<cy></cy>
<cz>
<cza></cza>
<czb>
<stop></stop>
</czb>
</cz>
</c>
output:
<root>
<a>
<ax>
<start></start>
</ax>
<added>
<ay></ay>
<az></az>
</added>
</a>
<added>
<b>
<bx></bx>
</b>
</added>
<c>
<added>
<cx></cx>
<cy></cy>
</added>
<cz>
<added>
<cza></cza>
</added>
<czb>
<stop></stop>
</czb>
</cz>
</c>
Here is some attempt in XQuery 3.1 (which you can run with Java using Saxon HE or BaseX, not sure whether for both over JDOM, I think Saxon supports that https://www.saxonica.com/html/documentation11/sourcedocs/tree-models/thirdparty.html) to identify and wrap the elements e.g.
Result is e.g.
Not well tested and currently, like in the shown input sample, looking only for element nodes to be wrapped, not expecting mixed contents with text nodes split by e.g.
<start/>and<stop/>.Minimal Saxon HE (used 12.3) code to run XQuery against an input file and output the result (for testing to System.out) is e.g.
Example online: https://github.com/martin-honnen/SaxonXQueryWrapSiblingsBetweenMileStones
To use JDOM2 with Saxon HE you need to download the source from https://github.com/Saxonica/Saxon-HE/tree/main/12/source and compile the
net.sf.saxon.option.jdom2package into your Java project (and it seems comment out line 50 in JDOM2DocumentWrapper.java before you do that), then the code to run XQuery on the JDOM Document and return a new one is e.g.Example online: https://github.com/martin-honnen/SaxonXQueryWrapSiblingsBetweenMileStones/tree/UseJDOM