【发布时间】:2019-08-06 23:24:22
【问题描述】:
我需要使用 PHP 删除一些与 XML 上的特定字符串匹配的元素,我想我可以使用 DOM 来执行此操作,因为我一直在阅读。问题来自于使用多个字符串。
我有这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
</item>
<item>
<reference>00003</reference>
<other_string>PRODUCT 3</other_string>
<brand>BRAND 3</brand>
</item>
<item>
<reference>00004</reference>
<other_string>PRODUCT 4</other_string>
<brand>BRAND 4</brand>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
</item>
</products>
并且我需要删除与<brand></brand> 标记上的字符串“BRAND 3 和 BRAND 4”匹配的元素,并获得与此类似的 XML
<?xml version="1.0" encoding="utf-8"?>
<products>
<item>
<reference>00001</reference>
<other_string>PRODUCT 1</other_string>
<brand>BRAND 1</brand>
</item>
<item>
<reference>00002</reference>
<other_string>PRODUCT 2</other_string>
<brand>BRAND 2</brand>
</item>
<item>
<reference>00005</reference>
<other_string>PRODUCT 5</other_string>
<brand>BRAND 5</brand>
</item>
</products>
我们将不胜感激。
【问题讨论】:
-
使用 xpath 获取所有
brand标签。根据您的过滤规则检查他们的内容。如果它们匹配,请转到item并删除该项目