[2.0] Node Operators

XPath 2.0 defines three new operators to work with nodes: the is operator, the node-before operator (<<), and the node-after operator (>>).

The is operator

The is operator compares two nodes to see whether they are the same. This compares the nodes themselves, not their values. Continuing our example from the previous section, here’s a stylesheet that illustrates how the operator works:

<?xml version="1.0"?>
<!-- is.xsl -->
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
  <xsl:output method="text"/>
 
  <xsl:variable name="Dougs-favorites" as="node()*">
    <xsl:sequence 
      select="/favorite-books/booklist
              /book[contains(@favorite, 'Doug')]"/>
  </xsl:variable>

  <xsl:variable name="Sheris-favorites" as="node()*">
    <xsl:sequence 
      select="/favorite-books/booklist
              /book[contains(@favorite, 'Sheri')]"/>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:text>A test of the is operator:</xsl:text>
    <xsl:text>&#xA;  Comparing the first nodes of </xsl:text>
    <xsl:text>the sequences:&#xA;</xsl:text>

    <xsl:value-of 
      select="if (subsequence($Dougs-favorites, 1, 1) is 
                  subsequence($Sheris-favorites, 1, 1))
              then '    The first nodes are the same!&#xA;'
              else '    The first nodes aren''t the same!&#xA;'"/>
    
    <xsl:text>  Reversing one sequence and trying it </xsl:text>
    <xsl:text>again:&#xA;</xsl:text>

    <xsl:value-of 
      select="if (subsequence($Dougs-favorites, 1, 1) is 
                  subsequence(reverse($Sheris-favorites), 1, 1))
              then '    The first nodes are the same!&#xA;'
 else ' The first nodes ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.