XML
  Home arrow XML arrow Page 3 - 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&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    XSL Basics (part 2) - The Number Game


    (Page 3 of 9 )

    In case you find the "if" test a little too primitive for your needs, XSLT also offers the instruction. This is a lot like the "switch/case" statements you may be familiar with from PHP or JSP, and it looks like this:

    <xsl:choose> <xsl:when test="first_condition_is_true"> do this! </xsl:when> <xsl:when test="second_condition_is_true "> do this! </xsl:when>

    <xsl:when test="third_condition_is_true "> do this! </xsl:when> ... and so on ... <xsl:otherwise> all tests failed, do this! </xsl:otherwise>

    </xsl:choose>
    An example might help to make this clearer. Take a look at this XML document, which contains original and current prices for some fictitious stocks.

    <?xml version="1.0"?> <portfolio> <stock> <symbol>HHDT</symbol> <oprice>100</oprice> <cprice>25</cprice> </stock>

    <stock> <symbol>DFKS</symbol> <oprice>250</oprice> <cprice>150</cprice> </stock> <stock> <symbol>NHSJ</symbol> <oprice>20</oprice> <cprice>1000</cprice> </stock> <stock> <symbol>DJSK</symbol> <oprice>425</oprice> <cprice>2</cprice> </stock> <stock> <symbol>MDSH</symbol> <oprice>50</oprice> <cprice>50</cprice> </stock> <stock> <symbol>TRVB</symbol> <oprice>90</oprice> <cprice>86</cprice> </stock> </portfolio>
    Now, let's suppose I wanted to find out which of these stocks had risen in value, which had decreased, and which had remained static. I could use an XSLT stylesheet to calculate the change in value and display an appropriate message - maybe with a colour code - next to each stock in the list.

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/portfolio"> <html> <head> <basefont face="Arial" size="2"/> </head> <body> <h3>Portfolio At A Glance:</h3> <ul> <xsl:apply-templates /> </ul> </body> </html> </xsl:template> <xsl:template match="stock"> <xsl:choose> <!-- if price has gone up --> <xsl:when test="(cprice - oprice) > 0"> <li><font color="blue"><xsl:value-of select="symbol" /></font> (up by <xsl:value-of select="round(((cprice - oprice) * 100 div oprice))" />%)</li> </xsl:when> <!-- if price has gone down --> <xsl:when test="(cprice - oprice) < 0"> <li><font color="red"><xsl:value-of select="symbol" /></font> (down by <xsl:value-of select="round(((cprice - oprice) * 100 div oprice) * -1)" />%)</li> </xsl:when> <!-- no change --> <xsl:otherwise> <li><xsl:value-of select="symbol" /> (no change)</li> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
    Here's what it looks like:



    As you can see, the <xsl:choose> conditional statement has been used to find out whether a stock has increased or decreased in value, with a template generated appropriately. The calculations you see within the template rule are all used to display the change in value in percentage terms, and again illustrate how XSLT can be used to combine disparate bits of information from the source tree to create a new and different view in the result tree.

    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