XML
  Home arrow XML arrow Page 6 - XSL Basics (part 1)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
XML

XSL Basics (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2001-08-15


    Table of Contents:
  • XSL Basics (part 1)
  • A Quick History Lesson
  • Up A Tree
  • Test Drive
  • An Evening At The Moulin Rouge
  • Little Black Book

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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!

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

       

    XML ARTICLES

    - 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...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek