【问题标题】:Use xpath to find values but exclude certain elements使用 xpath 查找值但排除某些元素
【发布时间】:2020-11-17 14:35:43
【问题描述】:

我正在尝试搜索 XML 文件以查找匹配项,但我不想搜索每个元素。 not 语句不起作用所以我做错了什么? (之前没有使用过 xpath)

我想排除元素“product_image”和“category_image”

$items = $xml->xpath("//item/*[contains(text(), '$find')][not(product_image, category_image)]/parent::*");


<item>
    <id>108</id>
    <title>Big car</title>
    <cat>SUV</cat>
    <color>black</color>
    <product_image>CUa6wVWiHAXHI68mtbUFsNj-F05lDM62_250x250.png</product_image>
    <category_image>comeUYh5I9Juz2hr1NBZCSx9cKSgXXNA_250x250.png</category_image>
</item>
[...]

【问题讨论】:

  • 你当前/想要的输出是什么?
  • 它会搜索每一个元素,这不是我想要的。 NOT 部分不起作用
  • 您希望它如何工作?什么条件?
  • 很好地搜索 ITEM 的每个子项,但不是 2 个字段(*_image)
  • 什么是[contains(text(), '$find')] 谓词?应该怎么做?

标签: php xml xpath


【解决方案1】:

试试这个 XPath 以获得所需的输出:

//item/*[not(self::product_image or self::category_image)]

在XPath2.0中可以

//item/*[not(name()=("product_image", "category_image"))]

//item/*[not(ends-with(name(), "_image"))]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多