获取具有唯一属性值的节点


Get nodes with unique attribute value

我想清理一个XML文件,并且不允许节点具有具有相同值的属性。XML如下所示:

<bar>
  <foo id="123" some="attribute"/>
  <foo id="abc" some="other attribute"/>
  <foo id="123" some="different attribute"/>
</bar>

它应该看起来像:

<bar>
  <foo id="abc" some="other attribute"/>
  <foo id="123" some="different attribute"/>
</bar>

我正在使用PHP,但我不想做很多循环,所以我想在DOMXPath对象中使用XPath请求。我发现 Xpath 非重复值函数可能会有所帮助,但据我了解,它仅适用于重复节点,不适用于节点重复属性。

XPath 中有解决方案,甚至是 PHP 中的出色算法吗?

尝试遵循 xpath

/bar/foo[not(@id = following-sibling::foo/@id)]

我没有测试太多,因为您只提供了很少的测试数据,但我认为它可以工作。