XML
  Home arrow XML arrow Page 8 - 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    XSL Basics (part 2) - Flavour Of The Month


    (Page 8 of 9 )

    Just as XML has entities, pre-defined constants which can be used at different places within your document, so too does XSLT have variables. However, XSLT variables are not the same as the ones you may be used to working with in standard programming languages. XSLT variables, once defined, cannot be altered; they remain static and can merely be used at different places within the XSLT stylesheet.

    XSLT variables are defined with the <xsl:variable> instruction, and a value is attached to them either via a "select" attribute or enclosed within the opening and closing tags of the instruction; this value may be a literal, or obtained from an expression. In case a variable lacks both a "select" attribute and a content value, it is automatically assigned an empty string value.

    Variables may be defined either within or outside template rules. A variable defined outside a template rule (at the top-level of the document) is globally available, while a variable defined within a template rule is available only to descendants of that rule.

    Once defined, variables can be accessed by referencing the variable name with a preceding dollar($) symbol.

    Consider the following stylesheet, again with reference to the preceding XML data.

    <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="flavour">strawberry</xsl:variable> <xsl:template match="/"> <html> <head> <body> -- root level -- My favourite flavour is <xsl:value-of select="$flavour" /><br /> <xsl:apply-templates select="recipe/ingredients" /> <xsl:apply-templates select="recipe/process" /> -- root level -- My favourite flavour is <xsl:value-of select="$flavour" /><br /> </body> </head> </html> </xsl:template> <xsl:template match="ingredients/item"> <xsl:variable name="flavour">raspberry</xsl:variable> -- item level -- My favourite flavour is <xsl:value-of select="$flavour" /><br /> </xsl:template> <xsl:template match="process/step"> -- step level -- My favourite flavour is <xsl:value-of select="$flavour" /><br /> </xsl:template> </xsl:stylesheet>
    Here, I've first set up a global variable, $flavour, with the value "strawberry". I've displayed it in my very first template rule, which matches the document root. Since this template rule does not contain any overriding variable definition, it will use and print the value of the global variable, "strawberry".

    Then I've transferred control to the second template rule, which matches "item" elements. Over here, I've defined a new value for the variable, "raspberry". This time, the value of the global variable will be superseded by the value within the template rule, and an attempt to print $flavour will return "raspberry".

    Once that node collection is exhausted, control goes to the third template rule, which matches "step" elements. This rule does not contain any variable definition; further, it does not have access to the value defined within the second template rule, since it is not a direct descendant of that rule. Consequently, it will also use the value "strawberry".

    Here's the output:

    -- root level -- My favourite flavour is strawberry -- item level -- My favourite flavour is raspberry -- item level -- My favourite flavour is raspberry -- item level -- My favourite flavour is raspberry -- step level -- My favourite flavour is strawberry -- step level -- My favourite flavour is strawberry -- step level -- My favourite flavour is strawberry -- step level -- My favourite flavour is strawberry -- step level -- My favourite flavour is strawberry -- step level -- My favourite flavour is strawberry -- root level -- My favourite flavour is strawberry

    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