286N/A<?
xml version="1.0" encoding="UTF-8"?>
286N/ACopyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved. 286N/ADO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 286N/AThis code is free software; you can redistribute it and/or modify it 286N/Aunder the terms of the GNU General Public License version 2 only, as 286N/Apublished by the Free Software Foundation. Oracle designates this 286N/Aparticular file as subject to the "Classpath" exception as provided 286N/Aby Oracle in the LICENSE file that accompanied this code. 286N/AThis code is distributed in the hope that it will be useful, but WITHOUT 286N/AANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 286N/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 286N/Aversion 2 for more details (a copy is included in the LICENSE file that 286N/AYou should have received a copy of the GNU General Public License version 286N/A2 along with this work; if not, write to the Free Software Foundation, 286N/AInc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 286N/APlease contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 286N/A<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 286N/A<
meta name="@author" content="mailto:Ben@galbraiths.org" />
286N/A<
meta name="@author" content="mailto:Norman.Walsh@Sun.com" />
286N/A<
meta name="@author" content="mailto:Jeff.Suttor@Sun.com" />
286N/A<
meta name="@version" content="$Revision: 1.3 $, $Date: 2005/11/03 19:34:17 $" />
286N/A<
meta name="@since" content="1.5" />
286N/A<
p>This package provides an <
em>object-model neutral</
em> API for the
286N/Aevaluation of XPath expressions and access to the evaluation
286N/A<
p>The following XML standards apply:</
p>
286N/A<
p>The XPath language provides a simple, concise syntax for selecting
286N/Anodes from an XML document. XPath also provides rules for converting a
286N/Anode in an XML document object model (DOM) tree to a boolean, double,
286N/Aor string value. XPath is a W3C-defined language and an official W3C
286N/Arecommendation; the W3C hosts the XML Path Language (XPath) Version
286N/A<
p>XPath started in life in 1999 as a supplement to the XSLT and
286N/AXPointer languages, but has more recently become popular as a
286N/Astand-alone language, as a single XPath expression can be used to
286N/Areplace many lines of DOM API code.
286N/A<
h3>XPath Expressions</
h3>
286N/A<
p>An XPath <
em>expression</
em> is composed of a <
em>location
286N/Apath</
em> and one or more optional <
em>predicates</
em>. Expressions
286N/Amay also include XPath variables.
286N/A<
p>The following is an example of a simple XPath expression:</
p>
286N/A<
p>This example would select the <
code><bar></
code> element in
286N/Aan XML document such as the following:</
p>
286N/A<
p>The expression <
code>/
foo/
bar</
code> is an example of a location
286N/Apath. While XPath location paths resemble Unix-style file system
286N/Apaths, an important distinction is that XPath expressions return
286N/A<
em>all</
em> nodes that match the expression. Thus, all three
286N/A<
code><bar></
code> elements in the following document would be
286N/A<
p>A special location path operator, <
code>//</
code>, selects nodes at
286N/Aany depth in an XML document. The following example selects all
286N/A<
code><bar></
code> elements regardless of their location in a
286N/A<
p>A wildcard operator, *, causes all element nodes to be selected.
286N/AThe following example selects all children elements of a
286N/A<
code><foo></
code> element:</
p>
286N/A<
p>In addition to element nodes, XPath location paths may also address
286N/Aattribute nodes, text nodes, comment nodes, and processing instruction
286N/Anodes. The following table gives examples of location paths for each
286N/Aof these node types:</
p>
286N/A<
td>Selects the attribute <
code>id</
code> of the <
code><bar></
code> element
286N/A<
td>Selects the text nodes of the <
code><bar></
code> element. No
286N/Adistinction is made between escaped and non-escaped character data.
286N/A<
td>Selects all comment nodes contained in the <
code><bar></
code> element.
286N/A<
td><
code>/
foo/
bar/<
strong>processing-instruction()</
strong></
code>
286N/A<
td>Selects all processing-instruction nodes contained in the
286N/A<
code><bar></
code> element.
286N/A<
p>Predicates allow for refining the nodes selected by an XPath
286N/Alocation path. Predicates are of the form
286N/A<
code>[<
em>expression</
em>]</
code>. The following example selects all
286N/A<
code><foo></
code> elements that contain an <
code>include</
code>
286N/Aattribute with the value of <
code>true</
code>:</
p>
286N/A<
p>Predicates may be appended to each other to further refine an
286N/Aexpression, such as:</
p>
286N/A//foo[@include='true'][@mode='bar']
286N/A<
h3>Using the XPath API</
h3>
286N/AThe following example demonstrates using the XPath API to select one
286N/Aor more nodes from an XML document:</
p>
286N/A<
h3>XPath Expressions and Types</
h3>
286N/A<
p>While XPath expressions select nodes in the XML document, the XPath
286N/AAPI allows the selected nodes to be coalesced into one of the
286N/Afollowing other data types:</
p>
286N/A<
li><
code>Boolean</
code></
li>
286N/A<
li><
code>Number</
code></
li>
286N/A<
li><
code>String</
code></
li>
286N/A<
p>The desired return type is specified by a {@link
286N/Athe expression, which is either a call to
286N/AQName values are specified as constants in the {@link
286N/A<
p>When a <
code>Boolean</
code> return type is requested,
286N/A<
p>The <
code>String</
code> return type is a convenience for retrieving
286N/Athe character data from a text node, attribute node, comment node, or
286N/Aprocessing-instruction node. When used on an element node, the value
286N/Aof the child text nodes is returned.
286N/A<
p>The <
code>Number</
code> return type attempts to coalesce the text
286N/Aof a node to a <
code>double</
code> data type.
286N/A<
p>XPath location paths may be relative to a particular node in the
286N/Adocument, known as the <
code>context</
code>. Consider the following
286N/A<
p>The <
code><widget></
code> element can be selected with the
286N/Afollowing XPath API code:</
p>
286N/A// parse the XML as a W3C Document
286N/A<
p>With a reference to the <
code><widget></
code> element, a
286N/Arelative XPath expression can now written to select the
286N/A<
code><manufacturer></
code> child element:</
p>
286N/A<
strong>String expression = "manufacturer";</
strong>
286N/A<
li>Author <
a href="mailto:Ben@galbraiths.org">Ben Galbraith</
a></
li>
286N/A<
li>Author <
a href="mailto:Norman.Walsh@Sun.com">Norman Walsh</
a></
li>
286N/A<
li>Author <
a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</
a></
li>