2

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>
kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

2

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

If I understand your question correctly, you are trying to get all the labels which are not inside the b tag.

Try with the below xpath.

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

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39