Quantcast
Channel: xsl to only copy some elements recursively and remove some of the descendants - Stack Overflow
Viewing all articles
Browse latest Browse all 3

xsl to only copy some elements recursively and remove some of the descendants

$
0
0

I would like to transform

<?xml version="1.0" ?><mydoc><file><colors><blue /><red /><green /></colors><secret><username /><password /></secret></file></mydoc>

into

<?xml version="1.0" ?><colors><blue /><red /></colors>

In plain english, I would like to copy the colors element recursively, including text, ignore the rest of the XML document and discard the green element.

There are solutions working with the example above but will fail if the XML varies even slightly. For instance by adding the nest element under the color element, or the other element which is not in the hierarchy of the color element, or text that is in the scope of the color element ( GOOD TEXT ) and another that is outside its scope ( BAD TEXT ).

<?xml version="1.0" ?><mydoc><file><colors><nest><blue /><red /><green /></nest>            GOOD TEXT</colors><secret><username /><password /></secret>        BAD TEXT</file><other>BAD TEXT TWO</other></mydoc>

I am most interested in a solution that is generic and not customized to the exemples presented here.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images