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

📄 jaxpxslt6.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<?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>Transforming XML Data with XSLT</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="JAXPXSLT5.html" />    <link rel="Next" href="JAXPXSLT7.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="JAXPXSLT5.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="JAXPXSLT7.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="wp64930"> </a><h2 class="pHeading1">Transforming XML Data with XSLT</h2><a name="wp64931"> </a><p class="pBody">The XML Stylesheet Language for Transformations (XSLT) can be used for many purposes. For example, with a sufficiently intelligent stylesheet, you could generate PDF or PostScript output from the XML data. But generally, XSLT is used to generate formatted HTML output, or to create an alternative XML representation of the data. </p><a name="wp64932"> </a><p class="pBody">In this section of the tutorial, you'll use an XSLT transform to translate XML input data to HTML output. </p><hr><a name="wp64933"> </a><p class="pNote">Note: The XSLT specification is large and complex. So this tutorial can only scratch the surface. It will give you enough of a background to get started, so you can undertake simple XSLT processing tasks. It should also give you a head start when you investigate XSLT further. For a more thorough grounding, consult a good reference manual, such as Michael Kay&#39;s XSLT Programmer&#39;s Reference.</p><hr><a name="wp64935"> </a><h3 class="pHeading2">Defining a Simple &lt;article&gt; Document Type</h3><a name="wp64936"> </a><p class="pBody">We'll start by defining a very simple document type that could be used for writing articles. Our <code class="cCode">&lt;article&gt;</code> documents will contain these structure tags:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64937"> </a><div class="pSmartList1"><li><code class="cCode">&lt;TITLE&gt;</code> -- The title of the article</li></div><a name="wp64938"> </a><div class="pSmartList1"><li><code class="cCode">&lt;SECT&gt;</code> -- A section, consisting of a <span style="font-style: italic">heading</span> and a <span style="font-style: italic">body</span></li></div><a name="wp64939"> </a><div class="pSmartList1"><li><code class="cCode">&lt;PARA&gt;</code> -- A paragraph</li></div><a name="wp64940"> </a><div class="pSmartList1"><li><code class="cCode">&lt;LIST&gt;</code> -- A list.</li></div><a name="wp64941"> </a><div class="pSmartList1"><li><code class="cCode">&lt;ITEM&gt;</code> -- An entry in a list</li></div><a name="wp64942"> </a><div class="pSmartList1"><li><code class="cCode">&lt;NOTE&gt;</code> -- An aside, which will be offset from the main text</li></div></ul></div><a name="wp64943"> </a><p class="pBody">The slightly unusual aspect of this structure is that we won't create a separate element tag for a section heading. Such elements are commonly created to distinguish the heading text (and any tags it contains) from the body of the section (that is, any structure elements underneath the heading).</p><a name="wp64944"> </a><p class="pBody">Instead, we'll allow the heading to merge seamlessly into the body of a section. That arrangement adds some complexity to the stylesheet, but that will give us a chance to explore XSLT's template-selection mechanisms. It also matches our intuitive expectations about document structure, where the text of a heading is directly followed by structure elements, which can simplify outline-oriented editing.</p><hr><a name="wp64945"> </a><p class="pNote">Note: However, that structure is not easily validated, because XML's mixed-content model allows text anywhere in a section, whereas we want to confine text and inline elements so that they only appear before the first structure element in the body of the section. The assertion-based validator (<a  href="IntroXML3.html#wp64190">Schematron</a>) can do it, but most other schema mechanisms can't. So we'll dispense with defining a DTD for the document type.</p><hr><a name="wp64946"> </a><p class="pBody">In this structure, sections can be nested. The depth of the nesting will determine what kind of HTML formatting to use for the section heading (for example, <code class="cCode">h1</code> or <code class="cCode">h2</code>). Using a plain <code class="cCode">SECT</code> tag (instead of numbered sections) is also useful with outline-oriented editing, because it lets you move sections around at will without having to worry about changing the numbering for that section or for any of the other sections that might be affected by the move.</p><a name="wp68552"> </a><p class="pBody">For lists, we'll use a <code class="cCode">type</code> attribute to specify whether the list entries are <code class="cCode">unordered</code> (bulleted), <code class="cCode">alpha</code> (enumerated with lower case letters), <code class="cCode">ALPHA</code> (enumerated with uppercase letters), or <code class="cCode">numbered</code>. </p><a name="wp68553"> </a><p class="pBody">We'll also allow for some inline tags that change the appearance of the text:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64949"> </a><div class="pSmartList1"><li><code class="cCode">&lt;B&gt;</code> -- bold</li></div><a name="wp64950"> </a><div class="pSmartList1"><li><code class="cCode">&lt;I&gt;</code> -- italics</li></div><a name="wp64951"> </a><div class="pSmartList1"><li><code class="cCode">&lt;U&gt;</code> -- underline</li></div><a name="wp64952"> </a><div class="pSmartList1"><li><code class="cCode">&lt;DEF&gt;</code> -- definition</li></div><a name="wp64953"> </a><div class="pSmartList1"><li><code class="cCode">&lt;LINK&gt;</code> -- link to a URL </li></div></ul></div><hr><a name="wp64954"> </a><p class="pNote">Note: An <span style="font-style: italic">inline</span> tag does not generate a line break, so a style change caused by an inline tag does not affect the flow of text on the page (although it will affect the appearance of that text). A <span style="font-style: italic">structure</span> tag, on the other hand, demarcates a new segment of text, so at a minimum it always generates a line break, in addition to other format changes.</p><hr><a name="wp64955"> </a><p class="pBody">The <code class="cCode">&lt;DEF&gt;</code> tag will be used for terms that are defined in the text. Such terms will be displayed in italics, the way they ordinarily are in a document. But using a special tag in the XML will allow an index program to find such definitions and add them to an index, along with keywords in headings. In the <span style="font-style: italic">Note</span> above, for example, the definitions of inline tags and structure tags could have been marked with <code class="cCode">&lt;DEF&gt;</code> tags, for future indexing.</p><a name="wp64956"> </a><p class="pBody">Finally, the <code class="cCode">LINK</code> tag serves two purposes. First, it will let us create a link to a URL without having to put the URL in twice -- so we can code <code class="cCode">&lt;link&gt;http//...&lt;/link&gt;</code> instead of <code class="cCode">&lt;a href=&quot;http//...&quot;&gt;http//...&lt;/a&gt;</code>. Of course, we'll also want to allow a form that looks like <code class="cCode">&lt;link target=&quot;...&quot;&gt;...name...&lt;/link&gt;</code>. That leads to the second reason for the <code class="cCode">&lt;link&gt;</code> tag--it will give us an opportunity to play with conditional expressions in XSLT.</p><hr><a name="wp64957"> </a><p class="pNote">Note: Although the article structure is exceedingly simple (consisting of only 11 tags), it raises enough interesting problems to get a good view of XSLT's basic capabilities. But we'll still leave large areas of the specification untouched. The last part of this tutorial will point out the major features we skipped.</p><hr><a name="wp64959"> </a><h3 class="pHeading2">Creating a Test Document</h3><a name="wp64960"> </a><p class="pBody">Here, you'll create a simple test document using nested <code class="cCode">&lt;SECT&gt;</code> elements, a few <code class="cCode">&lt;PARA&gt;</code> elements, a <code class="cCode">&lt;NOTE&gt;</code> element, a <code class="cCode">&lt;LINK&gt;</code>, and a <code class="cCode">&lt;LIST type=&quot;unordered&quot;&gt;</code>. The idea is to create a document with one of everything, so we can explore the more interesting translation mechanisms.</p><hr><a name="wp64961"> </a><p class="pNote">Note: The sample data described here is contained in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1.xml" target="_blank">article1.xml</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1-xml.html" target="_blank">article1-xml.html</a></code>.)</p><hr><a name="wp64962"> </a><p class="pBody">To make the test document, create a file called <code class="cCode">article.xml</code> and enter the XML data shown below.</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;&lt;PARA&gt;This section will introduce a subsection.&lt;/PARA&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;SECT&gt;The Subsection Heading&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PARA&gt;This is the text of the subsection.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/PARA&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/SECT&gt;&nbsp;&nbsp;&lt;/SECT&gt;&lt;/ARTICLE&gt;<a name="wp64963"> </a></pre></div><a name="wp64964"> </a><p class="pBody">Note that in the XML file, the subsection is totally contained within the major section. (In HTML, on the other hand, headings do not <span style="font-style: italic">contain</span> the body of a section.) The result is an outline structure that is harder to edit in plain-text form, like this, but is much easier to edit with an outline-oriented editor. </p><a name="wp64965"> </a><p class="pBody">Someday, given an tree-oriented XML editor that understands inline tags like <code class="cCode">&lt;B&gt;</code> and <code class="cCode">&lt;I&gt;</code>, it should be possible to edit an article of this kind in outline form, without requiring a complicated stylesheet. (Such an editor would allow the writer to focus on the structure of the article, leaving layout until much later in the process.) In such an editor, the article-fragment above would look something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">&lt;ARTICLE&gt; &nbsp;&nbsp;&lt;TITLE&gt;</code>A Sample Article <code class="cCodeBold">&nbsp;&nbsp;&lt;SECT&gt;</code>The First Major Section <code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;&lt;PARA&gt;</code>This section will introduce a subsection.<code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;&lt;SECT&gt;</code>The Subheading <code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PARA&gt;</code>This is the text of the subsection. Note that ...<a name="wp64966"> </a></pre></div><hr><a name="wp64967"> </a><p class="pNote">Note: At the moment, tree-structured editors exist, but they treat inline tags like <code class="cCode">&lt;B&gt;</code> and <code class="cCode">&lt;I&gt;</code> the same way that they treat other structure tags, which can make the &quot;outline&quot; a bit difficult to read.</p><hr><a name="wp64969"> </a><h3 class="pHeading2">Writing an XSLT Transform </h3><a name="wp64970"> </a><p class="pBody">In this part of the tutorial, you'll begin writing an XSLT transform that will convert the XML article and render it in HTML. </p><hr><a name="wp64971"> </a><p class="pNote">Note: The transform described in this section is contained in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1a.xsl" target="_blank">article1a.xsl</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/xslt/samples/article1a-xsl.html" target="_blank">article1a-xsl.html</a></code>.) </p><hr><a name="wp64972"> </a><p class="pBody">Start by creating a normal XML document:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<a name="wp64973"> </a></pre></div><a name="wp64974"> </a><p class="pBody">Then add the lines highlighted below to create an XSL stylesheet:

⌨️ 快捷键说明

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