XPath Boolean Operators

You can also use the and and or operators within your XPath expressions. These are referred to as "boolean operators", as they only test for "true" or "false".

With boolean operators, numbers return false if they are zero, otherwise they return true. Empty strings (i.e. "") return false, otherwise they return true.

Boolean Operator Usage Example

The Source XML File

Using the same XML file, containing 13 food items and their associated nutritional value:

The Requirement

This time, we will only display items that are either vegetables or fruit (remember, we have specified this using the type attribute). Like this:

XPath boolean operator example

The Solution

We could modify the code to test the value of the type attribute. We could achieve this using the XPath or operator to test that the value contains vegetable or fruit, as follows: