php simplexml xpath按属性匹配元素,按文本值匹配子元素

时间:2012-11-12 12:07:54

标签: php xpath simplexml

我有以下xml(实际上更长)

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

我可以使用simplexml xpaths

匹配给定外向值的邮政编码
$xml->xpath('/excluded_postcodes/postcode[@Outward="TR1"]');

我想做的是匹配给定的postode Inward Value的子项[@Ondward =“TR1”]

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

但我没有运气试图建立正确的xpath来实现这一目标。 任何帮助非常感谢。

谢谢

1 个答案:

答案 0 :(得分:2)

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