简体   繁体   English

XPath查询以选择唯一的唯一节点

[英]XPath query to select ONLY unique nodes

I have what is hopefully a very simple XPath query to build that I'm stuck on (very new to XPath). 我希望可以构建一个非常简单的XPath查询(对XPath来说是新的)。 I have the following xml: 我有以下xml:

 <?xml version="1.0" encoding="utf-8"?>
    <Persons>
      <Person>
        <PersonID>6352</PersonID>
        <Forename>Tristan</Forename>
      </Person>
      <Person>
        <PersonID>6353</PersonID>
        <Forename>Ruth</Forename>
      </Person>
      <Person>
        <PersonID>6913</PersonID>
        <Forename>Mina</Forename>
        <Surname>Asif</Surname>
      </Person>
      <Person>
        <PersonID>6913</PersonID>
        <Forename>Mina</Forename>
        <Surname>Asif</Surname>
      </Person>
      <Person>
        <PersonID>6914</PersonID>
        <Forename>Clark</Forename>
        <Surname>Williams</Surname>
      </Person>
    </Persons>

You can see that Person with ID 6913 is a duplicate. 您可以看到ID为6913的人是重复的。 I the xml in an XmlDocument object and I want to use XPath in the SelectNodes method to select all Persons who have a unique PersonID. 我在XmlDocument对象中使用xml,并且我想在SelectNodes方法中使用XPath来选择所有具有唯一PersonID的Person。 In the case above it should give me 4 out of the 5 Person items. 在上述情况下,应该给我5个人项目中的4。

I also need to select all nodes below the Person item. 我还需要选择“个人”项下面的所有节点。 That is PersonID, Forename, Surname (if one exists). 即PersonID,Forename,Surname(如果存在)。

Any help much appreciated! 任何帮助,不胜感激!

All Person tags that has not Person tag with the same PersonID before 所有之前没有具有相同PersonID的Person标签的Person标签

//Person[not(PersonID = preceding::Person/PersonID)]

it gives you nodes Person with children. 它为您提供节点带孩子的人。 In this case 4 elements: 在这种情况下,有4个元素:

Element='<Person>
  <PersonID>6352</PersonID>
  <Forename>Tristan</Forename>
</Person>'
Element='<Person>
  <PersonID>6353</PersonID>
  <Forename>Ruth</Forename>
</Person>'
Element='<Person>
  <PersonID>6913</PersonID>
  <Forename>Mina</Forename>
  <Surname>Asif</Surname>
</Person>'
Element='<Person>
  <PersonID>6914</PersonID>
  <Forename>Clark</Forename>
  <Surname>Williams</Surname>
</Person>'

要么

//Person[not(PersonID = following::Person/PersonID)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM