简体   繁体   English

xpath-由有条件的孩子获取父ID

[英]xpath - get parent Id by children with condition

I have XML that looks like that: 我有看起来像这样的XML:

<doc>   
    <H Id="4732894">
      <M Ind="aa">
        <A Id="G">A</A>
        <A Id="S">M</A>
      </M>
    </H>
    <H Id="4326789">
      <M Ind="aa">
        <A Id="G">B</A>
        <A Id="S">F</A>
      </M>
      <M Ind="ab">
        <A Id="G">B</A>
        <A Id="S">M</A>
      </M>
      <M Ind="ac">
        <A Id="G">3</A>
        <A Id="S">F</A>
      </M>
      <M Ind="ad">
        <A Id="G">2</A>
        <A Id="S">F</A>
      </M>
    </H>
</doc>

I need to take the Id of all the H elements that have M elements that have A elements with Id=G = B or C in the text, and Id=S = F . 我需要获取所有具有M元素的H元素的Id ,其中M元素具有A元素,其中Id=G = BC在文本中,并且Id=S = F

So in this example the output should be just 4326789 , because the first M element in this H element fulfills the condition. 因此,在此示例中,输出应仅为4326789 ,因为此H元素中的第一个M元素满足条件。

This is what I've tried: 这是我尝试过的:

"//H/M/A[@Id='G'][contains(text(),'B C']/../../@Id"

and it already raised an error (so I didn't even try to add the and also for @Id='S' ). 并且它已经引发了一个错误(所以我什至没有尝试为@Id='S'添加and )。

Any help will be appreciated! 任何帮助将不胜感激!

Try below XPath to get required output: 尝试在XPath下面获取所需的输出:

//H[M[A[@Id="G" and .="B"] and A[@Id="S" and .="F"]]]/@id

Output: 输出:

4326789

Note that .="F" search for node with text content that is equal to "F" If you need A node that contains "F" , you can use A[@Id="S" and contains(., "F")] 请注意, .="F"搜索文本内容等于 "F" A节点。如果需要包含 "F"节点,则可以使用A[@Id="S" and contains(., "F")]

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

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