Adding a Calculated Value

 
xslt for dummies
Chapter 6 - We Want Results!
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

If you have used Excel or any database like Access, chances are you have used calculated values before. A calculated value is a number that is produced through some sort of calculation. (Heres where all that math you learned in school kicks in.) Placing a calculation within an XPath expression, you can create a calculated value for your results document.

Suppose, for example, that I want to add a new child element for the coffee element that provides a discounted price for the coffee. In this case, Id like to add a new discountprice element that is 20 percent off the price element value. The stylesheet is set up as follows :

 <!-- coffee-addelement_calc.xsl --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Add new calculated element --> <xsl:template match="coffee"> <coffee> <xsl:apply-templates/> <discountprice><xsl:value-of select="format-number(price*.8, '##.##')"/></discountprice> </coffee> </xsl:template> <!-- Copy everything else over --> <xsl:template match="@*node()"> <xsl:copy> <xsl:apply-templates select="@*node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> 

The first template rule I created uses coffee as the match pattern to return the coffee element node set. Literal text is used to define the discountprice tags. Because the value of the element needs to be generated during processing time, I need to use an XPath expression to generate the value. An xsl:value-of instruction comes in handy at this point, because you can use it to convert the result of an XPath expression into a string.

To get the value Im looking for, I can use a simple expression price * .8 . However, because this value needs to represent a currency amount, I need to round the value to two decimal places. To do that, XPath conveniently has a built-in function called format-number that allows you to define how you want to format a number output. I talk more about format-number in Chapter 11, but the important thing to know for this example is that the function has two parameters: The first is the number to format ( price*.8 ) and the second is the format picture ( ##.## , where # represents a numeric digit).

The results from the transformation are as follows:

 <?xml version="1.0" encoding="utf-8"?> <coffees> <region name="Latin America"> <coffee> <taste>Curiously Mild And Bland</taste> <price>11.99</price> <availability>Year-round</availability> <bestwith>Breakfast</bestwith> <discountprice>9.59</discountprice> </coffee> <coffee> <taste>Exotic and Untamed</taste> <price>12.99</price> <availability>Year-round</availability> <bestwith>Dessert</bestwith> <discountprice>10.39</discountprice> </coffee> </region> <region name="Africa"> <coffee> <taste>Exotic and Untamed</taste> <price>14.99</price> <availability>Limited</availability> <bestwith>Chocolate</bestwith> <discountprice>11.99</discountprice> </coffee> <coffee> <taste>Solid Yet Understated</taste> <price>9.99</price> <availability>Year-round</availability> <bestwith>Elephant Ears</bestwith> <discountprice>7.99</discountprice> </coffee> </region> </coffees> 
  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net