I’m searching for an XPath string (for VBA Excel) that can look for all nodes on a specific level and that are only returned in case they have a specific (grand) child node. Regardless how ‘deep’ this is nested. Relationale: I don’t know how deep a specific child is nested therefore I can’t make a fixed path.
In case of the example below. I’m searching for all nodes that are directly under the root ” and which have any child that contain the name ‘Test’
XML STRING:
<Program>
<AA name="1"/>
<BB name="2">
<CC name="Test"/>
</BB>
<DD name="Test"/>
<EE name="3">
<FF name="4">
<GG name="Test"/>
</FF>
</EE>
</Program>
REQUIRED SELECTION:
Item(0) = <BB name="2">
Item(1) = <DD name="Test">
Item(2) = <EE name="3">
The following XPath expression gives the nodes under Program
xmlDoc.selectNodes("/Program/*")
The following XPath expression gives the nodes which contain the name Test
xmlDoc.selectNodes("//*[@name="Test"]")
What I need is not a ‘union’ of the 2 paths but a ‘overlap’. Does anybody know how to make this?
The following XPath expression selects what you need:
It fetches all immediate children of
Programand then selects the ones that have anameattribute value of'Test'or contain descendants that do.A more verbose and perhaps more readable version of this expression would be:
So your VBA code can look like this: