Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath for all elements not with certain parent

Let's say I have the following XML:

<div>
   <a>
      <label>a</label>
   </a>
   <b>
      <label>b</label>
   </b>
   <c>
      <d>
         <label>c-d</label>
      </d>
   </c>
   <b>
      <d>
         <label>b-d</label>
      </d>
   </b>
</div>

I am trying to use Xpath to find all "label" elements, but not inside the <b> tags. In this example, I would want to get

<label>a</label>
<label>c-d</label>
like image 607
user3242477 Avatar asked Sep 05 '25 07:09

user3242477


1 Answers

This XPath,

//label[not(parent::b)]

selects all label elements without a b parent per your title.

This XPath,

//label[not(ancestor::b)]

selects all label elements without a b ancestor per your examples.

like image 113
kjhughes Avatar answered Sep 07 '25 20:09

kjhughes