Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exclude Xpath from other Xpath

If you have two Xpaths you can join them with the | operator to return both their results in one result set. This essentially gives back the union of the two sets of elements. The example below gives back all divs and all spans on a website:

//div | //span

What I need is the difference (subsection). I need all elements in the first Xpath group that are not in the second Xpath group. So far I have seen that there is an except operator but that only works in Xpath2. I need an Xpath1 solution. I have seen that the not function might help but I was not able to make it work.

As an example imagine the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    <td>5</td>
</tr> 

In this example I would have the Xpath group //tr/td. I would want to exclude <td>1</td> and <td>4</td>. Although there are many ways to solve the problem I am specifically looking for a solution where I can say in an Xpath: "Here is a group of elements and exclude this group of elements from it".

>Solution :

An approach realizing this is using the self:: axis and the not() operator in a predicate:
For example, with an XML like this

<root>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
    </tr>    
    <dr>
        <td>1</td>
        <td>4</td>
    </dr>    
</root>

you can use this XPath-1.0 expression:

//tr/td[not(self::*=//dr/td)]

The resulting nodeset is as desired

<td>2</td>
<td>3</td>
<td>5</td>

The XPath expression selects all elements of the first part and checks in the predicate if every element itself (self::*) is in the second part. If it is, it will be excluded (not(...)).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading