I have the following XML document:
<?xml version="1.0" encoding="utf-8"?>
<greatGrandParent>
<grandParent>
<parent>
<sibling>Fred</sibling>
<sibling>Max</sibling>
<sibling>Katie</sibling>
</parent>
<parent>
<sibling>Lindy</sibling>
<sibling>Richard</sibling>
</parent>
<parent>
<sibling />
</parent>
</grandParent>
<grandParent>
<parent>
<sibling>Steve</sibling>
<sibling>Abbas</sibling>
</parent>
<parent>
<sibling>Kate</sibling>
<sibling>James</sibling>
<sibling>Ian</sibling>
</parent>
</grandParent>
<grandParent>
<parent>
<sibling />
</parent>
</grandParent>
</greatGrandParent>
My requirement is - to remove an XML node if all of its child elements are empty. For instance, in the above shown XML,
the 3rd parent of the 1st grandParent has no siblings. Hence, the sibling should be removed. And since the parent has no siblings, the parent should be removed as well from the XML. But, the grandParent would still exist since it has other parents with siblings.
The only parent within the 3rd grandparent has no siblings. Hence, the sibling needs to be removed. And since the parent has no sibling, the parent should be removed as well. Since, the grandParent has no child elements(parent), the grandParent must also be removed from the XML.
Hence, the resulting XML must look like:
<?xml version="1.0" encoding="utf-8"?>
<greatGrandParent>
<grandParent>
<parent>
<sibling>Fred</sibling>
<sibling>Max</sibling>
<sibling>Katie</sibling>
</parent>
<parent>
<sibling>Lindy</sibling>
<sibling>Richard</sibling>
</parent>
</grandParent>
<grandParent>
<parent>
<sibling>Steve</sibling>
<sibling>Abbas</sibling>
</parent>
<parent>
<sibling>Kate</sibling>
<sibling>James</sibling>
<sibling>Ian</sibling>
</parent>
</grandParent>
</greatGrandParent>
I'd be glad if somebody can suggest me a solution to do this using any Java API or XSLT.