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

📄 jaxpxslt3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
The XPath specification defines quite a few addressing mechanisms, and they can be combined in many different ways. As a result, XPath delivers a lot of expressive power for a relatively simple specification. This section illustrates two more interesting combinations:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64405"> </a><div class="pSmartList1"><li><code class="cCode">LIST[@type=&quot;ordered&quot;][3]</code>--selects all <code class="cCode">LIST</code> elements of type &quot;<code class="cCode">ordered</code>&quot;, and returns the third.</li></div><a name="wp64406"> </a><div class="pSmartList1"><li><code class="cCode">LIST[3][@type=&quot;ordered&quot;]</code>--selects the third <code class="cCode">LIST</code> element, but only if it is of type &quot;<code class="cCode">ordered</code>&quot;.</li></div></ul></div><hr><a name="wp64407"> </a><p class="pNote">Note: Many more combinations of address operators are listed in section 2.5 of the <a  href="http://www.w3.org/TR/xpath" target="_blank">XPath Specification</a>. This is arguably the most useful section of the spec for defining an XSLT transform.</p><hr><a name="wp64408"> </a><h3 class="pHeading2">Wildcards</h3><a name="wp64409"> </a><p class="pBody">By definition, an unqualified XPath expression selects a set of XML nodes that matches that specified pattern. For example, <code class="cCode">/HEAD</code> matches all top-level <code class="cCode">HEAD</code> entries, while <code class="cCode">/HEAD[1]</code> matches only the first. <a  href="JAXPXSLT3.html#wp76893">Table 7-1</a> lists the wildcards that can be used in XPath expressions to broaden the scope of the pattern matching.</p><div align="left"><table border="1" summary="XPath Wildcards" id="wp76893">  <caption><a name="wp76893"> </a><div class="pTableTitle">Table 7-1   XPath Wildcards</div></caption>  <tr align="center">    <th><a name="wp76897"> </a><div class="pCellHeading">Wildcard</div></th>    <th><a name="wp76899"> </a><div class="pCellHeading">Meaning</div></th></tr>  <tr align="left">    <td><a name="wp76901"> </a><div class="pCellBody"><code class="cCode">* </code></div></td>    <td><a name="wp76903"> </a><div class="pCellBody">Matches any element node (not attributes or text).</div></td></tr>  <tr align="left">    <td><a name="wp76905"> </a><div class="pCellBody"><code class="cCode">node()</code></div></td>    <td><a name="wp76907"> </a><div class="pCellBody">Matches any node of any kind: element node, text node, attribute node, processing instruction node, namespace node, or comment node.</div></td></tr>  <tr align="left">    <td><a name="wp76909"> </a><div class="pCellBody"><code class="cCode">@*</code></div></td>    <td><a name="wp76911"> </a><div class="pCellBody">Matches any attribute node.</div></td></tr></table></div><p class="pBody"></p><a name="wp76866"> </a><p class="pBody">In the project database example, for instance, <code class="cCode">/*PERSON[.=&quot;Fred&quot;]</code> matches any <code class="cCode">PROJECT</code> or <code class="cCode">ACTIVITY</code> element that names Fred. </p><a name="wp64429"> </a><h3 class="pHeading2">Extended-Path Addressing</h3><a name="wp64430"> </a><p class="pBody">So far, all of the patterns we've seen have specified an exact number of levels in the hierarchy. For example,<code class="cCode"> /HEAD</code> specifies any <code class="cCode">HEAD</code> element at the first level in the hierarchy, while <code class="cCode">/*/*</code> specifies any element at the second level in the hierarchy. To specify an indeterminate level in the hierarchy, use a double forward slash (<code class="cCode">//</code>). For example, the XPath expression <code class="cCode">//PARA</code> selects all <code class="cCode">paragraph</code> elements in a document, wherever they may be found. </p><a name="wp64431"> </a><p class="pBody">The <code class="cCode">//</code> pattern can also be used within a path. So the expression <code class="cCode">/HEAD/LIST//PARA</code> indicates all paragraph elements in a subtree that begins from <code class="cCode">/HEAD/LIST</code>.</p><a name="wp64432"> </a><h3 class="pHeading2">XPath Data Types and Operators</h3><a name="wp64433"> </a><p class="pBody">XPath expressions yield either a set of nodes, a string, a boolean (true/false value), or a number. <a  href="JAXPXSLT3.html#wp76971">Table 7-2</a> lists the operators that can be used in an Xpath expression</p><div align="left"><table border="1" summary="XPath Operators" id="wp76971">  <caption><a name="wp76971"> </a><div class="pTableTitle">Table 7-2   XPath Operators</div></caption>  <tr align="center">    <th><a name="wp76975"> </a><div class="pCellHeading">Operator</div></th>    <th><a name="wp76977"> </a><div class="pCellHeading">Meaning</div></th></tr>  <tr align="left">    <td><a name="wp76979"> </a><div class="pCellBody"><code class="cCode">|</code></div></td>    <td><a name="wp76981"> </a><div class="pCellBody">Alternative. For example, <code class="cCode">PARA|LIST</code> selects all <code class="cCode">PARA</code> and <code class="cCode">LIST</code> elements.</div></td></tr>  <tr align="left">    <td><a name="wp76983"> </a><div class="pCellBody"><code class="cCode">or, and</code></div></td>    <td><a name="wp76985"> </a><div class="pCellBody">Returns the or/and of two boolean values.</div></td></tr>  <tr align="left">    <td><a name="wp76987"> </a><div class="pCellBody"><code class="cCode">=, !=</code></div></td>    <td><a name="wp77048"> </a><div class="pCellBody">Equal or not equal, for booleans, strings, and numbers. </div></td></tr>  <tr align="left">    <td><a name="wp76991"> </a><div class="pCellBody"><code class="cCode">&lt;, &gt;, &lt;=, &gt;=</code></div></td>    <td><a name="wp76993"> </a><div class="pCellBody">Less than, greater than, less than or equal to, greater than or equal to--for numbers.</div></td></tr>  <tr align="left">    <td><a name="wp76995"> </a><div class="pCellBody"> <code class="cCode">+, -, *, div, mod</code></div></td>    <td><a name="wp76997"> </a><div class="pCellBody">Add, subtract, multiply, floating-point divide, and modulus (remainder) operations (e.g. 6 mod 4 = 2)</div></td></tr></table></div><p class="pBody"></p><a name="wp64460"> </a><p class="pBody">Finally, expressions can be grouped in parentheses, so you don't have to worry about operator precedence. </p><hr><a name="wp67633"> </a><p class="pNote">Note: &quot;Operator precedence&quot; is a term that answers the question, &quot;If you specify <code class="cCode">a + b * c</code>, does that mean <code class="cCode">(a+b) * c</code> or <code class="cCode">a + (b*c)</code>?&quot;. (The operator precedence is roughly the same as that shown in the table.)</p><hr><a name="wp64461"> </a><h3 class="pHeading2">String-Value of an Element</h3><a name="wp64462"> </a><p class="pBody">Before continuing, it's worthwhile to understand how the string-value of a more complex element is determined. We'll do that now.</p><a name="wp64463"> </a><p class="pBody">The string-value of an element is the concatenation of all descendent text nodes, no matter how deep. So, for a &quot;mixed-model&quot; XML data element like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;PARA&gt;This paragraph contains a &lt;B&gt;bold&lt;/B&gt; word&lt;/PARA&gt;<a name="wp64464"> </a></pre></div><a name="wp64465"> </a><p class="pBody">The string-value of <code class="cCode">&lt;PARA&gt;</code> is &quot;This paragraph contains a bold word&quot;. In particular, note that <code class="cCode">&lt;B&gt;</code> is a child of <code class="cCode">&lt;PARA&gt;</code> and that the text contained in all children is concatenated to form the string-value. </p><a name="wp64466"> </a><p class="pBody">Also, it is worth understanding that the text in the abstract data model defined by XPath is fully normalized. So whether the XML structure contains the entity reference <code class="cCode">&amp;lt;</code> or &quot;<code class="cCode">&lt;</code>&quot; in a CDATA section, the element's string-value will contain the &quot;<code class="cCode">&lt;</code>&quot; character. Therefore, when generating HTML or XML with an XSLT stylesheet, occurrences of &quot;<code class="cCode">&lt;</code>&quot; will have to be converted to <code class="cCode">&amp;lt;</code> or enclosed in a CDATA section. Similarly, occurrences of &quot;<code class="cCode">&amp;</code>&quot; will need to be converted to <code class="cCode">&amp;amp;</code>.</p><a name="wp64467"> </a><h3 class="pHeading2">XPath Functions

⌨️ 快捷键说明

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