XPath: nodes that have a node child that has an attribute

XML snippet

    <component name='Stipulations'>
        <group name='NoStipulations' required='N'>
            <field name='StipulationType' required='N' />
            <field name='StipulationValue' required='N' />
        </group>
    </component>
    <component name='NestedParties3'>
        <group name='NoNested3PartyIDs' required='N'>
            <field name='Nested3PartyID' required='N' />
            <field name='Nested3PartyIDSource' required='N' />
            <field name='Nested3PartyRole' required='N' />
            <group name='NoNested3PartySubIDs' required='N'>
                <field name='Nested3PartySubID' required='N' />
                <field name='Nested3PartySubIDType' required='N' />
            </group>
        </group>
    </component>
    <component name='UnderlyingStipulations'>
        <group name='NoUnderlyingStips' required='N'>
            <field name='UnderlyingStipType' required='N' />
            <field name='UnderlyingStipValue' required='N' />
        </group>
    </component>

I want all the "group" nodes to have a child element of type "field" and the name "StipulationType".

This is what I have tried so far:

dictionary.XPathSelectElements("group[field[@name='StipulationType']]")
dictionary.XPathSelectElements("group[./field[@name='StipulationType']]")
+3
source share
2 answers

Problem:

dictionary.XPathSelectElements("group[field[@name='StipulationType']]")

This selects all elements of the group that satisfy the predicate, which are children of the current node.

However, you need all the elements of the group (satisfying the predicate) - it can be clearly seen from the XML fragment that not all elements of the group have the same parent.

Solution :

Rate the XPath expression from grand-grandparent (based on the provided snippet!):

One example would be:

component/group[field[@name='StipulationType']] 

( ) .

:

//, ( XPath), ( ), node.

+1

. , XPath :

//group[field[@name='StipulationType']]

/component/group[field[@name='StipulationType']]

+2

Source: https://habr.com/ru/post/1745908/


All Articles