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

OperatorMeaningExample
=Equal toprice = 299
!=Not equal tostatus != 'inactive'
<Less than (write as &lt; in XML)age < 18
>Greater than (write as &gt; in XML)salary > 50000
<=Less than or equalmarks <= 40
>=Greater than or equalquantity >= 10

Important Note on < and > in XSLT

Inside XSLT stylesheets (which are XML documents), the less-than sign must be escaped as &lt; and greater-than as &gt; to avoid breaking the XML syntax.

<!-- Inside XSLT -->
<xsl:if test="price &lt; 500"> ... </xsl:if>
<xsl:if test="marks &gt; 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

OperatorMeaningExampleResult
+Addition3 + 47
-Subtraction10 - 37
*Multiplication5 * 630
divDivision10 div 42.5
modModulo (remainder)10 mod 31

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

OperatorMeaningExample
andBoth conditions must be trueage > 18 and age < 60
orAt least one condition must be truestatus='active' or status='pending'
not()Inverts a condition — true becomes falsenot(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 &lt; and > as &gt; 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.

Leave a Comment

Your email address will not be published. Required fields are marked *