⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxpxslt3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>How XPath Works</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JAXPXSLT2.html" />    <link rel="Next" href="JAXPXSLT4.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPXSLT2.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPXSLT4.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp72547"> </a><h2 class="pHeading1">How XPath Works</h2><a name="wp64361"> </a><p class="pBody">The XPath specification is the foundation for a variety of specifications, including XSLT and linking/addressing specifications like XPointer. So an understanding of XPath is fundamental to a lot of advanced XML usage. This section provides a thorough introduction to XPATH in the context of XSLT, so you can refer to it as needed later on. </p><hr><a name="wp64362"> </a><p class="pNote">Note: In this tutorial, you won't actually use XPath until you get to the end of this section, <a  href="JAXPXSLT6.html#wp64930">Transforming XML Data with XSLT</a>. So, if you like, you can skip this section and go on ahead to the next section, <a  href="JAXPXSLT4.html#wp64603">Writing Out a DOM as an XML File</a>. (When you get to the end of that section, there will be a note that refers you back here, so you don't forget!)</p><hr><a name="wp64369"> </a><h3 class="pHeading2">XPATH Expressions</h3><a name="wp69437"> </a><p class="pBody">In general, an XPath expression specifies a <span style="font-style: italic">pattern</span> that selects a set of XML nodes. XSLT templates then use those patterns when applying transformations. (XPointer, on the other hand, adds mechanisms for defining a <span style="font-style: italic">point</span> or a <span style="font-style: italic">range</span>, so that XPath expressions can be used for addressing.)</p><a name="wp64370"> </a><p class="pBody">The nodes in an XPath expression refer to more than just elements. They also refer to text and attributes, among other things. In fact, the XPath specification defines an abstract document model that defines seven different kinds of nodes: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64375"> </a><div class="pSmartList1"><li>root</li></div><a name="wp67350"> </a><div class="pSmartList1"><li>element</li></div><a name="wp67351"> </a><div class="pSmartList1"><li>text</li></div><a name="wp67352"> </a><div class="pSmartList1"><li>attribute</li></div><a name="wp67349"> </a><div class="pSmartList1"><li>comment</li></div><a name="wp64376"> </a><div class="pSmartList1"><li>processing instruction</li></div><a name="wp64377"> </a><div class="pSmartList1"><li>namespace</li></div></ul></div><hr><a name="wp64378"> </a><p class="pNote">Note: The root element of the XML data is modeled by an <span style="font-style: italic">element</span> node. The XPath root node contains the document's root element, as well as other information relating to the document. </p><hr><a name="wp69428"> </a><h3 class="pHeading2">The XSLT/XPath Data Model</h3><a name="wp69429"> </a><p class="pBody">Like the DOM, the XSLT/XPath data model consists of a tree containing a variety of nodes. Under any given element node, there are text nodes, attribute nodes, element nodes, comment nodes, and processing instruction nodes.</p><a name="wp64380"> </a><p class="pBody">In this abstract model, syntactic distinctions disappear, and you are left with a normalized view of the data. In a text node, for example, it makes no difference whether the text was defined in a CDATA section, or if it included entity references. The text node will consist of normalized data, as it exists after all parsing is complete. So the text will contain a <code class="cCode">&lt;</code> character, regardless of whether an entity reference like <code class="cCode">&amp;lt;</code> or a CDATA section was used to include it. (Similarly, the text will contain an <code class="cCode">&amp;</code> character, regardless of whether it was delivered using <code class="cCode">&amp;amp;</code> or it was in a CDATA section.)</p><a name="wp64381"> </a><p class="pBody">In this section of the tutorial, we'll deal mostly with element nodes and text nodes. For the other addressing mechanisms, see the<a  href="http://www.w3.org/TR/xpath" target="_blank"> XPath Specification</a>.</p><a name="wp69443"> </a><h3 class="pHeading2">Templates and Contexts</h3><a name="wp69450"> </a><p class="pBody">An XSLT <span style="font-style: italic">template</span> is a set of formatting instructions that apply to the nodes selected by an XPATH expression. In an stylesheet, a XSLT template would look something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;xsl:template match=&quot;//LIST&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;...&lt;/xsl:template&gt;<a name="wp69567"> </a></pre></div><a name="wp69552"> </a><p class="pBody">The expression <code class="cCode">//LIST</code> selects the set of <code class="cCode">LIST</code> nodes from the input stream. Additional instructions within the template tell the system what to do with them.</p><a name="wp69590"> </a><p class="pBody">The set of nodes selected by such an expression defines the <span style="font-style: italic">context</span> in which other expressions in the template are evaluated. That context can be considered as the whole set -- for example, when determining the number of the nodes it contains.</p><a name="wp69608"> </a><p class="pBody">The context can also be considered as a single member of the set, as each member is processed one by one. For example, inside of the <code class="cCode">LIST</code>-processing template, the expression <code class="cCode">@type</code> refers to the <code class="cCode">type</code> attribute of the current <code class="cCode">LIST</code> node. (Similarly, the expression <code class="cCode">@*</code> refers to all of attributes for the current <code class="cCode">LIST</code> element.) </p><a name="wp64382"> </a><h3 class="pHeading2">Basic XPath Addressing</h3><a name="wp64383"> </a><p class="pBody">An XML document is a tree-structured (hierarchical) collection of nodes. As with a hierarchical directory structure, it is useful to specify a <span style="font-style: italic">path</span> that points a particular node in the hierarchy. (Hence the name of the specification: XPath.) In fact, much of the notation of directory paths is carried over intact:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64384"> </a><div class="pSmartList1"><li>The forward slash <code class="cCode">/</code> is used as a path separator.</li></div><a name="wp64385"> </a><div class="pSmartList1"><li>An absolute path from the root of the document starts with a /.</li></div><a name="wp64386"> </a><div class="pSmartList1"><li>A relative path from a given location starts with anything else.</li></div><a name="wp64387"> </a><div class="pSmartList1"><li>A double period <code class="cCode">..</code> indicates the parent of the current node.</li></div><a name="wp64388"> </a><div class="pSmartList1"><li>A single period <code class="cCode">.</code> indicates the current node.</li></div></ul></div><a name="wp64389"> </a><p class="pBody">For example, In an XHTML document (an XML document that looks like HTML, but which is <span style="font-style: italic">well-formed</span> according to XML rules) the path <code class="cCode">/h1/h2/</code> would indicate an <code class="cCode">h2</code> element under an <code class="cCode">h1</code>. (Recall that in XML, element names are case sensitive, so this kind of specification works much better in XHTML than it would in plain HTML, because HTML is case-insensitive.) </p><a name="wp64390"> </a><p class="pBody">In a pattern-matching specification like XSLT, the specification <code class="cCode">/h1/h2</code> selects <span style="font-style: italic">all</span> <code class="cCode">h2</code> elements that lie under an <code class="cCode">h1</code> element. To select a specific <code class="cCode">h2</code> element, square brackets <code class="cCode">[]</code> are used for indexing (like those used for arrays). The path <code class="cCode">/h1[4]/h2[5]</code> would therefore select the fifth <code class="cCode">h2</code> element under the fourth <code class="cCode">h1</code> element. </p><hr><a name="wp64391"> </a><p class="pNote">Note: In XHTML, all element names are in lowercase. That is a fairly common convention for XML documents. However, uppercase names are easier to read in a tutorial like this one. So, for the remainder of the XSLT tutorial, all XML element names will be in uppercase. (Attribute names, on the other hand, will remain in lowercase.)</p><hr><a name="wp64392"> </a><p class="pBody">A name specified in an XPath expression refers to an element. For example, &quot;<code class="cCode">h1</code>&quot; in <code class="cCode">/h1/h2</code> refers to an <code class="cCode">h1</code> element. To refer to an attribute, you prefix the attribute name with an <code class="cCode">@</code> sign. For example, <code class="cCode">@type</code> refers to the <code class="cCode">type</code> attribute of an element. Assuming you have an XML document with <code class="cCode">LIST</code> elements, for example, the expression <code class="cCode">LIST/@type</code> selects the <code class="cCode">type</code> attribute of the <code class="cCode">LIST</code> element. </p><hr><a name="wp64393"> </a><p class="pNote">Note: Since the expression does not begin with /, the reference specifies a <code class="cCode">list</code> node relative to the current context--whatever position in the document that happens to be.</p><hr><a name="wp64394"> </a><h3 class="pHeading2">Basic XPath Expressions</h3><a name="wp64395"> </a><p class="pBody">The full range of XPath expressions takes advantage of the wildcards, operators, and functions that XPath defines. You'll be learning more about those shortly. Here, we'll take a look at a couple of the most common XPath expressions, simply to introduce them.</p><a name="wp64396"> </a><p class="pBody">The expression <code class="cCode">@type=&quot;unordered&quot;</code> specifies an attribute named <code class="cCode">type</code> whose value is &quot;<code class="cCode">unordered</code>&quot;. And you already know that an expression like <code class="cCode">LIST/@type</code> specifies the <code class="cCode">type </code>attribute of a <code class="cCode">LIST</code> element. </p><a name="wp64397"> </a><p class="pBody">You can combine those two notations to get something interesting! In XPath, the square-bracket notation (<code class="cCode">[]</code>) normally associated with indexing is extended to specify <span style="font-style: italic">selection criteria</span>. So the expression <code class="cCode">LIST[@type=&quot;unordered&quot;]</code> selects all <code class="cCode">LIST</code> elements whose <code class="cCode">type</code> value is &quot;<code class="cCode">unordered</code>&quot;.</p><a name="wp64398"> </a><p class="pBody">Similar expressions exist for elements, where each element has an associated <span style="font-style: italic">string-value</span>. (You'll see how the string-value is determined for a complicated element in a little while. For now, we'll stick with simple elements that have a single text string.)</p><a name="wp64399"> </a><p class="pBody">Suppose you model what's going on in your organization with an XML structure that consists of <code class="cCode">PROJECT</code> elements and <code class="cCode">ACTIVITY</code> elements that have a text string with the project name, multiple <code class="cCode">PERSON</code> elements to list the people involved and, optionally, a <code class="cCode">STATUS</code> element that records the project status. Here are some more examples that use the extended square-bracket notation:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64400"> </a><div class="pSmartList1"><li><code class="cCode">/PROJECT[.=&quot;MyProject&quot;]</code>--selects a <code class="cCode">PROJECT</code> named &quot;<code class="cCode">MyProject</code>&quot;.</li></div><a name="wp64401"> </a><div class="pSmartList1"><li><code class="cCode">/PROJECT[STATUS]</code>--selects all projects that have a <code class="cCode">STATUS</code> child element.</li></div><a name="wp64402"> </a><div class="pSmartList1"><li><code class="cCode">/PROJECT[STATUS=&quot;Critical&quot;]</code>--selects all projects that have a <code class="cCode">STATUS</code> child element with the string-value &quot;<code class="cCode">Critical</code>&quot;.</li></div></ul></div><a name="wp64403"> </a><h3 class="pHeading2">Combining Index Addresses</h3><a name="wp64404"> </a><p class="pBody">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -