How to find the value of an element based on the attribute value of another element in XPath

I am trying to learn XPath and it is difficult for me since I am in all this. Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
    <details>
        <signature id="sig1">
            <name>mr. Barry Smith</name>
            <telephone type="fixed">01234 123456</telephone>
            <telephone type="mobile">071234562</telephone>
        </signature>

        <signature id="sig2">
            <name>mr. Harry Smith</name>
            <telephone type="fixed">01234 123456</telephone>
        </signature>
    </details>

      

How to find the names of people who have a cell phone, I can get either or but not both.

I've tried things like this:

staffdetails/signature/telephone[@type='mobile']name

      

Also, is there a reference manual for using XPAth so I can easily figure out any query I desire? Using online tutorials I found an explanation of how XPath works, but the examples don't cover enough.

Thank!

+3


source to share


2 answers


This should work:

//signature/name[following-sibling::telephone[@type='mobile']]

      

It reads like:



Pick anyone name

that has a parent signature

and a telephone

that type

= mobile

.

As for the link, I actually learned the most from the examples in the spec !

+1


source


There is no need to use following-sibling::

either nested predicates here. Just do this:



/details/signature[telephone/@type = 'mobile']/name

      

+7


source







All Articles