<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet href="../make-menu.xsl" type="text/xsl"?><html>
   <head>
      <this-is section="expressions" page="intro" subpage=""/>
      <!--
           Generated at 2010-10-29T18:20:48.427+01:00--><title>Saxonica: XSLT and XQuery Processing: XPath 2.0 Expression Syntax</title>
      <meta name="coverage" content="Worldwide"/>
      <meta name="copyright" content="Copyright Saxonica Ltd"/>
      <meta name="title"
            content="Saxonica: XSLT and XQuery Processing: XPath 2.0 Expression Syntax"/>
      <meta name="robots" content="noindex,nofollow"/>
      <link rel="stylesheet" href="../saxondocs.css" type="text/css"/>
   </head>
   <body class="main">
      <h1>XPath 2.0 Expression Syntax</h1>
      <p>This document is an informal guide to the syntax of XPath 2.0 expressions, which are used in Saxon both within
XSLT stylesheets, and in the Java API. XPath is also a subset of XQuery. For formal specifications, see the
 <a href="http://www.w3.org/TR/xpath20/" class="bodylink">XPath 2.0 specification</a>,
 except where differences are noted here.</p>
      <p>There is also a summary of <a class="bodylink" href="../expressions/xpath30new.xml">new features introduced in XPath 3.0</a> (originally published
      as XPath 2.1).</p>
      <p>XPath expressions may be used either in an XSLT stylesheet, or as a parameter to various Java
interfaces. The syntax is the same in both cases. Saxon supports XPath either through the standard JAXP interface
(which is somewhat limiting as it is designed for XPath 1.0 and is not well integrated with interfaces for XSLT and XQuery),
or via Saxon's own s9api interface ("snappy"). In s9api, the steps are:</p>
      <ol>
         <li content="para">
            <p>Create a <code>Processor</code> (which can also be used for XSLT, XQuery, and XSD processing)</p>
         </li>
         <li content="para">
            <p>User the <code>newXPathCompiler()</code> method to create an <code>XPathCompiler</code>, and use its methods 
 to set the static context for the expression</p>
         </li>
         <li content="para">
            <p>User the <code>compile()</code> method to compile the expression, and the load() method to instantiate it
 for use (you can compile the method once and load it as many times as you like for execution)</p>
         </li>
         <li content="para">
            <p>Call methods on the resulting <code>XPathSelector</code> object to set the context item and the values of
 any external variables for evaluating the expression</p>
         </li>
         <li content="para">
            <p>Call another method to evaluate the expression.
 Because <code>XPathSelector</code> is a Java <code>Iterable</code>, you can simply use the Java for-each construct
 to iterate over the results of the expression.</p>
         </li>
         <li content="para">
            <p>If the results are sequences of nodes, as is often the case, they are returned as instances of the Saxon
 class <code>XdmNode</code> which provides methods to perform further processing of the results.</p>
         </li>
      </ol>
      <p>An important change in XPath 2.0 is that all values are now considered as sequences. A sequence
consists of zero or more items; an item may be a node or a simple-value. Examples of simple-values
are integers, strings, booleans, and dates. A single value such as
a number is considered as a sequence of length 1. The empty sequence is written as <code>()</code>;
a singleton sequence may be written as <code>"a"</code> or <code>("a")</code>, and a general
sequence is written as <code>("a", "b", "c")</code>.</p>
      <p>The node-sets of XPath 1.0 are replaced in XPath 2.0 by sequences of nodes. Path expressions
will return node sequences whose nodes are in document order with no duplicates, but other kinds
of expression may return sequences of nodes in any order, with duplicates permitted.</p>
      <p>This section summarizes the syntactic constructs and operators provided in XPath 2.0. The functions
provided in the function library are listed separately: see the <a class="bodylink" href="../functions/intro.xml">Functions</a> section.</p>
      <table width="100%">
         <tr>
            <td>
               <p align="right"><a class="nav" href="literals.xml">Next</a></p>
            </td>
         </tr>
      </table>
   </body>
</html>