Path: blob/master/src/java.xml/share/classes/javax/xml/xpath/package-info.java
40948 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26*27* Provides an <em>object-model neutral</em> API for the28* evaluation of XPath expressions and access to the evaluation29* environment.30*31* <p>32* The XPath API supports <a href="http://www.w3.org/TR/xpath">33* XML Path Language (XPath) Version 1.0</a>34*35* <hr>36*37* <ul>38* <li><a href='#XPath.Overview'>1. XPath Overview</a></li>39* <li><a href='#XPath.Expressions'>2. XPath Expressions</a></li>40* <li><a href='#XPath.Datatypes'>3. XPath Data Types</a>41* <ul>42* <li><a href='#XPath.Datatypes.QName'>3.1 QName Types</a>43* <li><a href='#XPath.Datatypes.Class'>3.2 Class Types</a>44* <li><a href='#XPath.Datatypes.Enum'>3.3 Enum Types</a>45* </ul>46* </li>47* <li><a href='#XPath.Context'>4. XPath Context</a></li>48* <li><a href='#XPath.Use'>5. Using the XPath API</a></li>49* </ul>50* <p>51* <a id="XPath.Overview"></a>52* <h2>1. XPath Overview</h2>53*54* <p>55* The XPath language provides a simple, concise syntax for selecting56* nodes from an XML document. XPath also provides rules for converting a57* node in an XML document object model (DOM) tree to a boolean, double,58* or string value. XPath is a W3C-defined language and an official W3C59* recommendation; the W3C hosts the XML Path Language (XPath) Version60* 1.0 specification.61*62*63* <p>64* XPath started in life in 1999 as a supplement to the XSLT and65* XPointer languages, but has more recently become popular as a66* stand-alone language, as a single XPath expression can be used to67* replace many lines of DOM API code.68*69*70* <a id="XPath.Expressions"></a>71* <h2>2. XPath Expressions</h2>72*73* <p>74* An XPath <em>expression</em> is composed of a <em>location75* path</em> and one or more optional <em>predicates</em>. Expressions76* may also include XPath variables.77*78*79* <p>80* The following is an example of a simple XPath expression:81*82* <blockquote>83* <pre>84* /foo/bar85* </pre>86* </blockquote>87*88* <p>89* This example would select the {@code <bar>} element in90* an XML document such as the following:91*92* <blockquote>93* <pre>94* <foo>95* <bar/>96* </foo>97* </pre>98* </blockquote>99*100* <p>The expression {@code /foo/bar} is an example of a location101* path. While XPath location paths resemble Unix-style file system102* paths, an important distinction is that XPath expressions return103* <em>all</em> nodes that match the expression. Thus, all three104* {@code <bar>} elements in the following document would be105* selected by the {@code /foo/bar} expression:106*107* <blockquote>108* <pre>109* <foo>110* <bar/>111* <bar/>112* <bar/>113* </foo>114* </pre>115* </blockquote>116*117* <p>118* A special location path operator, {@code //}, selects nodes at119* any depth in an XML document. The following example selects all120* {@code <bar>} elements regardless of their location in a121* document:122*123* <blockquote>124* <pre>125* //bar126* </pre>127* </blockquote>128*129* <p>130* A wildcard operator, *, causes all element nodes to be selected.131* The following example selects all children elements of a132* {@code <foo>} element:133*134* <blockquote>135* <pre>136* /foo/*137* </pre>138* </blockquote>139*140* <p>141* In addition to element nodes, XPath location paths may also address142* attribute nodes, text nodes, comment nodes, and processing instruction143* nodes. The following table gives examples of location paths for each144* of these node types:145*146* <table class="striped">147* <caption>Examples of Location Path</caption>148* <thead>149* <tr>150* <th scope="col">Location Path</th>151* <th scope="col">Description</th>152* </tr>153* </thead>154* <tbody>155* <tr>156* <th scope="row">157* <code>/foo/bar/<strong>@id</strong></code>158* </th>159* <td>160* Selects the attribute {@code id} of the {@code <bar>} element161* </td>162* </tr>163* <tr>164* <th scope="row"><code>/foo/bar/<strong>text()</strong></code>165* </th>166* <td>167* Selects the text nodes of the {@code <bar>} element. No168* distinction is made between escaped and non-escaped character data.169* </td>170* </tr>171* <tr>172* <th scope="row"><code>/foo/bar/<strong>comment()</strong></code>173* </th>174* <td>175* Selects all comment nodes contained in the {@code <bar>} element.176* </td>177* </tr>178* <tr>179* <th scope="row"><code>/foo/bar/<strong>processing-instruction()</strong></code>180* </th>181* <td>182* Selects all processing-instruction nodes contained in the183* {@code <bar>} element.184* </td>185* </tr>186* </tbody>187* </table>188*189* <p>190* Predicates allow for refining the nodes selected by an XPath191* location path. Predicates are of the form192* <code>[<em>expression</em>]</code>. The following example selects all193* {@code <foo>} elements that contain an {@code include}194* attribute with the value of {@code true}:195*196* <blockquote>197* <pre>198* //foo[@include='true']199* </pre>200* </blockquote>201*202* <p>203* Predicates may be appended to each other to further refine an204* expression, such as:205*206* <blockquote>207* <pre>208* //foo[@include='true'][@mode='bar']209* </pre>210* </blockquote>211*212* <a id="XPath.Datatypes"></a>213* <h2>3. XPath Data Types</h2>214*215* <p>216* While XPath expressions select nodes in the XML document, the XPath217* API allows the selected nodes to be coalesced into one of the218* following data types:219*220* <ul>221* <li>{@code Boolean}</li>222* <li>{@code Number}</li>223* <li>{@code String}</li>224* </ul>225*226* <a id="XPath.Datatypes.QName"></a>227* <h2>3.1 QName types</h2>228* The XPath API defines the following {@link javax.xml.namespace.QName} types to229* represent return types of an XPath evaluation:230* <ul>231* <li>{@link javax.xml.xpath.XPathConstants#NODESET}</li>232* <li>{@link javax.xml.xpath.XPathConstants#NODE}</li>233* <li>{@link javax.xml.xpath.XPathConstants#STRING}</li>234* <li>{@link javax.xml.xpath.XPathConstants#BOOLEAN}</li>235* <li>{@link javax.xml.xpath.XPathConstants#NUMBER}</li>236* </ul>237*238* <p>239* The return type is specified by a {@link javax.xml.namespace.QName} parameter240* in method call used to evaluate the expression, which is either a call to241* {@code XPathExpression.evalute(...)} or {@code XPath.evaluate(...)}242* methods.243*244* <p>245* When a {@code Boolean} return type is requested,246* {@code Boolean.TRUE} is returned if one or more nodes were247* selected; otherwise, {@code Boolean.FALSE} is returned.248*249* <p>250* The {@code String} return type is a convenience for retrieving251* the character data from a text node, attribute node, comment node, or252* processing-instruction node. When used on an element node, the value253* of the child text nodes is returned.254*255* <p>256* The {@code Number} return type attempts to coalesce the text257* of a node to a {@code double} data type.258*259* <a id="XPath.Datatypes.Class"></a>260* <h2>3.2 Class types</h2>261* In addition to the QName types, the XPath API supports the use of Class types262* through the {@code XPathExpression.evaluateExpression(...)} or263* {@code XPath.evaluateExpression(...)} methods.264*265* The XPath data types are mapped to Class types as follows:266* <ul>267* <li>{@code Boolean} -- {@code Boolean.class}</li>268* <li>{@code Number} -- {@code Number.class}</li>269* <li>{@code String} -- {@code String.class}</li>270* <li>{@code Nodeset} -- {@code XPathNodes.class}</li>271* <li>{@code Node} -- {@code Node.class}</li>272* </ul>273*274* <p>275* Of the subtypes of {@code Number}, only {@code Double, Integer} and {@code Long} are supported.276*277* <a id="XPath.Datatypes.Enum"></a>278* <h2>3.3 Enum types</h2>279* Enum types are defined in {@link javax.xml.xpath.XPathEvaluationResult.XPathResultType}280* that provide mappings between the QName and Class types above. The result of281* evaluating an expression using the {@code XPathExpression.evaluateExpression(...)}282* or {@code XPath.evaluateExpression(...)} methods will be of one of these types.283* <p>284* Note the differences between the Enum and <a href="#XPath.Datatypes.QName">QName</a>285* mappings:286* <ul>287* <li>{@link javax.xml.xpath.XPathConstants#NUMBER NUMBER}<br>288* The Enum mapping for {@link javax.xml.xpath.XPathConstants#NUMBER NUMBER}289* supports {@code Double, Integer} and {@code Long}.<br><br>290* </li>291* <li>{@link javax.xml.xpath.XPathConstants#NODESET NODESET}<br>292* The Enum mapping for {@link javax.xml.xpath.XPathConstants#NODESET NODESET}293* is {@link javax.xml.xpath.XPathNodes XPathNodes} instead of294* {@link org.w3c.dom.NodeList NodeList} in the295* <a href="#XPath.Datatypes.QName">QName</a> mapping.296* </li>297* </ul>298*299* <a id="XPath.Context"></a>300* <h2>4. XPath Context</h2>301*302* <p>303* XPath location paths may be relative to a particular node in the304* document, known as the {@code context}. A context consists of:305* <ul>306* <li>a node (the context node)</li>307* <li>a pair of non-zero positive integers (the context position and the context size)</li>308* <li>a set of variable bindings</li>309* <li>a function library</li>310* <li>the set of namespace declarations in scope for the expression</li>311* </ul>312*313* <p>314* It is an XML document tree represented as a hierarchy of nodes, a315* {@link org.w3c.dom.Node} for example, in the JDK implementation.316*317* <a id="XPath.Use"></a>318* <h2>5. Using the XPath API</h2>319*320* Consider the following XML document:321* <blockquote>322* <pre>323* <widgets>324* <widget>325* <manufacturer/>326* <dimensions/>327* </widget>328* </widgets>329* </pre>330* </blockquote>331*332* <p>333* The {@code <widget>} element can be selected with the following process:334*335* <blockquote>336* <pre>337* // parse the XML as a W3C Document338* DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();339* Document document = builder.parse(new File("/widgets.xml"));340*341* //Get an XPath object and evaluate the expression342* XPath xpath = XPathFactory.newInstance().newXPath();343* String expression = "/widgets/widget";344* Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);345*346* //or using the evaluateExpression method347* Node widgetNode = xpath.evaluateExpression(expression, document, Node.class);348* </pre>349* </blockquote>350*351* <p>352* With a reference to the {@code <widget>} element, a353* relative XPath expression can be written to select the354* {@code <manufacturer>} child element:355*356* <blockquote>357* <pre>358* XPath xpath = XPathFactory.newInstance().newXPath();359* String expression = <b>"manufacturer";</b>360* Node manufacturerNode = (Node) xpath.evaluate(expression, <b>widgetNode</b>, XPathConstants.NODE);361*362* //or using the evaluateExpression method363* Node manufacturerNode = xpath.evaluateExpression(expression, <b>widgetNode</b>, Node.class);364* </pre>365* </blockquote>366*367* <p>368* In the above example, the XML file is read into a DOM Document before being passed369* to the XPath API. The following code demonstrates the use of InputSource to370* leave it to the XPath implementation to process it:371*372* <blockquote>373* <pre>374* XPath xpath = XPathFactory.newInstance().newXPath();375* String expression = "/widgets/widget";376* InputSource inputSource = new InputSource("widgets.xml");377* NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);378*379* //or using the evaluateExpression method380* XPathNodes nodes = xpath.evaluateExpression(expression, inputSource, XPathNodes.class);381* </pre>382* </blockquote>383*384* <p>385* In the above cases, the type of the expected results are known. In case where386* the result type is unknown or any type, the {@link javax.xml.xpath.XPathEvaluationResult}387* may be used to determine the return type. The following code demonstrates the usage:388* <blockquote>389* <pre>390* XPathEvaluationResult<?> result = xpath.evaluateExpression(expression, document);391* switch (result.type()) {392* case NODESET:393* XPathNodes nodes = (XPathNodes)result.value();394* ...395* break;396* }397* </pre>398* </blockquote>399*400* <p>401* The XPath 1.0 Number data type is defined as a double. However, the XPath402* specification also provides functions that returns Integer type. To facilitate403* such operations, the XPath API allows Integer and Long to be used in404* {@code evaluateExpression} method such as the following code:405* <blockquote>406* <pre>407* int count = xpath.evaluateExpression("count(/widgets/widget)", document, Integer.class);408* </pre>409* </blockquote>410*411* @since 1.5412*413*/414415package javax.xml.xpath;416417418