Take this XML example:
<root>
<grandParent GPid="1" hidden="false">
<parent Pid="1" hidden="false">
<child Cid="1" hidden="false"/>
<child Cid="2" hidden="true"/>
</parent>
<parent Pid="2" hidden="false">
<child Cid="3" hidden="false"/>
<child Cid="4" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="2" hidden="false">
<parent Pid="3" hidden="false">
<child Cid="5" hidden="true"/>
</parent>
<parent Pid="4" hidden="true">
<child Cid="6" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="3" hidden="true">
<parent Pid="5" hidden="false">
<child Cid="7" hidden="false"/>
</parent>
</grandParent>
</root>
I need some sort of filter to get a copy of this where all the nodes marked "hidden" are removed like so:
<root>
<grandParent GPid="1" hidden="false">
<parent Pid="1" hidden="false">
<child Cid="1" hidden="false"/>
</parent>
<parent Pid="2" hidden="false">
<child Cid="3" hidden="false"/>
<child Cid="4" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="2" hidden="false">
<parent Pid="3" hidden="false"/>
</grandParent>
</root>
I tried using something like this
var newXML:XML = XML(root.(grandParent.@hidden != "true").(grandParent.parent.@hidden != "true").(grandParent.parent.child.@hidden !=true);
But that really just gives me back the original XML (since I'm asking for the root where those conditions are met I get the root). I understand why my approach doesn't work, but I don't know where to go from here.
So here's what I was able to come up with, but I don't like having to loop. Let me know if you have a better way:
You could use a recursive function like this assuming your XML is in a variable myXML. Doing it this way, you would not be tied to the name of your elements (ie. grandParent, parent, child) and you would not be restricted in the number of levels (ie. you could add a
<pet>
node to each<child>
node.)