jaxp/src/java.xml/share/classes/javax/xml/xpath/package.html
changeset 45261 8a151bf73222
parent 29999 8493f5fc1052
equal deleted inserted replaced
45151:13796d1d9f0a 45261:8a151bf73222
     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
     1 <!doctype html>
     2 <html>
     2 <html>
     3 <head>
     3 <head>
     4 <!--
     4 <!--
     5 Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     5 Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
     6 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     7 
     7 
     8 This code is free software; you can redistribute it and/or modify it
     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
     9 under the terms of the GNU General Public License version 2 only, as
    10 published by the Free Software Foundation.  Oracle designates this
    10 published by the Free Software Foundation.  Oracle designates this
    24 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    25 or visit www.oracle.com if you need additional information or have any
    26 questions. 
    26 questions. 
    27 -->
    27 -->
    28 </head>
    28 </head>
    29 <body bgcolor="white">
    29 <body>
    30 
    30 
    31 This package provides an <em>object-model neutral</em> API for the
    31 This package provides an <em>object-model neutral</em> API for the
    32 evaluation of XPath expressions and access to the evaluation
    32 evaluation of XPath expressions and access to the evaluation
    33 environment.
    33 environment.
    34 
    34 
    35 <p>
    35 <p>
    36 The XPath API supports <a href="http://www.w3.org/TR/xpath">
    36 The XPath API supports <a href="http://www.w3.org/TR/xpath">
    37     XML Path Language (XPath) Version 1.0</a>
    37     XML Path Language (XPath) Version 1.0</a>
    38 
    38 
    39 <hr />
    39 <hr>
    40 
    40 
    41 <ul>
    41 <ul>
    42     <li><a href='#XPath.Overview'>1. XPath Overview</a></li>
    42     <li><a href='#XPath.Overview'>1. XPath Overview</a></li>
    43     <li><a href='#XPath.Expressions'>2. XPath Expressions</a></li>
    43     <li><a href='#XPath.Expressions'>2. XPath Expressions</a></li>
    44     <li><a href='#XPath.Datatypes'>3. XPath Data Types</a>
    44     <li><a href='#XPath.Datatypes'>3. XPath Data Types</a>
    50     </li>
    50     </li>
    51     <li><a href='#XPath.Context'>4. XPath Context</a></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>
    52     <li><a href='#XPath.Use'>5. Using the XPath API</a></li>
    53 </ul>
    53 </ul>
    54 <p>
    54 <p>
    55 <a name="XPath.Overview"></a>
    55 <a id="XPath.Overview"></a>
    56 <h3>1. XPath Overview</h3>
    56 <h3>1. XPath Overview</h3>
    57 
    57 
    58 <p>The XPath language provides a simple, concise syntax for selecting
    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
    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,
    60 node in an XML document object model (DOM) tree to a boolean, double,
    67 XPointer languages, but has more recently become popular as a
    67 XPointer languages, but has more recently become popular as a
    68 stand-alone language, as a single XPath expression can be used to
    68 stand-alone language, as a single XPath expression can be used to
    69 replace many lines of DOM API code.
    69 replace many lines of DOM API code.
    70 </p>
    70 </p>
    71 
    71 
    72 <a name="XPath.Expressions"></a>
    72 <a id="XPath.Expressions"></a>
    73 <h3>2. XPath Expressions</h3>
    73 <h3>2. XPath Expressions</h3>
    74 
    74 
    75 <p>An XPath <em>expression</em> is composed of a <em>location
    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
    76 path</em> and one or more optional <em>predicates</em>. Expressions
    77 may also include XPath variables.
    77 may also include XPath variables.
   137 <p>In addition to element nodes, XPath location paths may also address
   137 <p>In addition to element nodes, XPath location paths may also address
   138 attribute nodes, text nodes, comment nodes, and processing instruction
   138 attribute nodes, text nodes, comment nodes, and processing instruction
   139 nodes. The following table gives examples of location paths for each
   139 nodes. The following table gives examples of location paths for each
   140 of these node types:</p>
   140 of these node types:</p>
   141 
   141 
   142 <table border="1">
   142 <table class="striped">
   143 <tr>
   143 <caption>Examples of Location Path</caption>
   144 <td>Location Path</td>
   144 <thead>
   145 <td>Description</td>
   145 <tr>
   146 </tr>
   146 <th>Location Path</th>
       
   147 <th>Description</th>
       
   148 </tr>
       
   149 </thead>
       
   150 <tbody>
   147 <tr>
   151 <tr>
   148 <td>
   152 <td>
   149 <code>/foo/bar/<strong>@id</strong></code>
   153 <code>/foo/bar/<strong>@id</strong></code>
   150 </td>
   154 </td>
   151 <td>Selects the attribute <code>id</code> of the <code>&lt;bar&gt;</code> element
   155 <td>Selects the attribute <code>id</code> of the <code>&lt;bar&gt;</code> element
   169 </td>
   173 </td>
   170 <td>Selects all processing-instruction nodes contained in the
   174 <td>Selects all processing-instruction nodes contained in the
   171 <code>&lt;bar&gt;</code> element.
   175 <code>&lt;bar&gt;</code> element.
   172 </td>
   176 </td>
   173 </tr>
   177 </tr>
       
   178 </tbody>
   174 </table>
   179 </table>
   175 
   180 
   176 <p>Predicates allow for refining the nodes selected by an XPath
   181 <p>Predicates allow for refining the nodes selected by an XPath
   177 location path. Predicates are of the form
   182 location path. Predicates are of the form
   178 <code>[<em>expression</em>]</code>. The following example selects all
   183 <code>[<em>expression</em>]</code>. The following example selects all
   192 <pre>
   197 <pre>
   193 //foo[@include='true'][@mode='bar']
   198 //foo[@include='true'][@mode='bar']
   194 </pre>
   199 </pre>
   195 </blockquote>
   200 </blockquote>
   196 
   201 
   197 <a name="XPath.Datatypes"></a>
   202 <a id="XPath.Datatypes"></a>
   198 <h3>3. XPath Data Types</h3>
   203 <h3>3. XPath Data Types</h3>
   199 
   204 
   200 <p>While XPath expressions select nodes in the XML document, the XPath
   205 <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
   206 API allows the selected nodes to be coalesced into one of the
   202 following data types:</p>
   207 following data types:</p>
   205 <li><code>Boolean</code></li>
   210 <li><code>Boolean</code></li>
   206 <li><code>Number</code></li>
   211 <li><code>Number</code></li>
   207 <li><code>String</code></li>
   212 <li><code>String</code></li>
   208 </ul>
   213 </ul>
   209 
   214 
   210 <a name="XPath.Datatypes.QName"></a>
   215 <a id="XPath.Datatypes.QName"></a>
   211 <h3>3.1 QName types</h3>
   216 <h3>3.1 QName types</h3>
   212 The XPath API defines the following {@link javax.xml.namespace.QName} types to 
   217 The XPath API defines the following {@link javax.xml.namespace.QName} types to 
   213 represent return types of an XPath evaluation:
   218 represent return types of an XPath evaluation:
   214 <ul>
   219 <ul>
   215 <li>{@link javax.xml.xpath.XPathConstants#NODESET}</li>
   220 <li>{@link javax.xml.xpath.XPathConstants#NODESET}</li>
   234 of the child text nodes is returned.
   239 of the child text nodes is returned.
   235 
   240 
   236 <p>The <code>Number</code> return type attempts to coalesce the text
   241 <p>The <code>Number</code> return type attempts to coalesce the text
   237 of a node to a <code>double</code> data type.
   242 of a node to a <code>double</code> data type.
   238 
   243 
   239 <a name="XPath.Datatypes.Class"></a>
   244 <a id="XPath.Datatypes.Class"></a>
   240 <h3>3.2 Class types</h3>
   245 <h3>3.2 Class types</h3>
   241 In addition to the QName types, the XPath API supports the use of Class types
   246 In addition to the QName types, the XPath API supports the use of Class types
   242 through the <code>XPathExpression.evaluteExpression(...)</code> or 
   247 through the <code>XPathExpression.evaluteExpression(...)</code> or 
   243 <code>XPath.evaluateExpression(...)</code> methods. 
   248 <code>XPath.evaluateExpression(...)</code> methods. 
   244 
   249 
   252 </ul>
   257 </ul>
   253 
   258 
   254 <p>
   259 <p>
   255 Of the subtypes of Number, only Double, Integer and Long are supported.
   260 Of the subtypes of Number, only Double, Integer and Long are supported.
   256          
   261          
   257 <a name="XPath.Datatypes.Enum"></a>
   262 <a id="XPath.Datatypes.Enum"></a>
   258 <h3>3.3 Enum types</h3>
   263 <h3>3.3 Enum types</h3>
   259 Enum types are defined in {@link javax.xml.xpath.XPathEvaluationResult.XPathResultType} 
   264 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 
   265 that provide mappings between the QName and Class types above. The result of 
   261 evaluating an expression using the <code>XPathExpression.evaluteExpression(...)</code> 
   266 evaluating an expression using the <code>XPathExpression.evaluteExpression(...)</code> 
   262 or <code>XPath.evaluateExpression(...)</code> methods will be of one of these types.
   267 or <code>XPath.evaluateExpression(...)</code> methods will be of one of these types.
   263 
   268 
   264 <a name="XPath.Context"></a>
   269 <a id="XPath.Context"></a>
   265 <h3>4. XPath Context</h3>
   270 <h3>4. XPath Context</h3>
   266 
   271 
   267 <p>XPath location paths may be relative to a particular node in the
   272 <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:
   273 document, known as the <code>context</code>. A context consists of:
   269 <ul>
   274 <ul>
   276 
   281 
   277 <p>
   282 <p>
   278 It is an XML document tree represented as a hierarchy of nodes, a 
   283 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.
   284 {@link org.w3c.dom.Node} for example, in the JDK implementation.
   280 
   285 
   281 <a name="XPath.Use"></a>
   286 <a id="XPath.Use"></a>
   282 <h3>5. Using the XPath API</h3>
   287 <h3>5. Using the XPath API</h3>
   283 
   288 
   284 Consider the following XML document:
   289 Consider the following XML document:
   285 <blockquote>
   290 <blockquote>
   286 <pre>
   291 <pre>