XML
  Home arrow XML arrow Page 2 - 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) - Mercury Rising


    (Page 2 of 9 )

    XSLT allows you to perform basic conditional tests within a template rule. The simplest form of conditional instruction is the instruction, which looks something like this

    <xsl:if test="condition"> do this! </xsl:if>
    The "condition" here is a conditional expression, which evaluates to either true or false. If the statement evaluates to true, the template is used; if not, it is ignored by the processor.

    Consider the following XML document:

    <?xml version="1.0"?> <inventory> <item> <name>Brand Alpha</name> <rating>7</rating> </item> <item> <name>Brand Beta</name> <rating>9</rating> </item> <item> <name>Brand Gamma</name> <rating>2</rating> </item> </inventory>
    Here's a stylesheet to display these items as a list. Note how a conditional test has been used to display an asterisk next to items with a high rating.

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/inventory"> <html> <head> <basefont face="Arial" size="2"/> </head> <body> <ul> <xsl:apply-templates /> </ul> </body> </html> </xsl:template> <xsl:template match="item"> <li><xsl:value-of select="name" /></li> <xsl:if test="rating > 5"> * </xsl:if> </xsl:template> </xsl:stylesheet>
    Here's what it looks like:



    Here are a couple more examples of how this text can be used. The first template rule places the word "and" before printing the last item in the selected node collection, while the second displays a warning based on the value of a node.


    <xsl:template match="person"> <xsl:if test="position() = last()"> and </xsl:if> <xsl:value-of select="." /> </xsl:template> <xsl:template match="/"> <xsl:if test="//temp > 500"> <font size="+2" color="red">Warning! Temperature above acceptable limits!</font> </xsl:if> </xsl:template>

    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