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

📄 jaxpxslt6.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
Here is the HTML that is generated for the second section when you run the program now:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...&lt;h2&gt;The Second Major Section&lt;/h2&gt;&lt;p&gt;This section adds a LIST and a NOTE.&lt;/p&gt;&lt;p&gt;Here is the LIST:&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Pears&lt;/li&gt;&lt;li&gt;Grapes&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;And here is the NOTE:&lt;/p&gt;&lt;blockquote&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;br&gt;Don&#39;t forget to go to the hardware store on your way to the grocery!&lt;/blockquote&gt;<a name="wp65141"> </a></pre></div><a name="wp65143"> </a><h3 class="pHeading2">Process Inline (Content) Elements</h3><a name="wp65144"> </a><p class="pBody">The only remaining tags in the <code class="cCode">ARTICLE</code> type are the <span style="font-style: italic">inline</span> tags -- the ones that don't create a line break in the output, but which instead are integrated into the stream of text they are part of. </p><a name="wp65145"> </a><p class="pBody">Inline elements are different from structure elements, in that they are part of the content of a tag. If you think of an element as a node in a document tree, then each node has both <span style="font-style: italic">content</span> and <span style="font-style: italic">structure</span>. The content is composed of the text and inline tags it contains. The structure consists of the other elements (structure elements) under the tag.</p><hr><a name="wp65146"> </a><p class="pNote">Note: The sample document described in this section is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article3.xml" target="_blank">article3.xml</a></code>, and the stylesheet used to manipulate it is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article3.xsl" target="_blank">article3.xsl</a></code>. The result is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/stylizer3.html" target="_blank">stylizer3.html</a></code>. (The browser-displayable versions are <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article3-xml.html" target="_blank">article3-xml.html</a></code>,<code class="cCode"><a  href="../examples/jaxp/xslt/samples/article3-xsl.html" target="_blank"> article3-xsl.html</a></code>, and <code class="cCode"><a  href="../examples/jaxp/xslt/samples/stylizer3-src.html" target="_blank">stylizer3-src.html</a></code>.)</p><hr><a name="wp65147"> </a><p class="pBody">Start by adding one more bit of test data to the sample document:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&quot;1.0&quot;?&gt;&lt;ARTICLE&gt;&nbsp;&nbsp;&lt;TITLE&gt;A Sample Article&lt;/TITLE&gt;&nbsp;&nbsp;&lt;SECT&gt;The First Major Section&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&lt;/SECT&gt;&nbsp;&nbsp;&lt;SECT&gt;The Second Major Section&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&lt;/SECT&gt; <code class="cCodeBold">&nbsp;&nbsp;&lt;SECT&gt;The &lt;I&gt;Third&lt;/I&gt; Major Section&nbsp;&nbsp;&nbsp;&nbsp;&lt;PARA&gt;In addition to the inline tag in the heading, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this section defines the term &lt;DEF&gt;inline&lt;/DEF&gt;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;which literally means &quot;no line break&quot;. It also &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adds a simple link to the main page for the Java&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;platform (&lt;LINK&gt;http://java.sun.com&lt;/LINK&gt;), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;as well as a link to the &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;LINK target=&quot;http://java.sun.com/xml&quot;&gt;XML&lt;/LINK&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;page.&nbsp;&nbsp;&nbsp;&nbsp;&lt;/PARA&gt;&nbsp;&nbsp;&lt;/SECT&gt; </code>&lt;/ARTICLE&gt;<a name="wp65148"> </a></pre></div><a name="wp65149"> </a><p class="pBody">Now, process the inline <code class="cCode">&lt;DEF&gt;</code> elements in paragraphs, renaming them to HTML italics tags:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;xsl:template match=&quot;DEF&quot;&gt;&nbsp;&nbsp;&lt;i&gt; &lt;xsl:apply-templates/&gt; &lt;/i&gt; &lt;/xsl:template&gt; </code><a name="wp65150"> </a></pre></div><a name="wp65151"> </a><p class="pBody">Next, comment out the text-node normalization. It has served its purpose, and now you're to the point that you need to preserve important spaces:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;!--</code>&nbsp;&nbsp;&lt;xsl:template match=&quot;text()&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:value-of select=&quot;normalize-space()&quot;/&gt;&nbsp;&nbsp;&lt;/xsl:template&gt;<code class="cCodeBold">--&gt;</code><a name="wp65152"> </a></pre></div><a name="wp65153"> </a><p class="pBody">This modification keeps us from losing spaces before tags like <code class="cCode">&lt;I&gt;</code> and <code class="cCode">&lt;DEF&gt;</code>. (Try the program without this modification to see the result.) </p><a name="wp65154"> </a><p class="pBody">Now, process basic inline HTML elements like <code class="cCode">&lt;B&gt;</code>, <code class="cCode">&lt;I&gt;</code>, <code class="cCode">&lt;U&gt;</code> for bold, italics, and underlining.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;xsl:template match=&quot;B|I|U&quot;&gt;&nbsp;&nbsp;&lt;xsl:element name=&quot;{name()}&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates/&gt;&nbsp;&nbsp;&lt;/xsl:element&gt; &lt;/xsl:template&gt;</code><a name="wp65155"> </a></pre></div><a name="wp65156"> </a><p class="pBody">The <code class="cCode">&lt;xsl:element&gt;</code> tag lets you compute the element you want to generate. Here, you generate the appropriate inline tag using the name of the current element. In particular, note the use of curly braces (<code class="cCode">{}</code>) in the <code class="cCode">name=&quot;..&quot;</code> expression. Those curly braces cause the text inside the quotes to be processed as an XPath expression, instead of being interpreted as a literal string. Here, they cause the XPath <code class="cCode">name()</code> function to return the name of the current node.</p><a name="wp65157"> </a><p class="pBody">Curly braces are recognized anywhere that an <span style="font-style: italic">attribute value template</span> can occur. (Attribute value templates are defined in section 7.6.2 of the <a  href="http://www.w3.org/TR/xslt" target="_blank">XSLT specification</a>, and they appear several places in the template definitions.). In such expressions, curly braces can also be used to refer to the value of an attribute, <code class="cCode">{@foo}</code>, or to the content of an element <code class="cCode">{foo}</code>.</p><hr><a name="wp65158"> </a><p class="pNote">Note: You can also generate attributes using <code class="cCode">&lt;xsl:attribute&gt;</code>. For more information, see Section 7.1.3 of the<a  href="http://www.w3.org/TR/xslt" target="_blank"> XSLT Specification</a>.</p><hr><a name="wp65159"> </a><p class="pBody">The last remaining element is the <code class="cCode">LINK</code> tag. The easiest way to process that tag will be to set up a <span style="font-style: italic">named template</span> that we can drive with a parameter:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;xsl:template name=&quot;htmLink&quot;&gt;&nbsp;&nbsp;&lt;xsl:param name=&quot;dest&quot; select=&quot;UNDEFINED&quot;/&gt; &nbsp;&nbsp;&lt;xsl:element name=&quot;a&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:attribute name=&quot;href&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:value-of select=&quot;$dest&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:attribute&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates/&gt; &nbsp;&nbsp;&lt;/xsl:element&gt; &lt;/xsl:template&gt;</code><a name="wp65160"> </a></pre></div><a name="wp65161"> </a><p class="pBody">The major difference in this template is that, instead of specifying a <code class="cCode">match</code> clause, you gave the template a name with the <code class="cCode">name</code>=&quot;&quot; clause. So this template only gets executed when you invoke it.</p><a name="wp65162"> </a><p class="pBody">Within the template, you also specified a parameter named <code class="cCode">dest</code>, using the <code class="cCode">&lt;xsl:param&gt;</code> tag. For a bit of error checking, you used the <code class="cCode">select</code> clause to give that parameter a default value of <code class="cCode">UNDEFINED</code>. To reference the variable in the <code class="cCode">&lt;xsl:value-of&gt;</code> tag, you specified <code class="cCode">&quot;$dest&quot;</code>. </p><hr><a name="wp65163"> </a><p class="pNote">Note: Recall that an entry in quotes is interpreted as an expression, unless it is further enclosed in single quotes. That's why the single quotes were needed earlier, in <code class="cCode">&quot;@type=&#39;ordered&#39;&quot;</code>--to make sure that <code class="cCode">ordered</code> was interpreted as a string.</p><hr><a name="wp65164"> </a><p class="pBody">The <code class="cCode">&lt;xsl:element&gt;</code> tag generates an element. Previously, we have been able to simply specify the element we want by coding something like <code class="cCode">&lt;html&gt;</code>. But here you are dynamically generating the content of the HTML anchor (<code class="cCode">&lt;a&gt;</code>) in the body of the <code class="cCode">&lt;xsl:element&gt;</code> tag. And you are dynamically generating the <code class="cCode">href</code> attribute of the anchor using the <code class="cCode">&lt;xsl:attribute&gt;</code> tag.</p><a name="wp65165"> </a><p class="pBody">The last important part of the template is the <code class="cCode">&lt;apply-templates&gt;</code> tag, which inserts the text from the text node under the <code class="cCode">LINK</code> element. Without it, there would be no text in the generated HTML link.</p><a name="wp65166"> </a><p class="pBody">Next, add the template for the <code class="cCode">LINK</code> tag, and call the named template from within it: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;xsl:template match=&quot;LINK&quot;&gt;&nbsp;&nbsp;&lt;xsl:if test=&quot;@target&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--Target attribute specified.--&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:call-template name=&quot;htmLink&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:with-param name=&quot;dest&quot; select=&quot;@target&quot;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:call-template&gt;&nbsp;&nbsp;&lt;/xsl:if&gt;&lt;/xsl:template&gt;</code>&lt;xsl:template name=&quot;htmLink&quot;&gt;&nbsp;&nbsp;...<a name="wp65167"> </a></pre></div><a name="wp65169"> </a><p class="pBody">The <code class="cCode">test=&quot;@target&quot;</code> clause returns true if the <code class="cCode">target</code> attribute exists in the <code class="cCode">LINK</code> tag. So this <code class="cCode">&lt;xsl-if&gt;</code> tag generates HTML links when the text of the link and the target defined for it are different.</p><a name="wp65170"> </a><p class="pBody">The <code class="cCode">&lt;xsl:call-template&gt;</code> tag invokes the named template, while <code class="cCode">&lt;xsl:with-param&gt;</code> specifies a parameter using the <code class="cCode">name</code> clause, and its value using the <code class="cCode">select</code> clause.</p><a name="wp65171"> </a><p class="pBody">As the very last step in the stylesheet construction process, add the <code class="cCode">&lt;xsl-if&gt;</code> tag shown below to process <code class="cCode">LINK</code> tags that do not have a target attribute.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;xsl:template match=&quot;LINK&quot;&gt;&nbsp;&nbsp;&lt;xsl:if test=&quot;@target&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&lt;/xsl:if&gt;<a name="wp65172"> </a><code class="cCodeBold">&nbsp;&nbsp;&lt;xsl:if test=&quot;not(@target)&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:call-template name=&quot;htmLink&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:with-param name=&quot;dest&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:with-param&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:call-template&gt;&nbsp;&nbsp;&lt;/xsl:if&gt;</code>&lt;/xsl:template&gt;<a name="wp65173"> </a></pre></div><a name="wp65174"> </a><p class="pBody">The <code class="cCode">not(...)</code> clause inverts the previous test (remember, there is no <code class="cCode">else</code> clause). So this part of the template is interpreted when the <code class="cCode">target</code> attribute is not specified. This time, the parameter value comes not from a <code class="cCode">select</code> clause, but from the <span style="font-style: italic">contents</span> of the <code class="cCode">&lt;xsl:with-param&gt;</code> element. </p><hr><a name="wp65175"> </a><p class="pNote">Note: Just to make it explicit: Parameters and variables (which are discussed in a few moments in <a  href="JAXPXSLT6.html#wp72458"></a><a  href="JAXPXSLT6.html#wp72458">What Else Can XSLT Do?</a> can have their value specified <span style="font-style: italic">either</span> by a <code class="cCode">select</code> clause, whi

⌨️ 快捷键说明

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