11.3 Using a Parameter with Keys

Instead of using stationary keys as in the previous two stylesheets, what if you want to vary a key? You can use parameters to do this. Once again, a parameter is a value bound to a name that you can also pass into a template or stylesheet to change the outcome of a transformation. (Parameters were discussed in Chapter 7.) The kp.xsl stylesheet uses a parameter named kp to change the result of a call to key( ):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:key name="UN" match="state" use="@cc"/> <xsl:param name="kp">af</xsl:param>     <xsl:template match="/">  <xsl:text>The member state </xsl:text>  <xsl:value-of select="key('UN', $kp)/name"/>  <xsl:text> was admitted to the UN on </xsl:text>  <xsl:value-of select="key('UN', $kp)/admitted"/>  <xsl:text>.</xsl:text> </xsl:template>     </xsl:stylesheet>

The key named UN is the same as the key with the same name in both un.xsl and keys.xsl. Immediately following that key declaration is the top-level element, param. This element binds a name to a value for later reference in the stylesheet. The default value of the parameter kp is af, the country code for Afghanistan, which is the first member state in un.xml.

Later on in the stylesheet, in the two key( ) calls, the parameter kp is referenced with $kp. Each time the stylesheet calls key( ), the reference to kp is replaced by the value af. Now you'll get a chance to apply this stylesheet in a couple of different ways to see how this parameter makes a difference.

First, apply the stylesheet to un.xml the way you normally would:

xalan un.xml kp.xsl

and you will get the following result:

The member state Afghanistan was admitted to the UN on 19 Nov. 1946.

11.3.1 Passing in a Parameter to the Stylesheet

Now, in the following command, you can pass in a new parameter value for kp into the stylesheet and produce a different outcome to the transformation:

xalan -p kp 'es' un.xml kp.xsl

The single quotes are important. With this command line, the processor passes the value es for the kp parameter into kp.xsl. The default value of kp is af, which is replaced with the value es during the transformation, giving you this somewhat different result:

The member state Spain was admitted to the UN on 14 Dec. 1955.

Select other country codes of your choice from the cc attributes in un.xml, and pass them into the stylesheet to see what happens. Instant Saxon uses a little simpler syntax on the command line for passing in parameters. Note the end of this command line:

saxon un.xml kp.xsl kp=ls

Or:

saxon un.xml kp.xsl kp="ls"

Instant Saxon associates the parameter/value pair with an equals sign (=). This command gives you results like this:

The member state Lesotho was admitted to the UN on 17 Oct. 1966.

Because the parameter kp is used by both keys, its value is a sort of cross-reference that establishes a relationship between the value of an attribute and the content of an element. In the next example, you'll see how this can be expanded to touch more than one XML document at a time.



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