2796N/A<?
xml version="1.0" encoding="UTF-8"?>
2796N/ACopyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. 2796N/ADO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2796N/AThis code is free software; you can redistribute it and/or modify it 2796N/Aunder the terms of the GNU General Public License version 2 only, as 2796N/Apublished by the Free Software Foundation. Oracle designates this 2796N/Aparticular file as subject to the "Classpath" exception as provided 2796N/Aby Oracle in the LICENSE file that accompanied this code. 2796N/AThis code is distributed in the hope that it will be useful, but WITHOUT 2796N/AANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2796N/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2796N/Aversion 2 for more details (a copy is included in the LICENSE file that You should have received a copy of the GNU General Public License version 2 along with this work; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA or visit www.oracle.com if you need additional information or have any <!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"<
meta name="@author" content="mailto:Ben@galbraiths.org" />
<
meta name="@author" content="mailto:Norman.Walsh@Sun.com" />
<
meta name="@author" content="mailto:Jeff.Suttor@Sun.com" />
<
meta name="@version" content="$Revision: 1.3 $, $Date: 2005/11/03 19:34:17 $" />
<
meta name="@since" content="1.5" />
<
p>This package provides an <
em>object-model neutral</
em> API for the
evaluation of XPath expressions and access to the evaluation
<
p>The following XML standards apply:</
p>
<
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
<
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.
<
h3>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:</
p>
<
p>This example would select the <
code><bar></
code> element in
an XML document such as the following:</
p>
<
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:</
p>
<
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
<
p>A wildcard operator, *, causes all element nodes to be selected.
The following example selects all children elements of a
<
code><foo></
code> element:</
p>
<
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
<
code>/
foo/
bar/<
strong>@id</
strong></
code>
<
td>Selects the attribute <
code>id</
code> of the <
code><bar></
code> element
<
td><
code>/
foo/
bar/<
strong>text()</
strong></
code>
<
td>Selects the text nodes of the <
code><bar></
code> element. No
distinction is made between escaped and non-escaped character data.
<
td><
code>/
foo/
bar/<
strong>comment()</
strong></
code>
<
td>Selects all comment nodes contained in the <
code><bar></
code> element.
<
td><
code>/
foo/
bar/<
strong>processing-instruction()</
strong></
code>
<
td>Selects all processing-instruction nodes contained in the
<
code><bar></
code> element.
<
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>:</
p>
<
p>Predicates may be appended to each other to further refine an
//foo[@include='true'][@mode='bar']
<
h3>Using the XPath API</
h3>
The following example demonstrates using the XPath API to select one
or more nodes from an XML document:</
p>
InputSource inputSource = new InputSource("
widgets.xml");
<
h3>XPath Expressions and 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 other data types:</
p>
<
li><
code>Boolean</
code></
li>
<
li><
code>Number</
code></
li>
<
li><
code>String</
code></
li>
<
p>The desired return type is specified by a {@link
the expression, which is either a call to
QName values are specified as constants in the {@link
<
p>When a <
code>Boolean</
code> return type is requested,
<
code>
Boolean.TRUE</
code> is returned if one or more nodes were
<
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.
<
p>XPath location paths may be relative to a particular node in the
document, known as the <
code>context</
code>. Consider the following
<
p>The <
code><widget></
code> element can be selected with the
following XPath API code:</
p>
// parse the XML as a W3C Document
<
p>With a reference to the <
code><widget></
code> element, a
relative XPath expression can now written to select the
<
code><manufacturer></
code> child element:</
p>
<
strong>String expression = "manufacturer";</
strong>
<
li>Author <
a href="mailto:Ben@galbraiths.org">Ben Galbraith</
a></
li>
<
li>Author <
a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</
a></
li>
<
li>Author <
a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</
a></
li>