Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath: skip child node with an given id

Tags:

xpath

I have to configure our enterprise search engine and the indexing of documents is done via xpath selectors. In the current setup there is an xpath

.//div[@id='content']

Which selects basically all elements of the main part of a website. Meanwhile there is an additional div with a lot of nonsense included, so I tried to modify this xpath to skip this div-tag. I am struggling with the documentation regarding "not" but withou any luck so far.

<div id="content">
  <div id="i-want-this">
   ...
  </div>
  <div id="i-do-not-want-this">
   <span>foo</span>
  </div>
  <div id="i-want-this-too">
   ...
  </div>
</div>

While I see that the hints in the comments helped me so far, I still have an issue with child elements in the div-tag I want to skip. Let's say, there is a span-tag inside. If I select

//div[@id='content']/*[not(@id='i-do-not-want-this')] my result still includes this span-content. So I guess, I need a query for all elements below id="content" which do not have a parent id="i-do-not-want-this". Right?

like image 522
vreen Avatar asked Mar 23 '23 05:03

vreen


1 Answers

Use the following query. It will select all child elements which id is not i-do-not-want-this.

//div[@id='content']/*[@id != 'i-do-not-want-this']

or - the same logic - using the ǹot() function (thanks @paul_t)

//div[@id='content']/*[not(@id='i-do-not-want-this')]

Update

When I said the same logic then this isn't really correct. Pleases visit the comment from @IanRoberts

like image 143
hek2mgl Avatar answered Apr 25 '23 21:04

hek2mgl