Summing Up The Tutorial - Page 9
November 2, 2001
So far, this brief tour has only given you a taste of XSL's
capabilities—yet we've already covered the features that will let
you do four-fifths of your XSLT work! We've shown you how to:
- delete elements
- rename elements
- reorder elements
- delete attributes
- rename attributes
- convert elements to attributes
- convert attributes to elements
- process elements based on an attribute's value
These are the most basic changes that you'll want to make when
converting XML documents that conform to one schema or DTD into
documents that conform to another. If data is shared between two
organizations that designed their data structures indepen-dently,
those organizations probably have many types of information in
common—after all, that's why they're sharing it. Yet, it's also
likely that they assigned different names to similar information,
or ordered their information differently, or stored extra
information that the other organization doesn't need (or hasn't
paid for!). XSLT makes most of these conversions painless and
quick.
Before moving on, let's review what the XSLT processor is doing
now that you've seen it in action a few times. Imagine that an
XSLT processor has just started process-ing the children of the
chapter element in the following document,
<book><title>Paradise Lost</title>
<chapter><title>The Whiteness of the Whale</title>
<para>He lights, if it were Land that ever burned</para>
<para>With solid, as the Lake with liquid fire</para>
</chapter>
<chapter><title>The Castaway</title>
<para>Nine times the Space that measures Day and Night</para>
<para>To mortal men, he with his horrid crew</para>
</chapter>
</book>
and it's using a stylesheet with the following two template rules
to process it:
<!-- xq37.xsl -->
<xsl:template match="title">
Title: <xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter/title">
Chapter title: <xsl:apply-templates/>
</xsl:template>
(The match pattern "chapter/title" in the second template element
shows that this template rule is for the title
elements that are children of chapter elements. The first is for
all the other title elements.) The diagram in figure
1.7 shows the steps that take place. The chapter title's content
is the only text node shown in the source tree; the rest are
omitted to simplify the diagram.
Figure 1.7 How the XSLT processor handles
an element node
- It finds the first child of the
chapter element,
a title element node.
- It checks the stylesheet for a matching template rule. It
finds two, and picks the most specific one it can find.
- Once it's found the best template rule for the node, it gets
the template rule's template to add to the result tree.
- It adds the template, which consists of a text node ("Chapter
title:") and the result of processing the template's
xsl:apply-templates element to the title element's
lone text node child: the string "The Whiteness of the Whale."
Of course, XSLT can do much more than what we've seen so far. If
you'd like more background on key XSLT techniques before you dive
into stylesheet development, the following sections of part 2,
"XSLT user's guide: How do I work with...," on page 21 of this
book are good candidates for "Advanced Beginner" topics:
- chapter 2, "XPath," on page 23
- chapter 3, "Elements and attributes," on page 47
- section 3.6, "Copying elements to the result tree," page 57
- section 5.1, "Control statements," page 110
- section 6.1, "HTML and XSLT," page 187
- section 6.5, "Non-XML output," page 202
- section 6.6, "Numbering, automatic," page 205
- section 6.9, "Valid XML output: including DOCTYPE
declarations," page 225
If your stylesheet absolutely depends on these potentially
unavailable elements, the xsl:message element can do
more than just output a message about the problem: it can abort
the processing of the source document with its
terminate attribute. (See section 5.4.1, "Runtime
messages, aborting processor execution," page 134, for more on
the use of xsl:message.)
Attribute Value Templates - Page 8
XSLT Quickly
|