jaxp/src/share/classes/javax/xml/xpath/package.html
author ohair
Mon, 14 Apr 2008 14:52:27 -0700
changeset 311 3c14f21bf3f7
parent 6 7f561c08de6b
permissions -rw-r--r--
6484686: The next directory looks like it is no longer part of the build (deploy makefiles) Summary: Getting rid of the _OUTPUTDIR settings. Using BUILD_PARENT_DIRECTORY instead. This solves problems with the "/build/windows-i586*" paths getting mangled on Windows builds (fastdebug builds in particular). Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
<?xml version="1.0" encoding="UTF-8"?>
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
<!--
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
This code is free software; you can redistribute it and/or modify it
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
under the terms of the GNU General Public License version 2 only, as
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
published by the Free Software Foundation.  Sun designates this
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
particular file as subject to the "Classpath" exception as provided
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
by Sun in the LICENSE file that accompanied this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
This code is distributed in the hope that it will be useful, but WITHOUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
version 2 for more details (a copy is included in the LICENSE file that
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
accompanied this code).
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
You should have received a copy of the GNU General Public License version
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
2 along with this work; if not, write to the Free Software Foundation,
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
CA 95054 USA or visit www.sun.com if you need additional information or
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
have any questions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
-->
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
<html xmlns="http://www.w3.org/1999/xhtml">
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
<head>
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
<title>javax.xml.xpath</title>
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
<meta name="@author" content="mailto:Ben@galbraiths.org" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
<meta name="@author" content="mailto:Norman.Walsh@Sun.com" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
<meta name="@author" content="mailto:Jeff.Suttor@Sun.com" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
<meta name="@version" content="$Revision: 1.3 $, $Date: 2005/11/03 19:34:17 $" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
<meta name="@see" content="http://www.w3.org/TR/xpath" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
<meta name="@since" content="1.5" />
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
</head>
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
<body>
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
<p>This package provides an <em>object-model neutral</em> API for the
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
evaluation of XPath expressions and access to the evaluation
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
environment.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
<p>The following XML standards apply:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
<ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
<li><a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
</ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
<hr />
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
<h2>XPath Overview</h2>
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
<p>The XPath language provides a simple, concise syntax for selecting
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
nodes from an XML document. XPath also provides rules for converting a
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
node in an XML document object model (DOM) tree to a boolean, double,
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
or string value. XPath is a W3C-defined language and an official W3C
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
recommendation; the W3C hosts the XML Path Language (XPath) Version
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
1.0 specification.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
<p>XPath started in life in 1999 as a supplement to the XSLT and
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
XPointer languages, but has more recently become popular as a
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
stand-alone language, as a single XPath expression can be used to
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
replace many lines of DOM API code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
<h3>XPath Expressions</h3>
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
<p>An XPath <em>expression</em> is composed of a <em>location
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
path</em> and one or more optional <em>predicates</em>. Expressions
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
may also include XPath variables.
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
<p>The following is an example of a simple XPath expression:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
/foo/bar
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
<p>This example would select the <code>&lt;bar&gt;</code> element in
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
an XML document such as the following:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
&lt;foo&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
&lt;bar/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
&lt;/foo&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
<p>The expression <code>/foo/bar</code> is an example of a location
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
path. While XPath location paths resemble Unix-style file system
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
paths, an important distinction is that XPath expressions return
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
<em>all</em> nodes that match the expression. Thus, all three
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
<code>&lt;bar&gt;</code> elements in the following document would be
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
selected by the <code>/foo/bar</code> expression:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
&lt;foo&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
&lt;bar/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
&lt;bar/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
&lt;bar/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
&lt;/foo&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
<p>A special location path operator, <code>//</code>, selects nodes at
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
any depth in an XML document. The following example selects all
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
<code>&lt;bar&gt;</code> elements regardless of their location in a
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
document:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
//bar
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
<p>A wildcard operator, *, causes all element nodes to be selected.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
The following example selects all children elements of a
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
<code>&lt;foo&gt;</code> element:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
/foo/*
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
<p>In addition to element nodes, XPath location paths may also address
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
attribute nodes, text nodes, comment nodes, and processing instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
nodes. The following table gives examples of location paths for each
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
of these node types:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
<table border="1">
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
<tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
<td>Location Path</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
<td>Description</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
</tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
<tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
<td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
<code>/foo/bar/<strong>@id</strong></code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
<td>Selects the attribute <code>id</code> of the <code>&lt;bar&gt;</code> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
</tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
<tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
<td><code>/foo/bar/<strong>text()</strong></code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
<td>Selects the text nodes of the <code>&lt;bar&gt;</code> element. No
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
distinction is made between escaped and non-escaped character data.
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
</tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
<tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
<td><code>/foo/bar/<strong>comment()</strong></code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
<td>Selects all comment nodes contained in the <code>&lt;bar&gt;</code> element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
</tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
<tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
<td><code>/foo/bar/<strong>processing-instruction()</strong></code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
<td>Selects all processing-instruction nodes contained in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
<code>&lt;bar&gt;</code> element.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
</td>
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
</tr>
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
</table>
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
<p>Predicates allow for refining the nodes selected by an XPath
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
location path. Predicates are of the form
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
<code>[<em>expression</em>]</code>. The following example selects all
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
<code>&lt;foo&gt;</code> elements that contain an <code>include</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
attribute with the value of <code>true</code>:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
//foo[@include='true']
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
<p>Predicates may be appended to each other to further refine an
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
expression, such as:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
//foo[@include='true'][@mode='bar']
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
<h3>Using the XPath API</h3>
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
<p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
The following example demonstrates using the XPath API to select one
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
or more nodes from an XML document:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
XPath xpath = XPathFactory.newInstance().newXPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
String expression = "/widgets/widget";
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
InputSource inputSource = new InputSource("widgets.xml");
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
<h3>XPath Expressions and Types</h3>
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
<p>While XPath expressions select nodes in the XML document, the XPath
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
API allows the selected nodes to be coalesced into one of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
following other data types:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
<ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
<li><code>Boolean</code></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
<li><code>Number</code></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
<li><code>String</code></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
</ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
<p>The desired return type is specified by a {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
javax.xml.namespace.QName} parameter in method call used to evaluate
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
the expression, which is either a call to
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
<code>XPathExpression.evalute(...)</code> or to one of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
<code>XPath.evaluate(...)</code> convenience methods. The allowed
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
QName values are specified as constants in the {@link
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
javax.xml.xpath.XPathConstants} class; they are:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
<ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
<li>{@link javax.xml.xpath.XPathConstants#NODESET}</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
<li>{@link javax.xml.xpath.XPathConstants#NODE}</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
<li>{@link javax.xml.xpath.XPathConstants#STRING}</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
<li>{@link javax.xml.xpath.XPathConstants#BOOLEAN}</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
<li>{@link javax.xml.xpath.XPathConstants#NUMBER}</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
</ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
<p>When a <code>Boolean</code> return type is requested,
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
<code>Boolean.TRUE</code> is returned if one or more nodes were
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
selected; otherwise, <code>Boolean.FALSE</code> is returned.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
<p>The <code>String</code> return type is a convenience for retrieving
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
the character data from a text node, attribute node, comment node, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
processing-instruction node. When used on an element node, the value
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
of the child text nodes is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
<p>The <code>Number</code> return type attempts to coalesce the text
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
of a node to a <code>double</code> data type.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
<h3>XPath Context</h3>
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
<p>XPath location paths may be relative to a particular node in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
document, known as the <code>context</code>. Consider the following
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
XML document:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
&lt;widgets&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
&lt;widget&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
&lt;manufacturer/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
&lt;dimensions/&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
&lt;/widget&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
&lt;/widgets&gt;
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
<p>The <code>&lt;widget&gt;</code> element can be selected with the
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
following XPath API code:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
// parse the XML as a W3C Document
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
Document document = builder.parse(new File("/widgets.xml"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
XPath xpath = XPathFactory.newInstance().newXPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
String expression = "/widgets/widget";
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
<p>With a reference to the <code>&lt;widget&gt;</code> element, a
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
relative XPath expression can now written to select the
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
<code>&lt;manufacturer&gt;</code> child element:</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
<pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
XPath xpath = XPathFactory.newInstance().newXPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
<strong>String expression = "manufacturer";</strong>
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
Node manufacturerNode = (Node) xpath.evaluate(expression, <strong>widgetNode</strong>, XPathConstants.NODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
</pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
<ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
<li>Author <a href="mailto:Ben@galbraiths.org">Ben Galbraith</a></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
<li>Author <a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</a></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
<li>Author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
<li>See <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a></li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
<li>Since 1.5</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
</ul>		
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
</body>
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
</html>