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

标签 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"]');

我想做的是匹配给定的后置值的子节点[@Outward="TR1"]

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

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

谢谢

最佳答案

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

关于php simplexml xpath按属性匹配元素并按文本值匹配子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343450/

相关文章:

php - 如何调试 fatal error : Allowed memory size of XXXX bytes exhausted (tried to allocate VVV bytes)

php - XPath - 包含函数

xml - XPath:仅基于 text() 获取以下 sibling

php - 将 SimpleXML 导入 PHP。我需要关闭文件吗?

php - 如何从本地计算机获取当前时间

php - 将值放在中心的文本框中

c# - Html Agility Pack 无法使用 xpath 找到列表选项

php - 具有以下同级的强元素后的 xpath 文本不起作用

php - 基于2个子元素选择元素

php - 如何将表单数组值插入 MySQL?