One of the fundamental constructs for XSL transformations and XML links, XPath is nonetheless one of the lesser lights of the XML universe. However, if you're serious about developing your XML skills, you need to know it inside out - and this tutorial has all you need to get started.
In addition to what you've just seen, XPath also allows you to build expressions using simple logical and comparison operators. Consider the following table, which illustrates the common ones with examples:
Operator What It Means Example
----------------------------------------------------------------------------
--
= is
equal to item[desc = 'Ginger']
!= is not equal to
item[desc != 'Ginger']
is greater than servings > 2
<
is less than servings < 5
= is greater than
or equal to servings >= 2
<= is less than or equal to servings
<= 10
Most of these comparison operators are used in conjunction with XSLT's "if" and "choose" tests (note that if you use them in an XSLT stylesheet, the < symbol must be replaced with the pre-defined XML entity < to avoid XML errors.).
You can combine expressions using the "and" and "or" operators,
Operator Example
----------------------------------------------------------------------------
--
and desc = 'Ginger' and quantity = '1 tsp'
or desc = 'Cinnamon' or servings >= 3
Here's an example of how you could apply this in an XSLT stylesheet:
Finally, you can perform arithmetic operations with the various arithmetic operators:
Operator What It Means Example
----------------------------------------------------------------------------
--
+ Addition quantity
+ 5
- Subtraction quantity - 5
* Multiplication quantity * 5
div Division quantity
div 5
mod Modulo quantity mod 5
Here's an example of how you could apply this in an XSLT stylesheet: