SunQuest
 
       XML
  Home arrow XML arrow Page 5 - XSL Basics (part 2)
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2001-08-31

    Table of Contents:
  • XSL Basics (part 2)
  • Mercury Rising
  • The Number Game
  • A Node By Any Other Name
  • Looping The Loop
  • Sorting Things Out
  • Be Cool
  • Flavour Of The Month
  • Endgame

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XSL Basics (part 2) - Looping The Loop


    (Page 5 of 9 )

    In addition to conditional tests, XSLT also allows for a primitive looping mechanism via the instruction. If both the source tree and result tree follow a defined structure, this instruction can go a long way towards making your XSLT stylesheets more efficient and compact.

    The <xsl:for-each> instruction always comes with a "select" attribute, which is used to select a node collection for the template rule to act upon. Once the node collection has been obtained, the templates contained within the <xsl:for-each> instruction are applied to every node within the node collection.

    If this sounds confusing - the following example should make things clearer. Consider the following XML document:

    <?xml version="1.0"?> <top_five> <movie> <name>Star Wars: A New Hope</name> <cast>Mark Hamill, Carrie Fisher, Harrison Ford</cast> <director>George Lucas</director> <rank>1</rank> </movie> <movie> <name>The Patriot</name> <cast>Mel Gibson, Heath Ledger, Jason Isaacs</cast> <director>Roland Emmerich</director> <rank>5</rank> </movie> <movie> <name>The Whole Nine Yards</name> <cast>Bruce Willis, Matthew Perry</cast> <director>Jonathan Lynn</director> <rank>3</rank> </movie> <movie> <name>Gladiator</name> <cast>Russell Crowe, Connie Nielsen, Joaquin Phoenix</cast> <director>Ridley Scott</director> <rank>4</rank> </movie> <movie> <name>Unbreakable</name> <cast>Bruce Willis, Samuel L. Jackson</cast> <director>M. Night Shyamalan</director> <rank>2</rank> </movie> </top_five>
    As you can see, this is pretty clearly-structured, with every movie an independent entity within the document. This makes it perfect for a <xsl:for-each> loop.

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <basefont face="Arial" size="4" /> </head> <body> <ol> <xsl:apply-templates /> </ol> </body> </html> </xsl:template> <xsl:template match="/top_five"> <xsl:for-each select="movie"> <li><xsl:value-of select="name" /></li> <br /> <font size="2"><xsl:value-of select="cast" /> | <xsl:value-of select="director" /></font> </xsl:for-each> </xsl:template> </xsl:stylesheet>
    Now, the loop will iterate as many times as there as "movie" elements in the document, applying the template contained within its opening and closing tags on every iteration. Here's the resulting HTML output,

    <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <basefont face="Arial" size="4"> </head> <body> <ol> <li>Star Wars: A New Hope</li> <br> <font size="2">Mark Hamill, Carrie Fisher, Harrison Ford | George Lucas</font> <li>The Patriot</li> <br> <font size="2">Mel Gibson, Heath Ledger, Jason Isaacs | Roland Emmerich</font> <li>The Whole Nine Yards</li> <br> <font size="2">Bruce Willis, Matthew Perry | Jonathan Lynn</font> <li>Gladiator</li> <br> <font size="2">Russell Crowe, Connie Nielsen, Joaquin Phoenix | Ridley Scott</font> <li>Unbreakable</li> <br> <font size="2">Bruce Willis, Samuel L. Jackson | M. Night Shyamalan</font> </ol> </body> </html>


    and here's what it looks like:

    More XML Articles
    More By icarus, (c) Melonfire


     

       

    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




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