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> 143 </td> 144 <td>Selects the text nodes of the <code><bar></code> element. No 145 distinction is made between escaped and non-escaped character data. 146 </td> 147 </tr> 148 <tr> 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>