7.3 Using Parameters

The following stylesheet, param.xsl, is only a little different than variable.xsl the top-level element variable is switched with a param element but what a difference that small change makes:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:output doctype-system="catalog.dtd"/> <xsl:param name="discount" select="0.10"/> <xsl:template match="catalog">  <xsl:copy>   <xsl:apply-templates select="item"/>  </xsl:copy> </xsl:template>     <xsl:template match="item">  <xsl:copy>   <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>   <xsl:copy-of select="maker|description|size|price"/>   <discount><xsl:value-of select="$discount"/></discount>   <discountPrice><xsl:value-of select="price - (price * discount)"/></discountPrice>   <xsl:copy-of select="currency"/>  </xsl:copy> </xsl:template>     </xsl:stylesheet>

This stylesheet defines a global parameter (it's global because it is defined on the top level of the stylesheet). This means that the parameter discount is available throughout the stylesheet, wherever it might be needed. Given this stylesheet, the default value of discount is 0.10, and you can change the value of discount using a mechanism provided by the XSLT processor on the command line.

This book focuses mostly on XSLT processors that have command-line interfaces. For XSLT processors written in Java and based on Sun's Java API for XML Processing (JAXP), for example, APIs provide programmatic methods for setting parameter values, namely javax.xml.transform.setParameter(String name, Object value). You will get a chance to use JAXP in a programming example in Chapter 17.


7.3.1 Passing in a Parameter with Xalan

The Xalan processor has a -p command-line option. This option allows you to associate a parameter name with a new value, which is in turn handed to the processor to produce a different result tree. To increase the amount of the discount from 10 percent (0.10) to 20 percent (0.20), enter the following command line:

xalan -i 1 -p discount '0.20' price.xml param.xsl

This transformation will produce the following result:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE catalog SYSTEM "catalog.dtd"> <catalog>  <item >   <maker>Scratchmore</maker>   <description>Wool sweater</description>   <size>L</size>   <price>120.00</price>   <discount>0.20</discount>   <discountPrice>96</discountPrice>   <currency>USD</currency>  </item> </catalog>

Notice the new content of the discount and discountPrice elements. This was a result of passing in a parameter value. You can use the -p option for as many parameters as you wish to change, provided that those parameters are defined in the stylesheet you are processing. Other processors, such as Instant Saxon, use a different syntax to pass in parameter values.

7.3.2 Passing in a Parameter with Instant Saxon

The Instant Saxon processor uses a simpler command-line syntax than Xalan's syntax. To associate a parameter name with a new value, increasing the discount from 10 percent (0.10) to 30 percent (0.30), enter this line:

saxon price.xml param.xsl discount="0.3"

The parameter discount is coupled with a new value just by using the equals sign (=). Here is the output:

<?xml version="1.0" encoding="utf-8"?>     <!DOCTYPE catalog   SYSTEM "catalog.dtd"> <catalog>    <item >       <maker>Scratchmore</maker>       <description>Wool sweater</description>       <size>L</size>       <price>120.00</price>       <discount>0.3</discount>       <discountPrice>84</discountPrice>       <currency>USD</currency>    </item> </catalog>

Other XSLT processors, such as James Clark's XT and Microsoft's MSXSL, also use this simple syntax for associating a parameter with a new value.

So far, you have seen a global variable and parameter. Next you'll see how to define and use a local one.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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