3

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']]")
1
  • Good Question (+1). See my answer for a solution that often is more efficient than using //. :) Commented May 18, 2010 at 1:30

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
ah. Yes, when it doubt use the // to search at all levels, but bear in mind the performance penalty this can cause.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.