CINXE.COM
javax.xml.xpath (Java SE 10 & JDK 10 )
<!DOCTYPE HTML> <!-- NewPage --> <html lang="en"> <head> <!-- Generated by javadoc --> <title>javax.xml.xpath (Java SE 10 & JDK 10 )</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="keywords" content="javax.xml.xpath package"> <link rel="stylesheet" type="text/css" href="../../../stylesheet.css" title="Style"> <link rel="stylesheet" type="text/css" href="../../../jquery/jquery-ui.css" title="Style"> <script type="text/javascript" src="../../../script.js"></script> <script type="text/javascript" src="../../../jquery/jszip/dist/jszip.min.js"></script> <script type="text/javascript" src="../../../jquery/jszip-utils/dist/jszip-utils.min.js"></script> <!--[if IE]> <script type="text/javascript" src="../../../jquery/jszip-utils/dist/jszip-utils-ie.min.js"></script> <![endif]--> <script type="text/javascript" src="../../../jquery/jquery-1.10.2.js"></script> <script type="text/javascript" src="../../../jquery/jquery-ui.js"></script> <script>window.ohcglobal || document.write('<script src="/en/dcommon/js/global.js">\x3C/script>')</script></head> <body> <script type="text/javascript"><!-- try { if (location.href.indexOf('is-external=true') == -1) { parent.document.title="javax.xml.xpath (Java SE 10 & JDK 10 )"; } } catch(err) { } //--> var pathtoroot = "../../../";loadScripts(document, 'script');</script> <noscript> <div>JavaScript is disabled on your browser.</div> </noscript> <header role="banner"> <nav role="navigation"> <div class="fixedNav"> <!-- ========= START OF TOP NAVBAR ======= --> <div class="topNav"><a id="navbar.top"> <!-- --> </a> <div class="skipNav"><a href="#skip.navbar.top" title="Skip navigation links">Skip navigation links</a></div> <a id="navbar.top.firstrow"> <!-- --> </a> <ul class="navList" title="Navigation"> <li><a href="../../../overview-summary.html">Overview</a></li> <li><a href="../../../java.xml-summary.html">Module</a></li> <li class="navBarCell1Rev">Package</li> <li>Class</li> <li><a href="package-use.html">Use</a></li> <li><a href="package-tree.html">Tree</a></li> <li><a href="../../../deprecated-list.html">Deprecated</a></li> <li><a href="../../../index-files/index-1.html">Index</a></li> <li><a href="../../../help-doc.html">Help</a></li> </ul> <div class="aboutLanguage"><div style="margin-top: 14px;"><strong>Java SE 10 & JDK 10</strong> </div></div> </div> <div class="subNav"> <ul class="navList"> <li><a href="../../../javax/xml/ws/wsaddressing/package-summary.html">Prev Package</a></li> <li><a href="../../../jdk/dynalink/package-summary.html">Next Package</a></li> </ul> <ul class="navList"> <li><a href="../../../index.html?javax/xml/xpath/package-summary.html" target="_top">Frames</a></li> <li><a href="package-summary.html" target="_top">No Frames</a></li> </ul> <ul class="navList" id="allclasses_navbar_top"> <li><a href="../../../allclasses-noframe.html">All Classes</a></li> </ul> <ul class="navListSearch"> <li><label for="search">SEARCH:</label> <input type="text" id="search" value="search" disabled="disabled"> <input type="reset" id="reset" value="reset" disabled="disabled"> </li> </ul> <div> <script type="text/javascript"><!-- allClassesLink = document.getElementById("allclasses_navbar_top"); if(window==top) { allClassesLink.style.display = "block"; } else { allClassesLink.style.display = "none"; } //--> </script> <noscript> <div>JavaScript is disabled on your browser.</div> </noscript> </div> <a id="skip.navbar.top"> <!-- --> </a></div> <!-- ========= END OF TOP NAVBAR ========= --> </div> <div class="navPadding"> </div> <script type="text/javascript"><!-- $('.navPadding').css('padding-top', $('.fixedNav').css("height")); //--> </script> </nav> </header> <main role="main"> <div class="header"> <div class="subTitle"><span class="moduleLabelInPackage">Module</span> <a href="../../../java.xml-summary.html">java.xml</a></div> <h1 title="Package" class="title">Package javax.xml.xpath</h1> </div> <div class="contentContainer"> <section role="region"><a id="package.description"> <!-- --> </a> <div class="block">Provides an <em>object-model neutral</em> API for the evaluation of XPath expressions and access to the evaluation environment. <p> The XPath API supports <a href="http://www.w3.org/TR/xpath"> XML Path Language (XPath) Version 1.0</a> <hr> <ul> <li><a href='#XPath.Overview'>1. XPath Overview</a></li> <li><a href='#XPath.Expressions'>2. XPath Expressions</a></li> <li><a href='#XPath.Datatypes'>3. XPath Data Types</a> <ul> <li><a href='#XPath.Datatypes.QName'>3.1 QName Types</a> <li><a href='#XPath.Datatypes.Class'>3.2 Class Types</a> <li><a href='#XPath.Datatypes.Enum'>3.3 Enum Types</a> </ul> </li> <li><a href='#XPath.Context'>4. XPath Context</a></li> <li><a href='#XPath.Use'>5. Using the XPath API</a></li> </ul> <p> <a id="XPath.Overview"></a> <h3>1. XPath Overview</h3> <p> The XPath language provides a simple, concise syntax for selecting nodes from an XML document. XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification. <p> XPath started in life in 1999 as a supplement to the XSLT and XPointer languages, but has more recently become popular as a stand-alone language, as a single XPath expression can be used to replace many lines of DOM API code. <a id="XPath.Expressions"></a> <h3>2. XPath Expressions</h3> <p> An XPath <em>expression</em> is composed of a <em>location path</em> and one or more optional <em>predicates</em>. Expressions may also include XPath variables. <p> The following is an example of a simple XPath expression: <blockquote> <pre> /foo/bar </pre> </blockquote> <p> This example would select the <code><bar></code> element in an XML document such as the following: <blockquote> <pre> <foo> <bar/> </foo> </pre> </blockquote> <p>The expression <code>/foo/bar</code> is an example of a location path. While XPath location paths resemble Unix-style file system paths, an important distinction is that XPath expressions return <em>all</em> nodes that match the expression. Thus, all three <code><bar></code> elements in the following document would be selected by the <code>/foo/bar</code> expression: <blockquote> <pre> <foo> <bar/> <bar/> <bar/> </foo> </pre> </blockquote> <p> A special location path operator, <code>//</code>, selects nodes at any depth in an XML document. The following example selects all <code><bar></code> elements regardless of their location in a document: <blockquote> <pre> //bar </pre> </blockquote> <p> A wildcard operator, *, causes all element nodes to be selected. The following example selects all children elements of a <code><foo></code> element: <blockquote> <pre> /foo/* </pre> </blockquote> <p> In addition to element nodes, XPath location paths may also address attribute nodes, text nodes, comment nodes, and processing instruction nodes. The following table gives examples of location paths for each of these node types: <table class="striped"> <caption>Examples of Location Path</caption> <thead> <tr> <th scope="col">Location Path</th> <th scope="col">Description</th> </tr> </thead> <tbody> <tr> <th scope="row"> <code>/foo/bar/<strong>@id</strong></code> </th> <td> Selects the attribute <code>id</code> of the <code><bar></code> element </td> </tr> <tr> <th scope="row"><code>/foo/bar/<strong>text()</strong></code> </th> <td> Selects the text nodes of the <code><bar></code> element. No distinction is made between escaped and non-escaped character data. </td> </tr> <tr> <th scope="row"><code>/foo/bar/<strong>comment()</strong></code> </th> <td> Selects all comment nodes contained in the <code><bar></code> element. </td> </tr> <tr> <th scope="row"><code>/foo/bar/<strong>processing-instruction()</strong></code> </th> <td> Selects all processing-instruction nodes contained in the <code><bar></code> element. </td> </tr> </tbody> </table> <p> Predicates allow for refining the nodes selected by an XPath location path. Predicates are of the form <code>[<em>expression</em>]</code>. The following example selects all <code><foo></code> elements that contain an <code>include</code> attribute with the value of <code>true</code>: <blockquote> <pre> //foo[@include='true'] </pre> </blockquote> <p> Predicates may be appended to each other to further refine an expression, such as: <blockquote> <pre> //foo[@include='true'][@mode='bar'] </pre> </blockquote> <a id="XPath.Datatypes"></a> <h3>3. XPath Data Types</h3> <p> While XPath expressions select nodes in the XML document, the XPath API allows the selected nodes to be coalesced into one of the following data types: <ul> <li><code>Boolean</code></li> <li><code>Number</code></li> <li><code>String</code></li> </ul> <a id="XPath.Datatypes.QName"></a> <h3>3.1 QName types</h3> The XPath API defines the following <a href="../../../javax/xml/namespace/QName.html" title="class in javax.xml.namespace"><code>QName</code></a> types to represent return types of an XPath evaluation: <ul> <li><a href="../../../javax/xml/xpath/XPathConstants.html#NODESET"><code>XPathConstants.NODESET</code></a></li> <li><a href="../../../javax/xml/xpath/XPathConstants.html#NODE"><code>XPathConstants.NODE</code></a></li> <li><a href="../../../javax/xml/xpath/XPathConstants.html#STRING"><code>XPathConstants.STRING</code></a></li> <li><a href="../../../javax/xml/xpath/XPathConstants.html#BOOLEAN"><code>XPathConstants.BOOLEAN</code></a></li> <li><a href="../../../javax/xml/xpath/XPathConstants.html#NUMBER"><code>XPathConstants.NUMBER</code></a></li> </ul> <p> The return type is specified by a <a href="../../../javax/xml/namespace/QName.html" title="class in javax.xml.namespace"><code>QName</code></a> parameter in method call used to evaluate the expression, which is either a call to <code>XPathExpression.evalute(...)</code> or <code>XPath.evaluate(...)</code> methods. <p> When a <code>Boolean</code> return type is requested, <code>Boolean.TRUE</code> is returned if one or more nodes were selected; otherwise, <code>Boolean.FALSE</code> is returned. <p> The <code>String</code> return type is a convenience for retrieving the character data from a text node, attribute node, comment node, or processing-instruction node. When used on an element node, the value of the child text nodes is returned. <p> The <code>Number</code> return type attempts to coalesce the text of a node to a <code>double</code> data type. <a id="XPath.Datatypes.Class"></a> <h3>3.2 Class types</h3> In addition to the QName types, the XPath API supports the use of Class types through the <code>XPathExpression.evaluteExpression(...)</code> or <code>XPath.evaluateExpression(...)</code> methods. The XPath data types are mapped to Class types as follows: <ul> <li><code>Boolean</code> -- <code>Boolean.class</code></li> <li><code>Number</code> -- <code>Number.class</code></li> <li><code>String</code> -- <code>String.class</code></li> <li><code>Nodeset</code> -- <code>XPathNodes.class</code></li> <li><code>Node</code> -- <code>Node.class</code></li> </ul> <p> Of the subtypes of Number, only Double, Integer and Long are supported. <a id="XPath.Datatypes.Enum"></a> <h3>3.3 Enum types</h3> Enum types are defined in <a href="../../../javax/xml/xpath/XPathEvaluationResult.XPathResultType.html" title="enum in javax.xml.xpath"><code>XPathEvaluationResult.XPathResultType</code></a> that provide mappings between the QName and Class types above. The result of evaluating an expression using the <code>XPathExpression.evaluteExpression(...)</code> or <code>XPath.evaluateExpression(...)</code> methods will be of one of these types. <a id="XPath.Context"></a> <h3>4. XPath Context</h3> <p> XPath location paths may be relative to a particular node in the document, known as the <code>context</code>. A context consists of: <ul> <li>a node (the context node)</li> <li>a pair of non-zero positive integers (the context position and the context size)</li> <li>a set of variable bindings</li> <li>a function library</li> <li>the set of namespace declarations in scope for the expression</li> </ul> <p> It is an XML document tree represented as a hierarchy of nodes, a <a href="../../../org/w3c/dom/Node.html" title="interface in org.w3c.dom"><code>Node</code></a> for example, in the JDK implementation. <a id="XPath.Use"></a> <h3>5. Using the XPath API</h3> Consider the following XML document: <blockquote> <pre> <widgets> <widget> <manufacturer/> <dimensions/> </widget> </widgets> </pre> </blockquote> <p> The <code><widget></code> element can be selected with the following process: <blockquote> <pre> // parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); //Get an XPath object and evaluate the expression XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); //or using the evaluateExpression method Node widgetNode = xpath.evaluateExpression(expression, document, Node.class); </pre> </blockquote> <p> With a reference to the <code><widget></code> element, a relative XPath expression can be written to select the <code><manufacturer></code> child element: <blockquote> <pre> XPath xpath = XPathFactory.newInstance().newXPath(); String expression = <b>"manufacturer";</b> Node manufacturerNode = (Node) xpath.evaluate(expression, <b>widgetNode</b>, XPathConstants.NODE); //or using the evaluateExpression method Node manufacturerNode = xpath.evaluateExpression(expression, <b>widgetNode</b>, Node.class); </pre> </blockquote> <p> In the above example, the XML file is read into a DOM Document before being passed to the XPath API. The following code demonstrates the use of InputSource to leave it to the XPath implementation to process it: <blockquote> <pre> XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); //or using the evaluateExpression method XPathNodes nodes = xpath.evaluate(expression, inputSource, XPathNodes.class); </pre> </blockquote> <p> In the above cases, the type of the expected results are known. In case where the result type is unknown or any type, the <a href="../../../javax/xml/xpath/XPathEvaluationResult.html" title="interface in javax.xml.xpath"><code>XPathEvaluationResult</code></a> may be used to determine the return type. The following code demonstrates the usage: <blockquote> <pre> XPathEvaluationResult<?> result = xpath.evaluateExpression(expression, document); switch (result.type()) { case NODESET: XPathNodes nodes = (XPathNodes)result.value(); ... break; } </pre> </blockquote> <p> The XPath 1.0 Number data type is defined as a double. However, the XPath specification also provides functions that returns Integer type. To facilitate such operations, the XPath API allows Integer and Long to be used in <code>evaluateExpression</code> method such as the following code: <blockquote> <pre> int count = xpath.evaluate("count(/widgets/widget)", document, Integer.class); </pre> </blockquote></div> <dl> <dt><span class="simpleTagLabel">Since:</span></dt> <dd>1.5</dd> </dl> </section> <ul class="blockList"> <li class="blockList"> <table class="typeSummary"> <caption><span>Interface Summary</span><span class="tabEnd"> </span></caption> <tr> <th class="colFirst" scope="col">Interface</th> <th class="colLast" scope="col">Description</th> </tr> <tbody> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPath.html" title="interface in javax.xml.xpath">XPath</a></th> <td class="colLast"> <div class="block"><code>XPath</code> provides access to the XPath evaluation environment and expressions.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathEvaluationResult.html" title="interface in javax.xml.xpath">XPathEvaluationResult</a><T></th> <td class="colLast"> <div class="block">The <code>XPathEvaluationResult</code> interface represents the result of the evaluation of an XPath expression within the context of a particular node.</div> </td> </tr> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathExpression.html" title="interface in javax.xml.xpath">XPathExpression</a></th> <td class="colLast"> <div class="block"><code>XPathExpression</code> provides access to compiled XPath expressions.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathFunction.html" title="interface in javax.xml.xpath">XPathFunction</a></th> <td class="colLast"> <div class="block"><code>XPathFunction</code> provides access to XPath functions.</div> </td> </tr> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathFunctionResolver.html" title="interface in javax.xml.xpath">XPathFunctionResolver</a></th> <td class="colLast"> <div class="block"><code>XPathFunctionResolver</code> provides access to the set of user defined <code>XPathFunction</code>s.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathNodes.html" title="interface in javax.xml.xpath">XPathNodes</a></th> <td class="colLast"> <div class="block">XPathNodes represents a set of nodes selected by a location path as specified in <a href="http://www.w3.org/TR/xpath/#node-sets">XML Path Language (XPath) Version 1.0, 3.3 Node-sets</a>.</div> </td> </tr> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathVariableResolver.html" title="interface in javax.xml.xpath">XPathVariableResolver</a></th> <td class="colLast"> <div class="block"><code>XPathVariableResolver</code> provides access to the set of user defined XPath variables.</div> </td> </tr> </tbody> </table> </li> <li class="blockList"> <table class="typeSummary"> <caption><span>Class Summary</span><span class="tabEnd"> </span></caption> <tr> <th class="colFirst" scope="col">Class</th> <th class="colLast" scope="col">Description</th> </tr> <tbody> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathConstants.html" title="class in javax.xml.xpath">XPathConstants</a></th> <td class="colLast"> <div class="block">XPath constants.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathFactory.html" title="class in javax.xml.xpath">XPathFactory</a></th> <td class="colLast"> <div class="block">An <code>XPathFactory</code> instance can be used to create <a href="../../../javax/xml/xpath/XPath.html" title="interface in javax.xml.xpath"><code>XPath</code></a> objects.</div> </td> </tr> </tbody> </table> </li> <li class="blockList"> <table class="typeSummary"> <caption><span>Enum Summary</span><span class="tabEnd"> </span></caption> <tr> <th class="colFirst" scope="col">Enum</th> <th class="colLast" scope="col">Description</th> </tr> <tbody> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathEvaluationResult.XPathResultType.html" title="enum in javax.xml.xpath">XPathEvaluationResult.XPathResultType</a></th> <td class="colLast"> <div class="block">XPathResultType represents possible return types of an XPath evaluation.</div> </td> </tr> </tbody> </table> </li> <li class="blockList"> <table class="typeSummary"> <caption><span>Exception Summary</span><span class="tabEnd"> </span></caption> <tr> <th class="colFirst" scope="col">Exception</th> <th class="colLast" scope="col">Description</th> </tr> <tbody> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathException.html" title="class in javax.xml.xpath">XPathException</a></th> <td class="colLast"> <div class="block"><code>XPathException</code> represents a generic XPath exception.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathExpressionException.html" title="class in javax.xml.xpath">XPathExpressionException</a></th> <td class="colLast"> <div class="block"><code>XPathExpressionException</code> represents an error in an XPath expression.</div> </td> </tr> <tr class="altColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathFactoryConfigurationException.html" title="class in javax.xml.xpath">XPathFactoryConfigurationException</a></th> <td class="colLast"> <div class="block"><code>XPathFactoryConfigurationException</code> represents a configuration error in a <code>XPathFactory</code> environment.</div> </td> </tr> <tr class="rowColor"> <th class="colFirst" scope="row"><a href="../../../javax/xml/xpath/XPathFunctionException.html" title="class in javax.xml.xpath">XPathFunctionException</a></th> <td class="colLast"> <div class="block"><code>XPathFunctionException</code> represents an error with an XPath function.</div> </td> </tr> </tbody> </table> </li> </ul> </div> </main> <footer role="contentinfo"> <nav role="navigation"> <!-- ======= START OF BOTTOM NAVBAR ====== --> <div class="bottomNav"><a id="navbar.bottom"> <!-- --> </a> <div class="skipNav"><a href="#skip.navbar.bottom" title="Skip navigation links">Skip navigation links</a></div> <a id="navbar.bottom.firstrow"> <!-- --> </a> <ul class="navList" title="Navigation"> <li><a href="../../../overview-summary.html">Overview</a></li> <li><a href="../../../java.xml-summary.html">Module</a></li> <li class="navBarCell1Rev">Package</li> <li>Class</li> <li><a href="package-use.html">Use</a></li> <li><a href="package-tree.html">Tree</a></li> <li><a href="../../../deprecated-list.html">Deprecated</a></li> <li><a href="../../../index-files/index-1.html">Index</a></li> <li><a href="../../../help-doc.html">Help</a></li> </ul> <div class="aboutLanguage"><div style="margin-top: 14px;"><strong>Java SE 10 & JDK 10</strong> </div></div> </div> <div class="subNav"> <ul class="navList"> <li><a href="../../../javax/xml/ws/wsaddressing/package-summary.html">Prev Package</a></li> <li><a href="../../../jdk/dynalink/package-summary.html">Next Package</a></li> </ul> <ul class="navList"> <li><a href="../../../index.html?javax/xml/xpath/package-summary.html" target="_top">Frames</a></li> <li><a href="package-summary.html" target="_top">No Frames</a></li> </ul> <ul class="navList" id="allclasses_navbar_bottom"> <li><a href="../../../allclasses-noframe.html">All Classes</a></li> </ul> <div> <script type="text/javascript"><!-- allClassesLink = document.getElementById("allclasses_navbar_bottom"); if(window==top) { allClassesLink.style.display = "block"; } else { allClassesLink.style.display = "none"; } //--> </script> <noscript> <div>JavaScript is disabled on your browser.</div> </noscript> </div> <a id="skip.navbar.bottom"> <!-- --> </a></div> <!-- ======== END OF BOTTOM NAVBAR ======= --> </nav> <p class="legalCopy"><small><a href="http://bugreport.java.com/bugreport/">Report a bug or suggest an enhancement</a><br> For further API reference and developer documentation see the <a href="http://www.oracle.com/pls/topic/lookup?ctx=javase10&id=homepage" target="_blank">Java SE Documentation</a>, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.<br> Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.<br> <a href="../../../../legal/copyright.html">Copyright</a> © 1993, 2018, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.<br>All rights reserved. Use is subject to <a href="http://www.oracle.com/technetwork/java/javase/terms/license/java10speclicense.html">license terms</a> and the <a href="http://www.oracle.com/technetwork/java/redist-137594.html">documentation redistribution policy</a>. <!-- Version 10+46 --></small></p> </footer> <!-- Start SiteCatalyst code --> <script type="application/javascript" src="https://www.oracleimg.com/us/assets/metrics/ora_docs.js"></script> <!-- End SiteCatalyst code --> <noscript> <p>Scripting on this page tracks web page traffic, but does not change the content in any way.</p> </noscript> </body> </html>