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