uTools-Manuals/docs/java/javax/xml/xpath/package-summary.html

249 lines
12 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="header">
<h1 class="title" title="Package">Package javax.xml.xpath</h1>
<div class="docSummary">
<div class="block">
该包提供了一个
<em>对象模型中立的</em> API用于评估XPath表达式和访问评估环境。
</div>
</div>
<p>See: <a href="#package.description">描述</a></p>
</div><div class="contentContainer">
<ul class="blockList">
<li class="blockList">
<table border="0" cellpadding="3" cellspacing="0" class="typeSummary" summary="Interface Summary table, listing interfaces, and an explanation">
<caption>
<span>接口摘要</span>
<span class="tabEnd"> </span>
</caption>
<tbody>
<tr>
<th class="colFirst" scope="col">接口</th>
<th class="colLast" scope="col">描述</th>
</tr>
</tbody>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPath.html" title="interface in javax.xml.xpath">XPath</a></td>
<td class="colLast">
<div class="block">
<code>XPath</code>提供对XPath评估环境和表达式的访问。
</div> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathExpression.html" title="interface in javax.xml.xpath">XPathExpression</a></td>
<td class="colLast">
<div class="block">
<code>XPathExpression</code>提供对编译的XPath表达式的访问。
</div> </td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathFunction.html" title="interface in javax.xml.xpath">XPathFunction</a></td>
<td class="colLast">
<div class="block">
<code>XPathFunction</code>提供对XPath函数的访问。
</div> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathFunctionResolver.html" title="interface in javax.xml.xpath">XPathFunctionResolver</a></td>
<td class="colLast">
<div class="block">
<code>XPathFunctionResolver</code>提供访问一组用户定义的
<code>XPathFunction</code>秒。
</div> </td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathVariableResolver.html" title="interface in javax.xml.xpath">XPathVariableResolver</a></td>
<td class="colLast">
<div class="block">
<code>XPathVariableResolver</code>提供对用户定义的XPath变量集的访问。
</div> </td>
</tr>
</tbody>
</table> </li>
<li class="blockList">
<table border="0" cellpadding="3" cellspacing="0" class="typeSummary" summary="Class Summary table, listing classes, and an explanation">
<caption>
<span>类摘要</span>
<span class="tabEnd"> </span>
</caption>
<tbody>
<tr>
<th class="colFirst" scope="col"></th>
<th class="colLast" scope="col">描述</th>
</tr>
</tbody>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathConstants.html" title="class in javax.xml.xpath">XPathConstants</a></td>
<td class="colLast">
<div class="block">
XPath常量。
</div> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathFactory.html" title="class in javax.xml.xpath">XPathFactory</a></td>
<td class="colLast">
<div class="block">
<span>可以使用<code>XPathFactory</code>实例创建<a href="../../../javax/xml/xpath/XPath.html" title="javax.xml.xpath中的接口"><code>XPath</code></a>对象。</span>
</div> </td>
</tr>
</tbody>
</table> </li>
<li class="blockList">
<table border="0" cellpadding="3" cellspacing="0" class="typeSummary" summary="Exception Summary table, listing exceptions, and an explanation">
<caption>
<span>异常摘要</span>
<span class="tabEnd"> </span>
</caption>
<tbody>
<tr>
<th class="colFirst" scope="col">异常</th>
<th class="colLast" scope="col">描述</th>
</tr>
</tbody>
<tbody>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathException.html" title="class in javax.xml.xpath">XPathException</a></td>
<td class="colLast">
<div class="block">
<code>XPathException</code>代表一个通用的XPath异常。
</div> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathExpressionException.html" title="class in javax.xml.xpath">XPathExpressionException</a></td>
<td class="colLast">
<div class="block">
<code>XPathExpressionException</code>表示XPath表达式中的错误。
</div> </td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathFactoryConfigurationException.html" title="class in javax.xml.xpath">XPathFactoryConfigurationException</a></td>
<td class="colLast">
<div class="block">
<code>XPathFactoryConfigurationException</code>表示在配置错误
<code>XPathFactory</code>环境。
</div> </td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../../javax/xml/xpath/XPathFunctionException.html" title="class in javax.xml.xpath">XPathFunctionException</a></td>
<td class="colLast">
<div class="block">
<code>XPathFunctionException</code>表示XPath函数的错误。
</div> </td>
</tr>
</tbody>
</table> </li>
</ul>
<a name="package.description">
<!-- --> </a>
<h2 title="Package javax.xml.xpath Description">Package javax.xml.xpath Description</h2>
<div class="block">
<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>&lt;bar&gt;</code> element in an XML document such as the following:</p>
<pre>&lt;foo&gt;
&lt;bar/&gt;
&lt;/foo&gt;</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>&lt;bar&gt;</code> elements in the following document would be selected by the <code>/foo/bar</code> expression:</p>
<pre>&lt;foo&gt;
&lt;bar/&gt;
&lt;bar/&gt;
&lt;bar/&gt;
&lt;/foo&gt;</pre>
<p>A special location path operator, <code>//</code>, selects nodes at any depth in an XML document. The following example selects all <code>&lt;bar&gt;</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>&lt;foo&gt;</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">
<tbody>
<tr>
<td>Location Path</td>
<td>描述</td>
</tr>
<tr>
<td> <code>/foo/bar/<strong>@id</strong></code> </td>
<td>Selects the attribute <code>id</code> of the <code>&lt;bar&gt;</code> element </td>
</tr>
<tr>
<td><code>/foo/bar/<strong>text()</strong></code> </td>
<td>Selects the text nodes of the <code>&lt;bar&gt;</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>&lt;bar&gt;</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>&lt;bar&gt;</code> element. </td>
</tr>
</tbody>
</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>&lt;foo&gt;</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");
NodeList nodes = (NodeList) 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>&lt;widgets&gt;
&lt;widget&gt;
&lt;manufacturer/&gt;
&lt;dimensions/&gt;
&lt;/widget&gt;
&lt;/widgets&gt;</pre>
<p>The <code>&lt;widget&gt;</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>&lt;widget&gt;</code> element, a relative XPath expression can now written to select the <code>&lt;manufacturer&gt;</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>
</div>
</div>