XML XPath Comparison Operators
- XPath provides comparison operators that allow us to compare two values for the purpose of evaluating conditions.
- These operators are used in XPath expressions to filter and select nodes from an XML document.
Syntax
The following comparison operators are supported in XPath:
Operator | Description |
---|---|
= | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
The syntax for using a comparison operator in an XPath expression is as follows:
expression1 operator expression2
Where expression1
and expression2
are the expressions being compared, and operator
is one of the comparison operators listed in the table above.
Example
Consider the following XML document:
<bookstore>
<book>
<title>XPath and XQuery</title>
<author>Priscilla Walmsley</author>
<price>30.00</price>
</book>
<book>
<title>XML Schema</title>
<author>Eric van der Vlist</author>
<price>20.00</price>
</book>
<book>
<title>XSLT</title>
<author>Michael Kay</author>
<price>40.00</price>
</book>
</bookstore>
To select all the books that have a price greater than or equal to 30.00, we can use the following XPath expression:
/bookstore/book[price >= 30.00]
The expression price >= 30.00
compares the price
element of each book
element with the value 30.00
.
Output
The output of the above XPath expression would be:
<book>
<title>XPath and XQuery</title>
<author>Priscilla Walmsley</author>
<price>30.00</price>
</book>
<book>
<title>XSLT</title>
<author>Michael Kay</author>
<price>40.00</price>
</book>
This is because only the first and the third book
elements have a price
element with a value greater than or equal to 30.00
.
Explanation
The comparison operator >=
in the XPath expression /bookstore/book[price >= 30.00]
works by comparing the price
element of each book
element with the value 30.00
. If the value of the price
element is greater than or equal to 30.00
, the corresponding book
element is selected.
Use
Comparison operators are commonly used in XPath expressions to filter and select nodes from an XML document based on a specific condition. For example, we can use comparison operators to:
- Select all the
book
elements that have aprice
element with a value greater than or equal to a specific amount. - Select all the
employee
elements that have asalary
element with a value less than a specific amount. - Select all the
student
elements that have agrade
element with a value greater than or equal to a specific grade.
Important Points
- Comparison operators are used in XPath expressions to evaluate conditions.
- XPath supports six comparison operators:
=
,!=
,<
,>
,<=
, and>=
. - Comparison operators can be used to filter and select nodes from an XML document based on specific conditions.
- Comparison operators can be combined with logical operators (such as
and
,or
, andnot
) to create more complex conditions.
Summary
XPath comparison operators allow us to compare values and evaluate conditions in XPath expressions. These operators are used to filter and select nodes from an XML document based on specific conditions. XPath supports six comparison operators: =
, !=
, <
, >
, <=
, and >=
. Comparison operators can be used in combination with other XPath features, such as axes and logical operators, to create more complex expressions.