XSL Basics (part 1) - Little Black Book (Page 6 of 6 )
One more example, this one demonstrating how powerful this ability to recursively apply templates is. Consider the following example:
<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="address.xsl"?>
<addressbook>
<record>
<name>John
Smith</name>
<street>24, Main Street</street>
<city>Poodle Springs</city>
<zip>16628</zip>
<country>USA</country>
</record>
<record>
<name>Sherlock
Holmes</name>
<street>122B, Baker Street</street>
<city>London</city>
<zip>12367</zip>
<country>United
Kingdom</country>
</record>
<record>
<name>Jane Doe</name>
<street>64
Fedwikstrasse</street>
<city>Antwerp</city>
<zip>848222</zip>
<country>Brussels</country>
</record>
</addressbook>
Now, since this data follows a very simple structure, and moreover I'm not very concerned about the order in which the various records appear, I can format it and present it as HTML with just two XSLT template rules:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template
match="/addressbook">
<html>
<head>
<basefont face="Arial" size="2"/>
</head>
<body>
<h1>My
Address Book</h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template
match="record">
<b><xsl:value-of select="name" /></b>
<br />
<xsl:value-of
select="street" />
<br />
<xsl:value-of select="city" /> - <xsl:value-of
select="zip" />
<br />
<xsl:value-of select="country" />
<p />
</xsl:template>
</xsl:stylesheet>
The first rule locates the document element and places the standard HTML headers and footers in the corresponding positions in the result tree. Next, the
<xsl:apply-templates />
instruction processes all the children of this node - in this case, these are
all "record" elements, for which there is a corresponding template rule. Each time a record is located via the "record" element, the template rule is invoked and a new fragment added to the result tree. At the end of the process, a composite tree is built out of all the different chunks - and it looks like this:
<html>
<head>
<basefont face="Arial" size="2">
</head>
<body>
<h1>My
Address Book</h1>
<b>John Smith</b><br>24, Main Street<br>Poodle
Springs - 16628<br>USA<p></p>
<b>Sherlock Holmes</b><br>122B,
Baker Street<br>London - 12367<br>United
Kingdom<p></p>
<b>Jane
Doe</b><br>64 Fedwikstrasse<br>Antwerp - 848222<br>Brussels<p></p>
</body>
</html>
And that just about covers the essential concepts behind XSL transformations. In the second part of this article, I will be looking at a few of XSLT's more advanced constructs, demonstrating how to add loops and conditional tests to your XSLT templates.
Note: All examples in this article have been tested on Microsoft Internet Explorer 5.5 and Saxon 6.4.3. Examples are illustrative only, and are not meant for a production environment. YMMV!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |