| <html><head><META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Comparisons</title><link href="book.css" type="text/css" rel="stylesheet"><link href="book.css" type="text/css" rel="stylesheet"><meta content="DocBook XSL Stylesheets V1.76.1" name="generator"><link rel="home" href="index.html" title="usermanual"><link rel="up" href="ch02s03.html" title="How to use the XPath 2.0 grammar with PsychoPath"><link rel="prev" href="ch02s03s06.html" title="Range expressions"><link rel="next" href="ch02s03s08.html" title="Conditional Expressions"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="section" title="Comparisons"><div class="titlepage"><div><div><h3 class="title"><a name="Comparisons"></a>Comparisons</h3></div></div></div><p>The simplest comparison operators are |
| <span class="bold"><strong>eq</strong></span>, |
| <span class="bold"><strong>ne</strong></span>, |
| <span class="bold"><strong>lt</strong></span>, |
| <span class="bold"><strong>le</strong></span>, |
| <span class="bold"><strong>gt</strong></span>, |
| <span class="bold"><strong>ge</strong></span>. These compare two atomic values of the same type, for example two integers, two dates, or two strings. (Collation hasn’t been implemented in current version of PsychoPath). If the operands are not atomic values, an error is raised. |
| </p><p>The operators |
| <span class="bold"><strong>=''', </strong></span>!=' |
| <span class="italic">, '</span><=' |
| <span class="italic">, '</span>> |
| <span class="bold"><strong>, '''<</strong></span>, and |
| <span class="bold"><strong>>=</strong></span> can compare arbitrary sequences. The result is true if any pair of items from the two sequences has the specified relationship, for example |
| <span class="italic">$A = $B</span> is true if there is an item in |
| <span class="italic">$A</span> that is equal to some item in |
| <span class="italic">$B</span>. |
| </p><p>The operators |
| <span class="bold"><strong>is</strong></span> and |
| <span class="bold"><strong>isnot</strong></span> test whether the operands represent the same (identical) node. For example, |
| <span class="italic">title |
| <a class="ulink" href="1" target="_top">1</a> is * |
| <a class="ulink" href="@note" target="_top">@note</a> |
| <a class="ulink" href="1" target="_top">1</a> |
| </span> is true if the first title child is the first child element that has a |
| <span class="italic">@note</span> attribute. If either operand is an empty sequence the result is an empty sequence (which will usually be treated as false). |
| </p><p>The operators |
| <span class="bold"><strong><<</strong></span> and |
| <span class="bold"><strong>>></strong></span> test whether one node precedes or follows another in document order. Consider this XML document (XPexample.xml): |
| <source lang="xml"> |
| </p><div class="literallayout"><p><book><br> |
| <title>Being a Dog Is a Full-Time Job</title><br> |
| <author>Charles M. Schulz</author><br> |
| <character><br> |
| <name>Snoopy</name><br> |
| <friend-of>Peppermint Patty</friend-of><br> |
| <since>1950-10-04</since><br> |
| <age>2</age><br> |
| <qualification>extroverted beagle</qualification><br> |
| </character><br> |
| <character><br> |
| <name>Peppermint Patty</name><br> |
| <since>1966-08-22>/since><br> |
| <age>4</age><br> |
| <qualification>bold, brash and tomboyish</qualification><br> |
| </character><br> |
| </book><br> |
| </p></div><p> </p><p>This file conforms to the following Schema (XPexample.xsd):</p><p><?xml version="1.0" encoding="UTF-8"?></p><div class="literallayout"><p><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"<br> |
| elementFormDefault="qualified"><br> |
| </p></div><div class="literallayout"><p> <xs:element name="book"><br> |
| <xs:complexType><br> |
| <xs:sequence><br> |
| <xs:element ref="title" /><br> |
| <xs:element ref="author" /><br> |
| <xs:element maxOccurs="unbounded" ref="character" /><br> |
| </xs:sequence><br> |
| </xs:complexType><br> |
| </xs:element><br> |
| </p></div><div class="literallayout"><p> <xs:element name="title" type="characterName"></xs:element><br> |
| </p></div><div class="literallayout"><p> <xs:element name="author" type="characterName"></xs:element><br> |
| </p></div><div class="literallayout"><p> <xs:element name="character"><br> |
| <xs:complexType><br> |
| <xs:sequence><br> |
| <xs:element ref="name" /><br> |
| <xs:element ref="friend-of" minOccurs="0" /><br> |
| <xs:element ref="since" /><br> |
| <xs:element ref="age" /><br> |
| <xs:element ref="qualification" /><br> |
| </xs:sequence><br> |
| </xs:complexType><br> |
| </xs:element><br> |
| <xs:element name="name" type="characterName" /><br> |
| <xs:element name="friend-of" type="characterName" /><br> |
| <xs:element name="since" type="xs:date" /><br> |
| <xs:element name="age" type="xs:nonNegativeInteger" /><br> |
| <xs:element name="qualification" type="xs:string" /><br> |
| </p></div><div class="literallayout"><p> <xs:simpleType name="characterName"><br> |
| <xs:restriction base="xs:normalizedString"><br> |
| <xs:minLength value="1"/><br> |
| </xs:restriction><br> |
| </xs:simpleType><br> |
| </xs:schema><br> |
| </p></div><p> </p><p> |
| <span class="bold"><strong> |
| <span class="italic">Examples:</span> |
| </strong></span> |
| </p><div class="literallayout"><p>book/character[name="Snoopy"] &lt;&lt; book/character[name="Peppermint Patty"] <br> |
| </p></div><p> |
| |
| <span class="bold"><strong>result:</strong></span> |
| </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>xs:boolean: true</p></li></ol></div><div class="literallayout"><p>book/character[name="Peppermint Patty"] &lt;&lt; book/character[name="Snoopy"]<br> |
| </p></div><p> |
| |
| <span class="bold"><strong>result:</strong></span> |
| </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>xs:boolean: false</p></li></ol></div></div></body></html> |