• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Script Everything

  • Spreadsheets
  • Python
  • Blog
Home ● HTML ● XPath AND, OR, NOT Conditions: Logical Operators (Examples)

XPath AND, OR, NOT Conditions: Logical Operators (Examples)

August 26, 2022 by Ryan Sheehy

How do you apply logical conditions to get multiple elements, very specific elements, or even exclude elements using XPath?

XPath syntax does enable the use of logical operators and , or and not() when searching for elements within your HTML or XML document. To use the logical and and or conditions on obtaining certain elements wrap your syntax in square brackets with a prefixed asterisk.

For example, I recently needed to obtain all the tr tags in a data table and only wanted the tr tags if they were nested within a thead or tbody (not a tfoot tag). Therefore, my syntax looked something like this:

"//table[@id='x']/*[self::thead or self::tbody]/tr

In another example, I needed to find all the anchor tags within a row that did not contain in their link text the words Delete , Edit and View . For this logic I used the not() function as follows:

"//table[@id='x']//tr//a[not(contains(text(), 'Delete')) and not(contains(text(), 'Edit')) and not(contains(text(), 'View'))]"

As you can see from the above snippet the not() function wraps the contains() function which uses the text() function to read the anchor text. It then checks each anchor tag in the row and provided it does not meet any of those conditions then the anchor link is obtained.

SyntaxError Is Not A Valid XPath Expression

If you do get a SyntaxError on your XPath expression and you’ve been using not() and contains() and a plethora of other functions in your XPath expression check you’ve properly closed all your parentheses. It can be easy with all the nested functions to forget to close a rogue parentheses.

Also, check you’re using contains (plural) not contain (singular) if you’re using this function in your code.

Summary

You can use XPath logical operators to get specific elements from an HTML or XML document. To use the logical operators and and or wrap them in square brackets, and when using the not() function wrap your condition within the parentheses.

If you do get a SyntaxError on your XPath expressions check you’ve properly closed your parentheses when using the not() function.

Filed Under: HTML

Primary Sidebar

Hello! My name is Ryan Sheehy the author of ScriptEverything.com

This website acts as a second brain of sorts for all the hacks and explorations I find when trying to solve problems at work.

About Me

Footer

Spreadsheets

I have been using spreadsheets since as early as 1996 and I continue to use this great productivity tool on a daily basis.

Check out some of my most recent discoveries about spreadsheets.

Python Code

I have been using Python since the late 1990’s due to the limitations of Excel’s VBA. I enjoy being able to wrangle and fetch data externally using Python.

Discover more of what I’ve found with Python.

Apps

Sometimes I play, hack and tinker with other applications to scratch an itch.

To find out the latest hack check out my blog feed.

Copyright © 2023 ScriptEverything.com

  • Contact
  • Privacy Policy