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

The Number Game - 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
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
 

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: