Comparisons
To compare two objects in XPath, use the =
sign to test for equality, or use !=
to test for inequality.
For a comparison operation, exactly two operands must be supplied. Comparisons are then made by evaluating each operand, and converting them as needed, so they are of the same type. This is done according to the process described below, in "Order of Precedence For Comparisons".
All elements and attributes are strings, but are automatically cast as integer values for numeric comparisons. Literal numeric values are cast to long or double types during comparison operations, as shown in the following table.
For information about <
and other binary comparison operators, see "Binary Comparison Operators", below.
Literal type | Comparison | Example |
---|---|---|
String |
|
|
Integer |
|
|
Real |
|
|
Single or double quotation marks can be used for string delimiters in expressions. This makes it easier to construct and pass patterns from within scripting languages.
For more information about how comparisons are performed using XPath, see section 3.4 ("Booleans") of the XML Path Language (XPath) Version 1.0 (W3C Recommendation 16 November 1999) at www.w3.org/TR/xpath.
Examples
Expression | Refers to |
---|---|
|
All |
|
All |
|
All |
|
All |
|
All |
Order of Precedence for Comparisons
Comparisons with regard to data types obey the order of precedence.
If at least one operand is a Boolean, each operand is first converted to a Boolean.
Otherwise, if at least one operand is a number, each operand is first converted to a number.
Otherwise, if at least one operand is a date, each operand is first converted to a date.
Otherwise, both operands are first converted to strings.
Binary Comparison Operators
A set of binary comparison operators compares numbers and returns Boolean results. The <
, <=
, >
, and >=
operators are used for less than, less than or equal, greater than, and greater than or equal, respectively. Single or double quotation marks can be used for string delimiters in expressions. This makes it easier to construct and pass patterns within scripting languages.
Note that these comparison operators work only with numbers. You can compare strings for equality, but if you want to compare strings to determine which comes first in sort order, you need to use the Microsoft XPath Extension Functions.
Examples
Expression | Refers to |
---|---|
|
All |
|
All |
|
The first three |
Example
XML File (test.xml)
<?xml version="1.0"?>
<test>
<x a="1">
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="2">
<y>y2</y>
</x>
<x a="3">
<y>y3</y>
</x>
</test>
XSLT File (test.xsl)
The following XSLT style sheet selects all the <x>
elements that are the first of their siblings in the document order.
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- Suppress text nodes not covered in subsequent template rule. -->
<xsl:template match="text()"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="*|@*"/>
<xsl:if test="text()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/test">
<xsl:apply-templates select="//x[position() = 1 ] "/>
</xsl:template>
</xsl:stylesheet>
Formatted Output
The transformation applied to the XML file above yields the following result:
<x a="1">
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
<x>
<y>y31</y>
<y>y32</y>
</x>