Xml XPath:选择具有特定子元素而不是另一个特定子元素的元素

Xml XPath:选择具有特定子元素而不是另一个特定子元素的元素,xml,xpath,openstreetmap,Xml,Xpath,Openstreetmap,现在看来,总是用源标记Openstreetmap节点是一个坏习惯——只有当源是特定的并且与标记到变更集的源不同时,才应该指定源标记。当节点没有其他标记时,它尤其不受欢迎。并非总是这样——曾经有一段时间变更集标记不是一种标准做法。因此,在我大量编辑的一些地方,我有一些清理工作要做,我打算为自己构建一个工具 例如,让我们从以下示例数据集开始: <?xml version='1.0' encoding='UTF-8'?> <osm version='0.6' upload='true

现在看来,总是用源标记Openstreetmap节点是一个坏习惯——只有当源是特定的并且与标记到变更集的源不同时,才应该指定源标记。当节点没有其他标记时,它尤其不受欢迎。并非总是这样——曾经有一段时间变更集标记不是一种标准做法。因此,在我大量编辑的一些地方,我有一些清理工作要做,我打算为自己构建一个工具

例如,让我们从以下示例数据集开始:

<?xml version='1.0' encoding='UTF-8'?>
<osm version='0.6' upload='true' generator='JOSM'>
<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>
<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="power" v="tower" />
</node>
<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>
</osm>
结果如预期:

Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
结果:这得到了与上面完全相同的结果。关于xpath中的运算符优先级,一定有一些我不理解的地方

只是为了核实一下,我试着

/osm/node/tag[not(@k = "source")]/.. 
结果如预期:

Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
结果如预期:

Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
结果不是我所期望的:

Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element='<node id="3736237027" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771207" lon="-16.3332326">
  <tag k="source" v="Microsoft Bing orbital imagery" />
</node>'
Element='<node id="3736237028" timestamp="2015-09-09T15:27:34Z" uid="160042" user="Jean-Marc Liotier" visible="true" version="1" changeset="33912319" lat="15.4771238" lon="-16.3349496">
  <tag k="source" v="Microsoft Bing orbital imagery" />
  <tag k="power" v="tower" />
</node>'
Element=”
'
元素
'
我是不是误解了not()的工作方式

顺便说一句,所有这些测试都是使用-我不知道它是否是测试XPath的正确工具


我正在尝试的多种组合条件是可以作为单个XPath表达式实现的,还是需要一个两步过程,首先选择具有我需要的标记属性的元素,然后排除我不需要的元素?

第一个XPath可以更简单地写为

/osm/node[tag/@v = "Microsoft Bing orbital imagery"]
然后,您可以添加第二个条件,即“除了源之外没有其他标记”:


问题不在于优先权,而在于范围。您想要否定的是
标记
,而不是它的属性。上一个XPath试图搜索一个标记,其同级标记或self
tag
的@k与“power”不同。

我认为,如果将这两个条件放在同一元素的括号内,它们将矛盾地应用于原始文档中该元素的同一实例。。。因此,在错误的假设下,我没有尝试这种方法。感谢您的尖锐回答-XPath的强大功能让我越来越兴奋,尤其是与我来自的粗糙的文本模式匹配相比。。。