2

I'm trying to create az xpath, where I want to get a node, in which there exists a child node which has an attribute. My problem is that the only difference in this structure is the child attribute. Here's an example to show you what I mean:

<Values>
    <record name="svc_sig">
        <record name="sig_in">
            <array name="rec_fields">
                <record>
                    <value name=field_name">UniqueName1</value>
                </record>
                <record>
                    <value name=field_name">UniqueName2</value>
                </record>
                <record>
                    <value name=field_name">UniqueName3</value>
                </record>
                <record>
                    <value name=field_name">UniqueName4</value>
                </record>
            </array>
        </record>
    </record>
<Values>

For example given UniqueName3 I want to get the record that contains it. So far I tried the following:

/Values/record[@name='svc_sig']/record[@name='sig_in']/array[@name]/record/value[@name='field_name']

With this however I get all the value nodes that has the attribute field_name.

1
  • 4
    If you want to get a 'record' which has 'UniqueName3' string, you should use XPATH like record[value/text()='UniqueName3'] Commented May 2, 2013 at 7:00

1 Answer 1

5

You may try this:

//value[text()="UniqueName3"]/..

This will select the record element which is the parent of the value element which contains UniqueName3 as it's text value.

Here's a proof: http://www.xpathtester.com/obj/1afeedf1-46bd-4adb-841e-da6e6945b6d4 (press the Test button).

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

2 Comments

I believe you can also do .= which is the shorthand of text()=
what if you want to test a node not the text()?

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.