The Ground Cero Guide to XSL
Henrik Aasted Sorensen
<< XSLTIntroducing XPath >>
5. Basic XSLT
With that covered, let's move on the basics of XSLT.
The root-tag of an XSLT-document is stylesheet.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:add="http://www.phonedirectoy.org/XML">
    
This example shows the stylesheet tag initiating 3 namespaces: XSL, XHTML and our address-language.
XSLT uses tag-matching rules to process a document. Each rule has its own tag, which must be a direct child of the root-tag.
<xsl:template match="add:addresses">
Will match the root-element of an address-document. The contents of a template is evaluated from the point of view of the node that was matched. The whole document is evaluated from the point of view of the root node, which is why it's necessary to make a template-rule for evaluating that.
The contents of the match-attribute is actually an XPath-expression, but to keep things a bit simple we'll initially stick with only making templates that match the names of tags directly. The section on XPath will broaden the possibities.
The contents of the <xsl:template>-tag is the a combination of XSL-tags and tags from the resulting XML-language. Any non-XSL-elements will be written to the destination document. Building on the initial example, we'll make a web-presentation of the address-book. You can download an extended address-book to experiment with here.
<xsl:template match="add:addresses">
    <html>
    <head><title> My addressbook </title></head>
    <body>
    <h1> My addressbook</h1>
    </body>
    </html>
</xsl:template>
    
The result of processing this XSL-document with the address-document will be a (very simple) web-page saying "My addressbook" several times.
To add some information to the webpage, we need more XSL-tags. To process the children of the address tag we add the <xsl:apply-templates>-tag, which will call the person-template with all the person-tags.
[...]
<h1> My addressbook</h1>
<xsl:apply-templates select="add:person"/>
</body>
[...]
This requires that we make another template-tag. This one must be capable of matching the person-tag.
<xsl:template match="add:person">
</xsl:template>
The contents of this tag should print some or all of the available information contained in the person-tag, which can be done like this:
<xsl:template match="add:person">
    </b><xsl:value-of select="@name"/></b><br/>
    Phone: <xsl:value-of select="add:phone"/>
</xsl:template>
This introduced the <xsl:value-of>-tag. This tag is used for printing the text-contents of a tag or an attribute to the resulting document. Notice the @ in front of the name-attribute. This signals to the XSL-processor that we're interested in an attribute and not a child-node.
A fancy feature of the resulting XHTML-document would be to make a link to a person's e-mail-address with the <a>-tag. Remember that the <a>-tag has the attribute href which contains the target URL of the link. XML doesn't support tags with-in tags, which prevents us from doing this:
<a href="<xsl:value-of select="email"/>"> <xsl:value-of select="email"/> </a>
Instead it's necessary to use XSL-variables.
<xsl:variable name="emailaddress" select="add:email"/>
This will instantiate a variable named emailaddress which contains the node email. Displaying the variable in the middle of the <a>-tag is done like this:
<a href="mailto:{$emailaddress}"> <xsl:value-of select="add:email"/> </a>
Hopefully this is sufficient to get you started at basic XSL.
<< XSLTIntroducing XPath >>
Index