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  
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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    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:
      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 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

    - 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek