A Simple Example: Hello World

I think the best way to teach a programming language is to start with a very simple, contrived example. So, here is an XSLT version of Hello World.

Stylesheet (HelloWorld.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <HelloWorld>Howdy!</HelloWorld>
  </xsl:template>
</xsl:stylesheet>

Here is our source document.

Source (Goodbye.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Goodbye>
  you all
</Goodbye>

We can run the source document through an XSLT processor using our HelloWorld.xsl stylesheet and produce the following result.

Result (HelloWorld.xml)
<?xml version="1.0" encoding="UTF-8"?>
<HelloWorld>
  Howdy!
</HelloWorld>

In fact, we can process any XML document using this stylesheet and produce exactly this same result. Let's look at the major parts of this stylesheet and understand why.

The first thing to note is that the stylesheet is itself a well-formed XML document. The root xsl:stylesheet Element declares the XSLT namespace and uses xsl: as a namespace prefix. One of the restrictions on using XSLT is that all the XSLT Elements and Attributes in a stylesheet must be qualified with a namespace prefix that resolves to the XSLT namespace. You need to know that xsl:transform can be used as a synonym for xsl:stylesheet. That the Recommendation allows either indicates to me some serious philosophical differences in the work group that wrote it. So serious, in fact, that rather than achieving consensus on one name over the other they took the very unusual step of punting and allowing either. However, it takes only a cursory reading of the Recommendation to perceive a preference for stylesheet over transform, so that's what I use.

In our example there is only one Element in the body of the stylesheet document, the xsl:template Element. To aid in clarity, I am going to use in this chapter's text the convention of using the xsl: prefix on XSLT Element names. However, there are times when the generic “template” by itself rather than “xsl:template” reads better, so I will use it too.

The xsl:template Element defines what is called a template rule. It is very aptly named because the contents of the template Element function as a template (or pattern or cookie cutter if you prefer) for a portion of the result tree. The analogy of using a template in manufacturing to stamp out several copies of a part is not quite so evident in this example because we just produce one fragment of the result tree. In our example the content of the template Element is the entire body of the result tree document. This cookie cutter aspect will become more obvious as we look at other examples.

The rule aspect of the template Element relates to the conditions under which the template is invoked or applied. These are determined by various Attributes of xsl:template or by the contents of another xsl:template. In our example the rule is defined by the presence of the match Attribute. The value of the match Attribute is a pattern, based on a subset of XPath, that identifies a particular set of nodes in the source tree. Such a set is called a node-set. An XSLT processor basically functions by starting with the root of the source tree and doing a preorder traversal of the tree (top down, left to right). When it finds a node in a node-set that matches the pattern of a template's match Attribute, the contents of the xsl:template are written to the result tree. Any XSLT Elements in the body of the template Element are processed before writing the contents to the result tree. Since there are various means by which these child Elements may invoke other templates, this entire process can be recursive. In our example the value of the match Attribute is "/", a single forward slash. This is a special abbreviation for the source tree's root.

So, now that we know what the pieces are we can understand how this stylesheet works. The XSLT processor starts at the root of the source tree and sees if there is a template rule that matches it. Our xsl:template has a match Attribute value indicating the source tree root, so the processor examines the contents. There are no Elements from the XSLT namespace, so it writes the entire contents to the result tree. There are no other templates, so it terminates. Now, since all we are matching on is the source tree root, it should now be fairly obvious why we can use any XML document as a source and produce our Hello World document as the result.

Those are the bare-bones basics. Let's look at another example before we start digging into more details and concepts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset