2
votes

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
If you want to get a 'record' which has 'UniqueName3' string, you should use XPATH like record[value/text()='UniqueName3']Navin Rawat
Thank you, that was exactly what I was looking for!Wrath

1 Answers

5
votes

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