1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
   2 <html>
   3 <head>
   4 <!--
   5 Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   6 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7 
   8 This code is free software; you can redistribute it and/or modify it
   9 under the terms of the GNU General Public License version 2 only, as
  10 published by the Free Software Foundation.  Oracle designates this
  11 particular file as subject to the "Classpath" exception as provided
  12 by Oracle in the LICENSE file that accompanied this code.
  13 
  14 This code is distributed in the hope that it will be useful, but WITHOUT
  15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17 version 2 for more details (a copy is included in the LICENSE file that
  18 accompanied this code).
  19 
  20 You should have received a copy of the GNU General Public License version
  21 2 along with this work; if not, write to the Free Software Foundation,
  22 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  23 
  24 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  25 or visit www.oracle.com if you need additional information or have any
  26 questions. 
  27 -->
  28 </head>
  29 <body bgcolor="white">
  30 
  31 This package provides an <em>object-model neutral</em> API for the
  32 evaluation of XPath expressions and access to the evaluation
  33 environment.
  34 
  35 <p>
  36 The XPath API supports <a href="http://www.w3.org/TR/xpath">
  37     XML Path Language (XPath) Version 1.0</a>
  38 
  39 <hr />
  40 
  41 <ul>
  42     <li><a href='#XPath.Overview'>1. XPath Overview</a></li>
  43     <li><a href='#XPath.Expressions'>2. XPath Expressions</a></li>
  44     <li><a href='#XPath.Datatypes'>3. XPath Data Types</a>
  45         <ul>
  46             <li><a href='#XPath.Datatypes.QName'>3.1 QName Types</a>
  47             <li><a href='#XPath.Datatypes.Class'>3.2 Class Types</a>
  48             <li><a href='#XPath.Datatypes.Enum'>3.3 Enum Types</a>
  49         </ul>    
  50     </li>
  51     <li><a href='#XPath.Context'>4. XPath Context</a></li>
  52     <li><a href='#XPath.Use'>5. Using the XPath API</a></li>
  53 </ul>
  54 <p>
  55 <a name="XPath.Overview"></a>
  56 <h3>1. XPath Overview</h3>
  57 
  58 <p>The XPath language provides a simple, concise syntax for selecting
  59 nodes from an XML document. XPath also provides rules for converting a
  60 node in an XML document object model (DOM) tree to a boolean, double,
  61 or string value. XPath is a W3C-defined language and an official W3C
  62 recommendation; the W3C hosts the XML Path Language (XPath) Version
  63 1.0 specification.
  64 </p>
  65 
  66 <p>XPath started in life in 1999 as a supplement to the XSLT and
  67 XPointer languages, but has more recently become popular as a
  68 stand-alone language, as a single XPath expression can be used to
  69 replace many lines of DOM API code.
  70 </p>
  71 
  72 <a name="XPath.Expressions"></a>
  73 <h3>2. XPath Expressions</h3>
  74 
  75 <p>An XPath <em>expression</em> is composed of a <em>location
  76 path</em> and one or more optional <em>predicates</em>. Expressions
  77 may also include XPath variables.
  78 </p>
  79 
  80 <p>The following is an example of a simple XPath expression:</p>
  81 
  82 <blockquote>
  83 <pre>
  84 /foo/bar
  85 </pre>
  86 </blockquote>
  87 
  88 <p>This example would select the <code>&lt;bar&gt;</code> element in
  89 an XML document such as the following:</p>
  90 
  91 <blockquote>
  92 <pre>
  93 &lt;foo&gt;
  94     &lt;bar/&gt;
  95 &lt;/foo&gt;
  96 </pre>
  97 </blockquote>
  98 
  99 <p>The expression <code>/foo/bar</code> is an example of a location
 100 path. While XPath location paths resemble Unix-style file system
 101 paths, an important distinction is that XPath expressions return
 102 <em>all</em> nodes that match the expression. Thus, all three
 103 <code>&lt;bar&gt;</code> elements in the following document would be
 104 selected by the <code>/foo/bar</code> expression:</p>
 105 
 106 <blockquote>
 107 <pre>
 108 &lt;foo&gt;
 109     &lt;bar/&gt;
 110     &lt;bar/&gt;
 111     &lt;bar/&gt;
 112 &lt;/foo&gt;
 113 </pre>
 114 </blockquote>
 115 
 116 <p>A special location path operator, <code>//</code>, selects nodes at
 117 any depth in an XML document. The following example selects all
 118 <code>&lt;bar&gt;</code> elements regardless of their location in a
 119 document:</p>
 120 
 121 <blockquote>
 122 <pre>
 123 //bar
 124 </pre>
 125 </blockquote>
 126 
 127 <p>A wildcard operator, *, causes all element nodes to be selected.
 128 The following example selects all children elements of a
 129 <code>&lt;foo&gt;</code> element:
 130 
 131 <blockquote>
 132 <pre>
 133 /foo/*
 134 </pre>
 135 </blockquote>
 136 
 137 <p>In addition to element nodes, XPath location paths may also address
 138 attribute nodes, text nodes, comment nodes, and processing instruction
 139 nodes. The following table gives examples of location paths for each
 140 of these node types:</p>
 141 
 142 <table border="1">
 143 <tr>
 144 <td>Location Path</td>
 145 <td>Description</td>
 146 </tr>
 147 <tr>
 148 <td>
 149 <code>/foo/bar/<strong>@id</strong></code>
 150 </td>
 151 <td>Selects the attribute <code>id</code> of the <code>&lt;bar&gt;</code> element
 152 </td>
 153 </tr>
 154 <tr>
 155 <td><code>/foo/bar/<strong>text()</strong></code>
 156 </td>
 157 <td>Selects the text nodes of the <code>&lt;bar&gt;</code> element. No
 158 distinction is made between escaped and non-escaped character data.
 159 </td>
 160 </tr>
 161 <tr>
 162 <td><code>/foo/bar/<strong>comment()</strong></code>
 163 </td>
 164 <td>Selects all comment nodes contained in the <code>&lt;bar&gt;</code> element.
 165 </td>
 166 </tr>
 167 <tr>
 168 <td><code>/foo/bar/<strong>processing-instruction()</strong></code>
 169 </td>
 170 <td>Selects all processing-instruction nodes contained in the
 171 <code>&lt;bar&gt;</code> element.
 172 </td>
 173 </tr>
 174 </table>
 175 
 176 <p>Predicates allow for refining the nodes selected by an XPath
 177 location path. Predicates are of the form
 178 <code>[<em>expression</em>]</code>. The following example selects all
 179 <code>&lt;foo&gt;</code> elements that contain an <code>include</code>
 180 attribute with the value of <code>true</code>:</p>
 181 
 182 <blockquote>
 183 <pre>
 184 //foo[@include='true']
 185 </pre>
 186 </blockquote>
 187 
 188 <p>Predicates may be appended to each other to further refine an
 189 expression, such as:</p>
 190 
 191 <blockquote>
 192 <pre>
 193 //foo[@include='true'][@mode='bar']
 194 </pre>
 195 </blockquote>
 196 
 197 <a name="XPath.Datatypes"></a>
 198 <h3>3. XPath Data Types</h3>
 199 
 200 <p>While XPath expressions select nodes in the XML document, the XPath
 201 API allows the selected nodes to be coalesced into one of the
 202 following data types:</p>
 203 
 204 <ul>
 205 <li><code>Boolean</code></li>
 206 <li><code>Number</code></li>
 207 <li><code>String</code></li>
 208 </ul>
 209 
 210 <a name="XPath.Datatypes.QName"></a>
 211 <h3>3.1 QName types</h3>
 212 The XPath API defines the following {@link javax.xml.namespace.QName} types to 
 213 represent return types of an XPath evaluation:
 214 <ul>
 215 <li>{@link javax.xml.xpath.XPathConstants#NODESET}</li>
 216 <li>{@link javax.xml.xpath.XPathConstants#NODE}</li>
 217 <li>{@link javax.xml.xpath.XPathConstants#STRING}</li>
 218 <li>{@link javax.xml.xpath.XPathConstants#BOOLEAN}</li>
 219 <li>{@link javax.xml.xpath.XPathConstants#NUMBER}</li>
 220 </ul>
 221 
 222 <p>The return type is specified by a {@link javax.xml.namespace.QName} parameter 
 223 in method call used to evaluate the expression, which is either a call to
 224 <code>XPathExpression.evalute(...)</code> or <code>XPath.evaluate(...)</code> 
 225 methods. 
 226 
 227 <p>When a <code>Boolean</code> return type is requested,
 228 <code>Boolean.TRUE</code> is returned if one or more nodes were
 229 selected; otherwise, <code>Boolean.FALSE</code> is returned.
 230 
 231 <p>The <code>String</code> return type is a convenience for retrieving
 232 the character data from a text node, attribute node, comment node, or
 233 processing-instruction node. When used on an element node, the value
 234 of the child text nodes is returned.
 235 
 236 <p>The <code>Number</code> return type attempts to coalesce the text
 237 of a node to a <code>double</code> data type.
 238 
 239 <a name="XPath.Datatypes.Class"></a>
 240 <h3>3.2 Class types</h3>
 241 In addition to the QName types, the XPath API supports the use of Class types
 242 through the <code>XPathExpression.evaluteExpression(...)</code> or 
 243 <code>XPath.evaluateExpression(...)</code> methods. 
 244 
 245 The XPath data types are mapped to Class types as follows:
 246 <ul>
 247 <li><code>Boolean</code> -- <code>Boolean.class</code></li>
 248 <li><code>Number</code> -- <code>Number.class</code></li>
 249 <li><code>String</code> -- <code>String.class</code></li>
 250 <li><code>Nodeset</code> -- <code>XPathNodes.class</code></li>
 251 <li><code>Node</code> -- <code>Node.class</code></li>
 252 </ul>
 253 
 254 <p>
 255 Of the subtypes of Number, only Double, Integer and Long are supported.
 256          
 257 <a name="XPath.Datatypes.Enum"></a>
 258 <h3>3.3 Enum types</h3>
 259 Enum types are defined in {@link javax.xml.xpath.XPathEvaluationResult.XPathResultType} 
 260 that provide mappings between the QName and Class types above. The result of 
 261 evaluating an expression using the <code>XPathExpression.evaluteExpression(...)</code> 
 262 or <code>XPath.evaluateExpression(...)</code> methods will be of one of these types.
 263 
 264 <a name="XPath.Context"></a>
 265 <h3>4. XPath Context</h3>
 266 
 267 <p>XPath location paths may be relative to a particular node in the
 268 document, known as the <code>context</code>. A context consists of:
 269 <ul>
 270     <li>a node (the context node)</li>
 271     <li>a pair of non-zero positive integers (the context position and the context size)</li>
 272     <li>a set of variable bindings</li>
 273     <li>a function library</li>
 274     <li>the set of namespace declarations in scope for the expression</li>    
 275 </ul>
 276 
 277 <p>
 278 It is an XML document tree represented as a hierarchy of nodes, a 
 279 {@link org.w3c.dom.Node} for example, in the JDK implementation.
 280 
 281 <a name="XPath.Use"></a>
 282 <h3>5. Using the XPath API</h3>
 283 
 284 Consider the following XML document:
 285 <blockquote>
 286 <pre>
 287 &lt;widgets&gt;
 288 &lt;widget&gt;
 289 &lt;manufacturer/&gt;
 290 &lt;dimensions/&gt;
 291 &lt;/widget&gt;
 292 &lt;/widgets&gt;
 293 </pre>
 294 </blockquote>
 295 
 296 <p>
 297 The <code>&lt;widget&gt;</code> element can be selected with the following process:
 298 
 299 <blockquote>
 300 <pre>
 301 // parse the XML as a W3C Document
 302 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 303 Document document = builder.parse(new File("/widgets.xml"));
 304 
 305 //Get an XPath object and evaluate the expression
 306 XPath xpath = XPathFactory.newInstance().newXPath();
 307 String expression = "/widgets/widget";
 308 Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
 309 
 310 //or using the evaluateExpression method
 311 Node widgetNode = xpath.evaluateExpression(expression, document, Node.class);
 312 </pre>
 313 </blockquote>
 314 
 315 <p>With a reference to the <code>&lt;widget&gt;</code> element, a
 316 relative XPath expression can be written to select the
 317 <code>&lt;manufacturer&gt;</code> child element:</p>
 318 
 319 <blockquote>
 320 <pre>
 321 XPath xpath = XPathFactory.newInstance().newXPath();
 322 <strong>String expression = "manufacturer";</strong>
 323 Node manufacturerNode = (Node) xpath.evaluate(expression, <strong>widgetNode</strong>, XPathConstants.NODE);
 324 
 325 //or using the evaluateExpression method
 326 Node manufacturerNode = xpath.evaluateExpression(expression, <strong>widgetNode</strong>, Node.class);
 327 </pre>
 328 </blockquote>
 329 
 330 <p>
 331 In the above example, the XML file is read into a DOM Document before being passed
 332 to the XPath API. The following code demonstrates the use of InputSource to 
 333 leave it to the XPath implementation to process it:
 334 
 335 <blockquote>
 336 <pre>
 337 XPath xpath = XPathFactory.newInstance().newXPath();
 338 String expression = "/widgets/widget";
 339 InputSource inputSource = new InputSource("widgets.xml");
 340 NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
 341 
 342 //or using the evaluateExpression method
 343 XPathNodes nodes = xpath.evaluate(expression, inputSource, XPathNodes.class);
 344 </pre>
 345 </blockquote>
 346 
 347 <p>
 348 In the above cases, the type of the expected results are known. In case where
 349 the result type is unknown or any type, the {@link javax.xml.xpath.XPathEvaluationResult}
 350 may be used to determine the return type. The following code demonstrates the usage:
 351 <blockquote>
 352 <pre>
 353 XPathEvaluationResult&lt;?&gt; result = xpath.evaluateExpression(expression, document);
 354 switch (result.type()) {
 355     case NODESET:
 356         XPathNodes nodes = (XPathNodes)result.value();
 357         ...
 358         break;
 359 }
 360 </pre>
 361 </blockquote>
 362 
 363 <p>
 364 The XPath 1.0 Number data type is defined as a double. However, the XPath 
 365 specification also provides functions that returns Integer type. To facilitate
 366 such operations, the XPath API allows Integer and Long to be used in 
 367 {@code evaluateExpression} method such as the following code: 
 368 <blockquote>
 369 <pre>
 370 int count = xpath.evaluate("count(/widgets/widget)", document, Integer.class);
 371 </pre>
 372 </blockquote>
 373 
 374 @since 1.5
 375 
 376 </body>
 377 </html>