Barry Hamilton
Barry Hamilton

Reputation: 973

php simplexml xpath match element by attribute and children by text value

I have the following xml (in reality much longer)

    <excluded_postcodes>
   <postcode Outward="TR1">
    <Inward>1DH</Inward>
    <Inward>1DQ</Inward><
    Inward>1DW</Inward>
    <Inward>1DZ</Inward>
    <Inward>1EP</Inward>
    <Inward>1ET</Inward>
    <Inward>1EU</Inward>
    <Inward>1EX</Inward>
       </postcode>
       <postcode Outward="TR1">
            <Inward>1PT</Inward>
    <Inward>1QA</Inward>
    <Inward>1QH</Inward>
    <Inward>1SE</Inward>
    <Inward>1SG</Inward>
       </postcode>
    </excluded_postcodes>

I can match for postcodes of given outward value fine using simplexml xpaths

$xml->xpath('/excluded_postcodes/postcode[@Outward="TR1"]');

What I would like to do is match the children of a given Inward Value of postode[@Outward="TR1"]

$xml->xpath('/excluded_postcodes/postcode[@Outward="TR1"]/Inward//text()="1EU"');

But I am having no luck trying to establish the correct xpath to achieve this. Any help much appreciated.

Thank you

Upvotes: 2

Views: 901

Answers (1)

Varon
Varon

Reputation: 3916

/excluded_postcodes/postcode[@Outward="TR1"]/Inward[text()="1EU"]

Upvotes: 2

Related Questions