简体   繁体   中英

xpath - get parent Id by children with condition

I have XML that looks like that:

<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 .

So in this example the output should be just 4326789 , because the first M element in this H element fulfills the condition.

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' ).

Any help will be appreciated!

Try below XPath to get required output:

//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")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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