tags:

views:

53

answers:

2

XML Fragment

    <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>

What I want is all "group" nodes which have a child node of type "field" and a name "StipulationType".

This is what I've tried so far:

dictionary.XPathSelectElements("group[field[@name='StipulationType']]")
dictionary.XPathSelectElements("group[./field[@name='StipulationType']]")
+2  A: 

Looks good. You might need to get a little more specific with your XPath depending on the implementation:

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

or

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

should work

Jweede
I see what I did wrong. I didn't include the // in front of group, which is vital given my source xml. (My sample doesn't indicate how random the nesting levels truly are.)
Jonathan Allen
ah. Yes, when it doubt use the `//` to search at all levels, but bear in mind the performance penalty this can cause.
Jweede
+1  A: 

The problem:

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

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

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

The solution:

Evaluate the XPath expression from the grand-grandparent (based on the provided fragment!):

One example would be:

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

Based on the structure of the complete document (not provided), more location steps might be necessary.

What to avoid:

Avoid the // abbreviation as it may cause (with not good optimizing XPath engines) an extensive traversal of the whole subtree (even document) that starts from the node against this is evaluated.

Dimitre Novatchev