XPath Operators
XPath expressions do more than navigate the tree. They also evaluate conditions, perform arithmetic, and combine results using operators. Operators appear most often inside predicates — the filters in square brackets — but also in standalone XPath expressions that return numbers, strings, or booleans.
Categories of XPath Operators
- Comparison operators — compare values
- Arithmetic operators — perform math
- Logical operators — combine boolean conditions
- Union operator — combine node-sets
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | price = 299 |
| != | Not equal to | status != 'inactive' |
| < | Less than (write as < in XML) | age < 18 |
| > | Greater than (write as > in XML) | salary > 50000 |
| <= | Less than or equal | marks <= 40 |
| >= | Greater than or equal | quantity >= 10 |
Important Note on < and > in XSLT
Inside XSLT stylesheets (which are XML documents), the less-than sign must be escaped as < and greater-than as > to avoid breaking the XML syntax.
<!-- Inside XSLT --> <xsl:if test="price < 500"> ... </xsl:if> <xsl:if test="marks > 75"> ... </xsl:if> <!-- In standalone XPath tools you can write: --> price < 500 marks > 75
Comparison with Attribute Values
/products/product[@status = 'active']
→ products where status attribute equals "active"
/employees/employee[@salary > 60000]
→ employees where salary attribute is above 60000
/students/student[@grade != 'F']
→ students where grade attribute is not F
Comparison with Child Elements
/products/product[price < 1000]
→ products where child element price is below 1000
/students/student[marks >= 80]
→ students where child element marks is 80 or above
/orders/order[total = 0]
→ orders where child element total equals zero
Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 3 + 4 | 7 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 5 * 6 | 30 |
| div | Division | 10 div 4 | 2.5 |
| mod | Modulo (remainder) | 10 mod 3 | 1 |
Note: XPath uses div for division instead of /, because / is the path separator. XPath uses mod instead of % for the modulo operation.
Arithmetic in Predicates
/items/item[position() mod 2 = 1]
→ Every odd-positioned item (1st, 3rd, 5th...)
/items/item[price * quantity > 5000]
→ Items where price times quantity exceeds 5000
/report/row[last() - position() < 3]
→ The last 3 rows of the report
Arithmetic in xsl:value-of
<xsl:value-of select="price * quantity"/>
→ Calculates and outputs: price × quantity
<xsl:value-of select="price * 0.18"/>
→ Calculates 18% of price (GST amount)
<xsl:value-of select="price + (price * 0.18)"/>
→ Price including 18% tax
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
| and | Both conditions must be true | age > 18 and age < 60 |
| or | At least one condition must be true | status='active' or status='pending' |
| not() | Inverts a condition — true becomes false | not(status='inactive') |
Examples
/employees/employee[salary > 50000 and department = 'Engineering']
→ Engineers earning above 50000
/products/product[category='electronics' or category='computers']
→ Products in either category
/students/student[not(grade='F')]
→ All students who did not fail
/orders/order[total > 0 and status != 'cancelled']
→ Active orders with a positive total
/items/item[price > 100 and (category='book' or category='stationery')]
→ Books or stationery above 100
Union Operator |
The pipe character combines two node-sets into one result. Both sides must be valid XPath expressions.
//name | //title
→ All name elements AND all title elements
/catalog/product | /catalog/service
→ Both product and service elements
/report/header | /report/footer
→ Both header and footer elements
String Comparison Rules
When comparing with = or !=, string values in XPath follow these rules:
- Comparison is case-sensitive. "Active" and "active" are not equal.
- Leading and trailing spaces matter. " open " is not equal to "open".
- Numbers compared with strings convert automatically — 5 = "5" is true.
[@status = 'Active'] → matches "Active" but not "active" [@status = 'active'] → matches "active" but not "Active"
Complete Operator Example
XML
<inventory>
<item sku="I001" available="true">
<name>Monitor</name><price>8500</price><quantity>5</quantity>
</item>
<item sku="I002" available="false">
<name>Keyboard</name><price>750</price><quantity>0</quantity>
</item>
<item sku="I003" available="true">
<name>Webcam</name><price>1800</price><quantity>12</quantity>
</item>
</inventory>
XPath Queries Using Operators
/inventory/item[@available='true']
→ Monitor and Webcam (both available)
/inventory/item[price > 1000 and @available='true']
→ Monitor and Webcam (price above 1000 AND available)
/inventory/item[quantity = 0 or not(@available='true')]
→ Keyboard (quantity is 0, also not available)
/inventory/item[price * quantity > 10000]
→ Monitor (8500 × 5 = 42500) and Webcam (1800 × 12 = 21600)
Key Points to Remember
- Comparison operators: =, !=, <, >, <=, >= compare node values and attribute values.
- Inside XML documents write < as < and > as > to avoid breaking XML syntax.
- Arithmetic operators: +, -, *, div, mod — note div and mod instead of / and %.
- Logical operators: and, or, not() — combine multiple conditions.
- The union operator | combines two node-sets into one result.
- String comparisons are case-sensitive.
- Arithmetic expressions work inside predicates and in xsl:value-of select attributes.
