Home arrow XML arrow Page 6 - XSL Basics (part 1)

Little Black Book - XML

With a sound foundation in XML theory behind you, it's now time to address the other half of the jigsaw - actually converting all that marked-up data into something useful. This first article in a two-part series examines the need, rationale and basic concepts of XSLT, the Extensible Stylesheet Language for Transformations, with sample code and examples.

TABLE OF CONTENTS:
  1. XSL Basics (part 1)
  2. A Quick History Lesson
  3. Up A Tree
  4. Test Drive
  5. An Evening At The Moulin Rouge
  6. Little Black Book
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 12
August 15, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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!

 
 
>>> More XML Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: