The most important thing to learn from this article is that XSLT does a
lot more than apply style. When put through an XSLT processor, the information in the XML source document will be evaluated, rearranged, then reassembled. What you end up with is not just a pretty version of the XML data rather, you get flexible source information that can be easily added to, modified, or reordered. This end product is called the Result Tree.
All this happens through a series of tests. Here's a really simple example.
<xsl:template match="course">
<p>
<xsl:value-of/>
</p>
</xsl:template>
The first thing I should explain is that tags that end with a "/" are
empty. That is, there's nothing that can occur between an opening and
closing tag of that type. A good example of this is the HTML <img> tag. Since all the information you'd need for an image resides within one tag, there's no need for a closing </img> tag. Empty tags are acceptable in well-formed XML documents, and XSLT stylesheets must be well-formed XML.
Let's get back to our example. It tells an XSLT processor that if it finds a set of <recipe> tags, it should spit out the contents and surround them with a <p> and </p>. Or, if you want to impress folks with
your XSLT knowledge, you'd say that those items were "added to the Result Tree."
This is a pretty simple test, and it's typical. The contents of the XML elements are surrounded with presentational information.
Now, let's take a look at a complete XSL stylesheet for some recipes. You might want to look at the XML source document before you continue.
Before I move on to more XSL markup, I need to stop
for a quiz. Everyone knows that in HTML the <p> is fine all on its own. So why then did I use the closing </p> tag?
Anyone? Anyone?
The answer: Without the corresponding closing tag, that section would no longer be well-formed XML, and would therefore generate a fatal error.
next page»