XML
  Home arrow XML arrow Page 7 - 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 
Moblin 
JMSL Numerical Library 
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


    XSL Basics (part 2) - Be Cool


    (Page 7 of 9 )

    The example you just saw used an ordered list to display the items in correct numeric order. Now, while this is fine and dandy so long as you're only outputting HTML, what happens if you suddenly need to output the same data as ASCII text, which doesn't come with a convenient auto-numbering feature?

    Well, have no fear - XSLT comes with its own built-in numbering system, accessed via the <xsl:number> instruction. This construct comes in very handy when you need to print a series of sequential numbers, or print numbers in a style different from the standard decimal system.

    Let's take a look at how it works, by using the following XML document as source data:

    <?xml version="1.0"?> <bookstore> <title>Be Cool</title> <title>Mystic River</title> <title>Hit List</title> <title>Silent Joe</title> <title>The Travel Detective</title> </bookstore>
    And here's the XSLT stylesheet:

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="title"> <xsl:number value="position()"/> <xsl:value-of select="." /> </xsl:template> </xsl:stylesheet>
    And the output would be:

    1Be Cool 2Mystic River 3Hit List 4Silent Joe 5The Travel Detective
    In this case, the position of each node in the collection is used as the number.

    In case the number format doesn't work for you, you can alter it with the "format" attribute - try the format "I" for Roman numerals, or "a" for alphabetic numbering. The rule

    <xsl:template match="title"> <xsl:number value="position()" format="I. "/> <xsl:value-of select="." /> </xsl:template>
    returns

    I. Be Cool II. Mystic River III. Hit List IV. Silent Joe V. The Travel Detective


    In addition to this simple numbering mechanism, XSLT also allows you to count and display specific types of nodes within the document. For example, if I needed to display a number next to each of the steps in the process below,

    <?xml version="1.0"?> <recipe> <ingredients> <item>Boneless chicken breasts</item> <item>Chopped onions</item> <item>Ginger</item> </ingredients> <process> <step>Cut chicken into cubes, wash and apply lime juice and salt</step> <step>Add ginger, garlic, chili, coriander and lime juice in a separate bowl</step> <step>Mix well, and add chicken to marinate for 3-4 hours</step> <step>Place chicken pieces on skewers and barbeque</step> <step>Remove, apply butter, and barbeque again until meat is tender</step> <step>Garnish with lemon and chopped onions</step> </process> </recipe>
    I could use the following stylesheet.

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> Preparation: <xsl:apply-templates select="recipe/process" /> </xsl:template> <xsl:template match="process/step"> <xsl:number count="step" format="a. " /> <xsl:value-of select="." /> </xsl:template> </xsl:stylesheet>
    Here, the second template rule specifies that only "step" elements are to be counted and numbered. Note, however, that this numbering mechanism only works on nodes at the same level in the node tree - if you'd like to include nodes at other levels, there's an additional "level" attribute you need to use.

    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 3 hosted by Hostway