epubjs
Version:
Render ePub documents in the browser, across many devices
250 lines (237 loc) • 33.7 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Transforming Documents with XSL/XSLT</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Transforming Documents with XSL/XSLT"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-24-SECT-9"/>Transforming Documents with XSL/XSLT</h1></div></div></div><p>Earlier in this chapter, we used a <code class="literal">Transformer</code> object to copy a DOM representation
of an example back to XML text. We mentioned that we were not really
tapping the potential of the <a id="I_indexterm24_id836651" class="indexterm"/><code class="literal">Transformer</code>. Now, we’ll
give you the full story.</p><p>The <a id="I_indexterm24_id836665" class="indexterm"/><code class="literal">javax.xml.transform</code>
package is the API for using the XSL/XSLT transformation language. XSL
stands for Extensible Stylesheet Language. <a id="I_indexterm24_id836677" class="indexterm"/><a id="I_indexterm24_id836683" class="indexterm"/>Like Cascading Stylesheets (CSS) for HTML, XSL allows us to
“mark up” XML documents by adding tags that provide presentation
information. XSL Transformation (XSLT) takes this further by adding the
ability to completely restructure the XML and produce arbitrary output.
XSL and XSLT together make up their own programming language for
processing an XML document as input and producing another (usually XML)
document as output. (From here on in, we’ll refer to them collectively as
XSL.)</p><p>XSL is extremely powerful, and new applications for its use arise
every day. For example, consider a website that is frequently updated and
that must provide access to a variety of mobile devices and traditional
browsers. Rather than recreating the site for these and additional
platforms, XSL can transform the content to an appropriate format for each
platform. More generally, rendering content from XML is simply a better
way to preserve your data and keep it separate from your presentation
information. XSL can be used to render an entire website in different
styles from files containing “pure data” in XML, much like a database.
Multilingual sites also benefit from XSL to lay out text in different ways
for different audiences.</p><p><a id="I_indexterm24_id836712" class="indexterm"/> <a id="I_indexterm24_id836718" class="indexterm"/>You can probably guess the caveat that we’re going to issue:
XSL is a big topic worthy of its own books (see, for example, O’Reilly’s
<a class="ulink" href="http://shop.oreilly.com/product/9780596001438.do"><span class="emphasis"><em>Java and
XSLT</em></span></a> by Eric Burke), and we can only give you a taste
of it here. Furthermore, some people find XSL difficult to understand at
first glance because it requires thinking in terms of recursively
processing document tags. In recent years, much of the impetus behind XSL
as a way to produce web-based content has fallen away in favor of using
more JavaScript on the client. However, XSL remains a powerful way to
transform XML and is widely used in other document-oriented
applications.</p><div class="sect2" title="XSL Basics"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-24-SECT-9.1"/>XSL Basics</h2></div></div></div><p><a id="idx11221" class="indexterm"/> <a id="idx11238" class="indexterm"/>XSL is an XML-based standard, so it should come as no
surprise that the language is based on XML. An XSL <a id="I_indexterm24_id836781" class="indexterm"/><span class="emphasis"><em>stylesheet</em></span> is an XML document using
special tags defined by the XSL namespace to describe the
transformation. The most basic XSL operations involve matching parts of
the input XML document and generating output based on their contents.
One or more XSL <a id="I_indexterm24_id836792" class="indexterm"/><span class="emphasis"><em>templates</em></span> live within the stylesheet
and are called in response to tags appearing in the input. XSL is often
used in a purely input-driven way, whereas input XML tags trigger output
in the order in which they appear, using only the information they
contain. But more generally, the output can be constructed from
arbitrary parts of the input, drawing from it like a database, composing
elements and attributes. The XSLT transformation part of XSL adds things
like conditionals and iteration to this mix, which enable any kind of
output to be generated based on the input.</p><p>An XSL stylesheet contains a stylesheet tag as its root element.
By convention, the stylesheet defines a namespace prefix <code class="literal">xsl</code> for the XSL namespace. Within the
stylesheet, are one or more template tags contain a <a id="I_indexterm24_id836819" class="indexterm"/><code class="literal">match</code> attribute that
describes the element upon which they operate.</p><a id="I_24_tt1350"/><pre class="programlisting"><code class="o"><</code><code class="nl">xsl:</code><code class="n">stylesheet</code>
<code class="nl">xmlns:</code><code class="n">xsl</code><code class="o">=</code><code class="s">"http://www.w3.org/1999/XSL/Transform"</code> <code class="n">version</code><code class="o">=</code><code class="s">"1.0"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xsl:</code><code class="n">template</code> <code class="n">match</code><code class="o">=</code><code class="s">"/"</code><code class="o">></code>
<code class="n">I</code> <code class="n">found</code> <code class="n">the</code> <code class="n">root</code> <code class="n">of</code> <code class="n">the</code> <code class="n">document</code><code class="o">!</code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">template</code><code class="o">></code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">stylesheet</code><code class="o">></code></pre><p>When a template matches an element, it has an opportunity to
handle all the children of the element. The simple stylesheet shown here
has one template that matches the root of the input document and simply
outputs some plain text. By default, input not matched is simply copied
to the output with its tags stripped (HTML convention). But here we
match the root so we consume the entire input and nothing but our
message appears on the output.</p><p>The <a id="I_indexterm24_id836850" class="indexterm"/><code class="literal">match</code> attribute can
refer to elements using the XPath notation that we described earlier.
This is a hierarchical path starting with the root element. For example,
<code class="literal">match="/inventory/animal"</code> would match
only the <code class="literal">animal</code> elements from our
<span class="emphasis"><em>zooinventory.xml</em></span> file. In XSL, the path may be
absolute (starting with “/”) or relative, in which case, the template
detects whenever that element appears in any subcontext (equivalent to
“//” in XPath).</p><p>Within the template, we can put whatever we want as long as it is
well-formed XML (if not, we can use a CDATA section or XInclude). But
the real power comes when we use parts of the input to generate output.
The XSL <a id="I_indexterm24_id836885" class="indexterm"/><code class="literal">value-of</code> tag is used to
output the content or child of the element. For example, the following
template would match an <code class="literal">animal</code>
element and output the value of its <code class="literal">Name</code> child element:</p><a id="I_24_tt1351"/><pre class="programlisting"><code class="o"><</code><code class="nl">xsl:</code><code class="n">template</code> <code class="n">match</code><code class="o">=</code><code class="s">"animal"</code><code class="o">></code>
<code class="nl">Name:</code> <code class="o"><</code><code class="nl">xsl:</code><code class="n">value</code><code class="o">-</code><code class="n">of</code> <code class="n">select</code><code class="o">=</code><code class="s">"name"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">template</code><code class="o">></code></pre><p>The <a id="I_indexterm24_id836919" class="indexterm"/><code class="literal">select</code> attribute uses
an XPath expression relative to the current node. In this case, we tell
it to print the value of the <code class="literal">name</code>
element within <code class="literal">animal</code>. We could have
used a relative path to a more deeply nested element within <code class="literal">animal</code> or even an absolute path to another
part of the document. To refer to the “current” element (in this case,
the <code class="literal">animal</code> element itself), a
<code class="literal">select</code> expression can use “<code class="literal">.</code>” as the path. The <code class="literal">select</code> expression can also retrieve attributes
from the elements that it references.</p><p>If we try to add the <code class="literal">animal</code>
template to our simple example, it won’t generate any output. What’s the
problem? If you recall, we said that a template matching an element has
the opportunity to process all its children. We already have a template
matching the root (“/”), so it is consuming all the input. The answer to
our dilemma—and this is where things get a little tricky—is to delegate
the matching to other templates using the <a id="I_indexterm24_id836986" class="indexterm"/><code class="literal">apply-templates</code> tag.
The following example correctly prints the names of all the animals in
our document:</p><a id="I_24_tt1352"/><pre class="programlisting"><code class="o"><</code><code class="nl">xsl:</code><code class="n">stylesheet</code>
<code class="nl">xmlns:</code><code class="n">xsl</code><code class="o">=</code><code class="s">"http://www.w3.org/1999/XSL/</code>
<code class="s"> Transform"</code> <code class="n">version</code><code class="o">=</code><code class="s">"1.0"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xsl:</code><code class="n">template</code> <code class="n">match</code><code class="o">=</code><code class="s">"/"</code><code class="o">></code>
<code class="n">Found</code> <code class="n">the</code> <code class="n">root</code><code class="o">!</code>
<code class="o"><</code><code class="nl">xsl:</code><code class="n">apply</code><code class="o">-</code><code class="n">templates</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">template</code><code class="o">></code>
<code class="o"><</code><code class="nl">xsl:</code><code class="n">template</code> <code class="n">match</code><code class="o">=</code><code class="s">"animal"</code><code class="o">></code>
<code class="nl">Name:</code> <code class="o"><</code><code class="nl">xsl:</code><code class="n">value</code><code class="o">-</code><code class="n">of</code> <code class="n">select</code><code class="o">=</code><code class="s">"name"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">template</code><code class="o">></code>
<code class="o"></</code><code class="nl">xsl:</code><code class="n">stylesheet</code><code class="o">></code></pre><p>We still have the opportunity to add output before and after the
<code class="literal">apply-templates</code> tag. But upon
invoking it, the template matching continues from the current node.
Next, we’ll use what we have so far and add a few bells and
whistles.<a id="I_indexterm24_id837020" class="indexterm"/><a id="I_indexterm24_id837027" class="indexterm"/></p></div><div class="sect2" title="Transforming the Zoo Inventory"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-24-SECT-9.2"/>Transforming the Zoo Inventory</h2></div></div></div><p>Your boss just called, and it’s now imperative that your zoo
clients have access to the zoo inventory through the Web, today! After
reading <a class="xref" href="ch15.html" title="Chapter 15. Web Applications and Web Services">Chapter 15</a>, you should be thoroughly
prepared to build a nice “zoo app.” Let’s get started by creating an XSL
stylesheet to turn our <span class="emphasis"><em>zooinventory.xml</em></span> into
HTML:</p><a id="I_24_tt1353"/><pre class="programlisting"><code class="o"><?</code><code class="n">xml</code> <code class="n">version</code><code class="o">=</code><code class="s">"1.0"</code> <code class="n">encoding</code><code class="o">=</code><code class="s">"UTF-8"</code><code class="o">?></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">schema</code> <code class="nl">xmlns:</code><code class="n">xs</code><code class="o">=</code><code class="s">"http://www.w3.org/2001/XMLSchema"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"inventory"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">maxOccurs</code><code class="o">=</code><code class="s">"unbounded"</code> <code class="n">ref</code><code class="o">=</code><code class="s">"animal"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">element</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"name"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"animal"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">ref</code><code class="o">=</code><code class="s">"name"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"species"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"habitat"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">choice</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"food"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">ref</code><code class="o">=</code><code class="s">"foodRecipe"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">choice</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"temperament"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"weight"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:double"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">attribute</code> <code class="n">name</code><code class="o">=</code><code class="s">"animalClass"</code> <code class="k">default</code><code class="o">=</code><code class="s">"unknown"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">simpleType</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">restriction</code> <code class="n">base</code><code class="o">=</code><code class="s">"xs:token"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">enumeration</code> <code class="n">value</code><code class="o">=</code><code class="s">"unknown"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">enumeration</code> <code class="n">value</code><code class="o">=</code><code class="s">"mammal"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">enumeration</code> <code class="n">value</code><code class="o">=</code><code class="s">"reptile"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">enumeration</code> <code class="n">value</code><code class="o">=</code><code class="s">"bird"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">restriction</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">simpleType</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">attribute</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">element</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">name</code><code class="o">=</code><code class="s">"foodRecipe"</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">ref</code><code class="o">=</code><code class="s">"name"</code><code class="o">/></code>
<code class="o"><</code><code class="nl">xs:</code><code class="n">element</code> <code class="n">maxOccurs</code><code class="o">=</code><code class="s">"unbounded"</code> <code class="n">name</code><code class="o">=</code><code class="s">"ingredient"</code> <code class="n">type</code><code class="o">=</code><code class="s">"xs:string"</code><code class="o">/></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">sequence</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">complexType</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">element</code><code class="o">></code>
<code class="o"></</code><code class="nl">xs:</code><code class="n">schema</code><code class="o">></code></pre><p>The stylesheet contains three templates. The first matches
<code class="literal">/inventory</code> and outputs the beginning
of our HTML document (the header) along with the start of a table for
the animals. It then delegates using <a id="I_indexterm24_id837092" class="indexterm"/><code class="literal">apply-templates</code> before
closing the table and adding the HTML footer. The next template matches
<code class="literal">inventory/animal</code>, printing one row of
an HTML table for each animal. Although there are no other <code class="literal">animal</code> elements in the document, it still
doesn’t hurt to specify that we will match an <code class="literal">animal</code> only in the context of an <code class="literal">inventory</code>, because, in this case, we are
relying on <code class="literal">inventory</code> to start and end
our table. (This template makes sense only in the context of an <code class="literal">inventory</code>.) Finally, we provide a template
that matches <code class="literal">foodRecipe</code> and prints a
small, nested table for that information. <code class="literal">foodRecipe</code> makes use of the "<code class="literal">for-each</code>" operation to loop over child nodes
with a <a id="I_indexterm24_id837156" class="indexterm"/><code class="literal">select</code> specifying that
we are only interested in <code class="literal">ingredient</code>
children. For each <code class="literal">ingredient</code>, we
output its value in a row.</p><p>There is one more thing to note in the <code class="literal">animal</code> template. Our <code class="literal">apply-templates</code> element has a <code class="literal">select</code> attribute that limits the elements
affected. In this case, we are using the "<code class="literal">|</code>" regular expression-like syntax to say that
we want to apply templates for only the <code class="literal">food</code><span class="emphasis"><em>or</em></span><code class="literal">foodRecipe</code> child elements. Why do we do this?
Because we didn’t match the root of the document (only <code class="literal">inventory</code>), we still have the default
stylesheet behavior of outputting the plain text of nodes that aren’t
matched anywhere else. We take advantage of this behavior to print the
text of the <code class="literal">food</code> element. But we
don’t want to output the text of all of the other elements of <code class="literal">animal</code> that we’ve already printed explicitly,
so we process only the <code class="literal">food</code> and
<code class="literal">foodRecipe</code> elements. Alternatively,
we could have been more verbose, adding a template matching the root and
another template just for the <code class="literal">food</code>
element. That would also mean that new tags added to our XML would, by
default, be ignored and not change the output. This may or may not be
the behavior you want, and there are other options as well. As with all
powerful tools, there is usually more than one way to do
something.</p></div><div class="sect2" title="XSLTransform"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-24-SECT-9.3"/>XSLTransform</h2></div></div></div><p><a id="idx11222" class="indexterm"/> <a id="idx11239" class="indexterm"/>Now that we have a stylesheet, let’s apply it! The
following simple program, <code class="literal">XSLTransform</code>, uses the <code class="literal">javax.xml.transform</code> package to apply the
stylesheet to an XML document and print the result. You can use it to
experiment with XSL and our example code.</p><a id="I_24_tt1354"/><pre class="programlisting"> <code class="kn">import</code> <code class="nn">javax.xml.transform.*</code><code class="o">;</code>
<code class="kn">import</code> <code class="nn">javax.xml.transform.stream.*</code><code class="o">;</code>
<code class="kd">public</code> <code class="kd">class</code> <code class="nc">XSLTransform</code>
<code class="o">{</code>
<code class="kd">public</code> <code class="kd">static</code> <code class="kt">void</code> <code class="nf">main</code><code class="o">(</code> <code class="n">String</code> <code class="o">[]</code> <code class="n">args</code> <code class="o">)</code> <code class="kd">throws</code> <code class="n">Exception</code> <code class="o">{</code>
<code class="k">if</code> <code class="o">(</code> <code class="n">args</code><code class="o">.</code><code class="na">length</code> <code class="o"><</code> <code class="mi">2</code> <code class="o">||</code> <code class="o">!</code><code class="n">args</code><code class="o">[</code><code class="mi">0</code><code class="o">].</code><code class="na">endsWith</code><code class="o">(</code><code class="s">".xsl"</code><code class="o">)</code> <code class="o">)</code> <code class="o">{</code>
<code class="n">System</code><code class="o">.</code><code class="na">err</code><code class="o">.</code><code class="na">println</code><code class="o">(</code><code class="s">"usage: XSLTransform file.xsl file.xml"</code><code class="o">);</code>
<code class="n">System</code><code class="o">.</code><code class="na">exit</code><code class="o">(</code><code class="mi">1</code><code class="o">);</code>
<code class="o">}</code>
<code class="n">String</code> <code class="n">xslFile</code> <code class="o">=</code> <code class="n">args</code><code class="o">[</code><code class="mi">0</code><code class="o">],</code> <code class="n">xmlFile</code> <code class="o">=</code> <code class="n">args</code><code class="o">[</code><code class="mi">1</code><code class="o">];</code>
<code class="n">TransformerFactory</code> <code class="n">factory</code> <code class="o">=</code> <code class="n">TransformerFactory</code><code class="o">.</code><code class="na">newInstance</code><code class="o">();</code>
<code class="n">Transformer</code> <code class="n">transformer</code> <code class="o">=</code>
<code class="n">factory</code><code class="o">.</code><code class="na">newTransformer</code><code class="o">(</code> <code class="k">new</code> <code class="n">StreamSource</code><code class="o">(</code> <code class="n">xslFile</code> <code class="o">)</code> <code class="o">);</code>
<code class="n">StreamSource</code> <code class="n">xmlsource</code> <code class="o">=</code> <code class="k">new</code> <code class="n">StreamSource</code><code class="o">(</code> <code class="n">xmlFile</code> <code class="o">);</code>
<code class="n">StreamResult</code> <code class="n">output</code> <code class="o">=</code> <code class="k">new</code> <code class="n">StreamResult</code><code class="o">(</code> <code class="n">System</code><code class="o">.</code><code class="na">out</code> <code class="o">);</code>
<code class="n">transformer</code><code class="o">.</code><code class="na">transform</code><code class="o">(</code> <code class="n">xmlsource</code><code class="o">,</code> <code class="n">output</code> <code class="o">);</code>
<code class="o">}</code>
<code class="o">}</code></pre><p>Run <code class="literal">XSLTransform</code>, passing the
XSL stylesheet and XML input, as in the following command:</p><a id="I_24_tt1355"/><pre class="programlisting"><code class="o">%</code> <strong class="userinput"><code><code class="n">java</code> <code class="n">XSLTransform</code> <code class="n">zooinventory</code><code class="o">.</code><code class="na">xsl</code> <code class="n">zooinventory</code><code class="o">.</code><code class="na">xml</code> <code class="o">></code> <code class="n">zooinventory</code><code class="o">.</code><code class="na">html</code></code></strong></pre><p>The output should look like <a class="xref" href="ch24s10.html#learnjava3-CHP-24-FIG-2" title="Figure 24-2. Image of the zoo inventory table">Figure 24-2</a>.</p><div class="figure"><a id="learnjava3-CHP-24-FIG-2"/><div class="figure-contents"><div class="mediaobject"><a id="I_24_tt1356"/><img src="httpatomoreillycomsourceoreillyimages1707717.png" alt="Image of the zoo inventory table"/></div></div><p class="title">Figure 24-2. Image of the zoo inventory table</p></div><p>Constructing the transform is a similar process to that of getting
a SAX or DOM parser. The difference from our earlier use of the <code class="literal">TransformerFactory</code> is that this time, we construct the transformer, passing it
the XSL stylesheet source. The resulting <code class="literal">Transformer</code>
object is then a dedicated machine that knows how to take input
XML and generate output according to its rules.</p><p>One important thing to note about <code class="literal">XSLTransform</code> is that it is not guaranteed
thread-safe. In our example, we run the transform only once. If you are
planning to run the same transform many times, you should take the
additional step of getting a <code class="literal">Templates</code> object for the transform first, then
using it to create <code class="literal">Transformer</code>s.</p><a id="I_24_tt1357"/><pre class="programlisting"><code class="n">Templates</code> <code class="n">templates</code> <code class="o">=</code>
<code class="n">factory</code><code class="o">.</code><code class="na">newTemplates</code><code class="o">(</code> <code class="k">new</code> <code class="n">StreamSource</code><code class="o">(</code> <code class="n">args</code><code class="o">[</code><code class="mi">0</code><code class="o">]</code> <code class="o">)</code> <code class="o">);</code>
<code class="n">Transformer</code> <code class="n">transformer</code> <code class="o">=</code> <code class="n">templates</code><code class="o">.</code><code class="na">newTransformer</code><code class="o">();</code></pre><p>The <code class="literal">Templates</code> object holds the
parsed representation of the stylesheet in a compiled form and makes the
process of getting a new <code class="literal">Transformer</code>
much faster. The transformers themselves may also be more highly
optimized in this case. The XSL transformer actually generates bytecode
for very efficient “translets” that implement the transform. This means
that instead of the transformer reading a description of what to do with
your XML, it actually produces a small compiled program to execute the
instructions!<a id="I_indexterm24_id837457" class="indexterm"/><a id="I_indexterm24_id837464" class="indexterm"/></p></div><div class="sect2" title="XSL in the Browser"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-24-SECT-9.4"/>XSL in the Browser</h2></div></div></div><p><a id="I_indexterm24_id837478" class="indexterm"/> <a id="I_indexterm24_id837490" class="indexterm"/>With our <code class="literal">XSLTransform</code>
example, you can see how you’d go about rendering XML to an HTML
document on the server side. But as mentioned in the introduction,
modern web browsers support XSL on the client side as well. Browsers can
automatically download an XSL stylesheet and use it to transform an XML
document. To make this happen, just add a standard XSL stylesheet
reference in your XML. You can put the stylesheet directive next to your
DOCTYPE declaration in the <span class="emphasis"><em>zooinventory.xml</em></span>
file:</p><a id="I_24_tt1358"/><pre class="programlisting"><code class="o"><?</code><code class="n">xml</code><code class="o">-</code><code class="n">stylesheet</code> <code class="n">type</code><code class="o">=</code><code class="s">"text/xsl"</code> <code class="n">href</code><code class="o">=</code><code class="s">"zooinventory.xsl"</code><code class="o">?></code></pre><p>As long as the <span class="emphasis"><em>zooinventory.xsl</em></span> file is
available at the same location (base URL) as the
<span class="emphasis"><em>zooinventory.xml</em></span> file, the browser will use it to
render HTML on the client side.</p></div></div></body></html>