Chapter 16 - Extending XSLT | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
Extension elements expand the xsl: built-in element set by providing new instructions inside your stylesheet. Take, for example, SAXONs while extension. I can use this element to add true conditional looping inside a stylesheet. Before using an extension element like the while extension in my stylesheet, I must first declare the extension namespace in the xsl:stylesheet element. All SAXON extensions use the following namespace declaration: xmlns:saxon="http://icl.com/saxon" Tip Although you can use any prefix, I recommend you stick with saxon to conform to the standards of the processor vendor and to minimize any possible confusion. Second, add the extension-element-prefixes attribute to the xsl: stylesheet element, using saxon as the attribute value. This attribute tells the processing engine what namespace prefixes are reserved for extensions: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon"> Inside a template rule, the saxon:while element executes a series of instructions so long as the test pattern returns true . In this simple example, I have test evaluate the value of a variable. However, because the value of an XSLT variable normally doesnt change, I use a second SAXON extension element, saxon:assign , that allows me to assign a new value to this variable. The template rule is shown here: <xsl:template match="/"> <xsl:variable name="idx" saxon:assignable="yes" select="1"/> <saxon:while test="$idx < 11"> Value of idx is <xsl:value-of select="$idx"/> <saxon:assign name="idx" select="$idx+1"/> </saxon:while> </xsl:template> In the xsl:variable instruction, notice the saxon:assignable extension attribute. The SAXON processor requires that this attribute be added to any xsl:variable that you intend to change by using saxon:assign . The output of my looping is as follows : The value of idx is 1 The value of idx is 2 The value of idx is 3 The value of idx is 4 The value of idx is 5 The value of idx is 6 The value of idx is 7 The value of idx is 8 The value of idx is 9 The value of idx is 10 Tip For more information on saxon:while , saxon:assign , and other SAXON extensions, visit the SAXON Web site at saxon. sourceforge .net .
|