Home arrow XML arrow Page 2 - XSL Basics (part 2)

Mercury Rising - XML

In this concluding article, expand your XSLT vocabulary by exploring conditional constructs, loops, variables, and numbering, together with examples and illustrations of how these programming capabilities can substantially simplify your XSLT experience.

TABLE OF CONTENTS:
  1. XSL Basics (part 2)
  2. Mercury Rising
  3. The Number Game
  4. A Node By Any Other Name
  5. Looping The Loop
  6. Sorting Things Out
  7. Be Cool
  8. Flavour Of The Month
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 6
August 31, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: