Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

text(lvalue) op text(rvalue)

a < GGG

Integer

(long) lvalue op (long) rvalue

a < 3

Real

(double) lvalue op (double) rvalue

a < 3.1

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

author[last-name = "Bob"]

All <author> elements that contain at least one <last-name> element with the value Bob.

author[last-name[1] = "Bob"]

All <author> elements whose first <last-name> child element has the value Bob.

author/degree[@from != "Harvard"]

All <author> elements that contain <degree> elements with a from attribute that is not equal to "Harvard".

author[last-name = /editor/last-name]

All <author> elements that contain a <last-name> element that is the same as the <last-name> element inside the <editor> element under the root element.

author[. = "Matthew Bob"]

All <author> elements whose string value is Matthew Bob.

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 &lt;, &lt;=, &gt;, and &gt;= 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

author[last-name = "Bob" and price &gt; 50]

All <author> elements that contain a <last-name> element with the value Bob, and a <price> element with a value greater than 50.

degree[@from != "Harvard"]

All <degree> elements with a from attribute that is not equal to "Harvard".

book[position() &lt;= 3]

The first three <book> elements (1, 2, 3) in the XML file.

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>

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.