SunQuest
 
       XML
  Home arrow XML arrow Page 6 - 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 
VeriSign Whitepapers 
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 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

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    XSL Basics (part 2) - Sorting Things Out


    (Page 6 of 9 )

    Now, if you've been paying attention, you'll have noticed a small problem with the preceding example. Though the XML document putatively contains a list of someone's top five movies, ranked according to the "rank" element, the stylesheet does not process this data and prints the movies in the order in which it finds them.

    It's to resolve precisely this kind of situation that XSLT also includes a powerful sorting mechanism, which can be used to rearrange the data within the document in a specific order. The <xsl:sort> instruction uses the "select" attribute to select the elements against which to sort the data, as also whether to use ascending or descending order.

    Let's rewrite the template rule above to present the top five movies in correct order:

    <xsl:template match="/top_five"> <xsl:for-each select="movie"> <xsl:sort select="rank" /> <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>
    In case I wanted to reverse the order - just to confuse readers and because I'm a nasty evil person - I could add an "order" attribute to specify the sort order:

    <xsl:template match="/top_five"> <xsl:for-each select="movie"> <xsl:sort select="rank" order="descending" /> <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>
    XSLT allows you to specify more than one <xsl:sort> instruction, so that it becomes possible to sort by more than one key - for example, rank followed by name followed by director.

    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

    BlackBerry VTS




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