XML
  Home arrow XML arrow Page 6 - XSL Basics (part 1)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
XML

XSL Basics (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    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.

     

       

    XML ARTICLES

    - 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...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway