1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.4.2_06) on Thu Nov 24 12:17:49 GMT 2005 -->
<TITLE>
javax.xml.xpath
</TITLE>
<META NAME="keywords" CONTENT="javax.xml.xpath package">
<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style">
<SCRIPT type="text/javascript">
function windowTitle()
{
parent.document.title="javax.xml.xpath";
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white" onload="windowTitle();">
<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A>
<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../javax/xml/validation/package-summary.html"><B>PREV PACKAGE</B></A>
<A HREF="../../../org/w3c/xsl/package-summary.html"><B>NEXT PACKAGE</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../index.html" target="_top"><B>FRAMES</B></A>
<A HREF="package-summary.html" target="_top"><B>NO FRAMES</B></A>
<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
<!-- ========= END OF TOP NAVBAR ========= -->
<HR>
<H2>
Package javax.xml.xpath
</H2>
This package provides an <em>object-model neutral</em> API for the
evaluation of XPath expressions and access to the evaluation
environment.
<P>
<B>See:</B>
<BR>
<A HREF="#package_description"><B>Description</B></A>
<P>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Interface Summary</B></FONT></TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPath.html" title="interface in javax.xml.xpath">XPath</A></B></TD>
<TD><code>XPath</code> provides access to the XPath evaluation environment and expressions.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathExpression.html" title="interface in javax.xml.xpath">XPathExpression</A></B></TD>
<TD><code>XPathExpression</code> provides access to compiled XPath expressions.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathFunction.html" title="interface in javax.xml.xpath">XPathFunction</A></B></TD>
<TD><code>XPathFunction</code> provides access to XPath functions.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathFunctionResolver.html" title="interface in javax.xml.xpath">XPathFunctionResolver</A></B></TD>
<TD><code>XPathFunctionResolver</code> provides access to the set of user defined <code>XPathFunction</code>s.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathVariableResolver.html" title="interface in javax.xml.xpath">XPathVariableResolver</A></B></TD>
<TD><code>XPathVariableResolver</code> provides access to the set of user defined XPath variables.</TD>
</TR>
</TABLE>
<P>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Class Summary</B></FONT></TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathConstants.html" title="class in javax.xml.xpath">XPathConstants</A></B></TD>
<TD>XPath constants.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathFactory.html" title="class in javax.xml.xpath">XPathFactory</A></B></TD>
<TD>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.</TD>
</TR>
</TABLE>
<P>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Exception Summary</B></FONT></TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathException.html" title="class in javax.xml.xpath">XPathException</A></B></TD>
<TD><code>XPathException</code> represents a generic XPath exception.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathExpressionException.html" title="class in javax.xml.xpath">XPathExpressionException</A></B></TD>
<TD><code>XPathExpressionException</code> represents an error in an XPath expression.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathFactoryConfigurationException.html" title="class in javax.xml.xpath">XPathFactoryConfigurationException</A></B></TD>
<TD><code>XPathFactoryConfigurationException</code> represents a configuration error in a <code>XPathFactory</code> environment.</TD>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD WIDTH="15%"><B><A HREF="../../../javax/xml/xpath/XPathFunctionException.html" title="class in javax.xml.xpath">XPathFunctionException</A></B></TD>
<TD><code>XPathFunctionException</code> represents an error with an XPath function.</TD>
</TR>
</TABLE>
<P>
<A NAME="package_description"><!-- --></A><H2>
Package javax.xml.xpath Description
</H2>
<P>
<p>This package provides an <em>object-model neutral</em> API for the
evaluation of XPath expressions and access to the evaluation
environment.
</p>
<p>The following XML standards apply:</p>
<ul>
<li><a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a></li>
</ul>
<hr />
<h2>XPath Overview</h2>
<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>
<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.
</p>
<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>
<p>The following is an example of a simple XPath expression:</p>
<pre>
/foo/bar
</pre>
<p>This example would select the <code><bar></code> element in
an XML document such as the following:</p>
<pre>
<foo>
<bar/>
</foo>
</pre>
<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>
<pre>
<foo>
<bar/>
<bar/>
<bar/>
</foo>
</pre>
<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:</p>
<pre>
//bar
</pre>
<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>
<pre>
/foo/*
</pre>
<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:</p>
<table border="1">
<tr>
<td>Location Path</td>
<td>Description</td>
</tr>
<tr>
<td>
<code>/foo/bar/<strong>@id</strong></code>
</td>
<td>Selects the attribute <code>id</code> of the <code><bar></code> element
</td>
</tr>
<tr>
<td><code>/foo/bar/<strong>text()</strong></code>
</td>
<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>
<td><code>/foo/bar/<strong>comment()</strong></code>
</td>
<td>Selects all comment nodes contained in the <code><bar></code> element.
</td>
</tr>
<tr>
<td><code>/foo/bar/<strong>processing-instruction()</strong></code>
</td>
<td>Selects all processing-instruction nodes contained in the
<code><bar></code> element.
</td>
</tr>
</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>:</p>
<pre>
//foo[@include='true']
</pre>
<p>Predicates may be appended to each other to further refine an
expression, such as:</p>
<pre>
//foo[@include='true'][@mode='bar']
</pre>
<h3>Using the XPath API</h3>
<p>
The following example demonstrates using the XPath API to select one
or more nodes from an XML document:</p>
<pre>
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
InputSource inputSource = new InputSource("widgets.xml");
NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
</pre>
<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>
<ul>
<li><code>Boolean</code></li>
<li><code>Number</code></li>
<li><code>String</code></li>
</ul>
<p>The desired 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 to one of the
<code>XPath.evaluate(...)</code> convenience methods. The allowed
QName values are specified as constants in the <A HREF="../../../javax/xml/xpath/XPathConstants.html" title="class in javax.xml.xpath"><CODE>XPathConstants</CODE></A> class; they are:</p>
<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>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>
<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>
<p>The <code>Number</code> return type attempts to coalesce the text
of a node to a <code>double</code> data type.
</p>
<h3>XPath Context</h3>
<p>XPath location paths may be relative to a particular node in the
document, known as the <code>context</code>. Consider the following
XML document:</p>
<pre>
<widgets>
<widget>
<manufacturer/>
<dimensions/>
</widget>
</widgets>
</pre>
<p>The <code><widget></code> element can be selected with the
following XPath API code:</p>
<pre>
// parse the XML as a W3C Document
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new File("/widgets.xml"));
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/widgets/widget";
Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
</pre>
<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>
<pre>
XPath xpath = XPathFactory.newInstance().newXPath();
<strong>String expression = "manufacturer";</strong>
Node manufacturerNode = (Node) xpath.evaluate(expression, <strong>widgetNode</strong>, XPathConstants.NODE);
</pre>
<ul>
<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>
<li>See <a href="http://www.w3.org/TR/xpath">XML Path Language (XPath) Version 1.0</a></li>
<li>Since 1.5</li>
</ul>
<P>
<P>
<HR>
<!-- ======= START OF BOTTOM NAVBAR ====== -->
<A NAME="navbar_bottom"><!-- --></A>
<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=3 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
<TR ALIGN="center" VALIGN="top">
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD>
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Package</B></FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <FONT CLASS="NavBarFont1">Class</FONT> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>
<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../javax/xml/validation/package-summary.html"><B>PREV PACKAGE</B></A>
<A HREF="../../../org/w3c/xsl/package-summary.html"><B>NEXT PACKAGE</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
<A HREF="../../../index.html" target="_top"><B>FRAMES</B></A>
<A HREF="package-summary.html" target="_top"><B>NO FRAMES</B></A>
<SCRIPT type="text/javascript">
<!--
if(window==top) {
document.writeln('<A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A>');
}
//-->
</SCRIPT>
<NOSCRIPT>
<A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>
</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<HR>
</BODY>
</HTML>
|