Using Variables and Parameters in XSLT


Using Variables and Parameters in XSLT

A variable is a memory location that stores data, and parameter is a variable that is passed to a function. The <xsl:variable> element helps define variables in XSLT, and the <xsl:param> element helps define XSLT parameters. The <xsl:variable> and <xsl:param> elements contain the name attribute that specifies the name of the variable and parameter.

You create local variables and parameters by defining them in the template rule. You can also create global variables and parameters in XSLT, by creating them as child elements of the <xsl:stylesheet> element. The global variable is accessible throughout the stylesheet.

You cannot change the value of an XSLT variable. You need to create a new variable if you want to change the value of an existing variable. Enter the following code to create an XSLT variable:

 <xsl:variable name="StudName" select="/StudentDetail/Student> </xsl:variable> 

In the above code, the select attribute specifies the XPATH expression and the result of the expression specifies the value of the XSLT variable. The StudName variable indicates an element node, which refers to the Student element of the source document.

Result Tree Fragments

The <xsl:variable> or <xsl:param> element creates a result tree fragment when you refer to the value of a specific element in the result tree. The result tree fragment is a part of the result tree obtained from an XSLT transformation, and is created by the XSLT processor. A result tree fragment should be balanced, which means that each start tag must correspond to an end tag. You can perform a valid string operation on a result tree fragment. For example, the following code shows a part of an XSLT stylesheet:

 <xsl:variable item="Computer"> <ch>chapter number</ch> books </xsl:variable> 

The item variable, in the above code, creates the following result tree fragment:

 <ch>chapter number</ch> books 

The result tree fragment, in the above code, consists of the <ch> element, and the trailing string, books, outside the <ch> element.

Note  

You cannot use the /, //, and [] operators on result tree fragments.

The <xsl:copy-of> element inserts a result tree fragment into the result tree without converting it to a string value. The syntax to use the <xsl:copy-of> element is:

 <xsl:copy-of select=expression/> 

The above syntax shows that the select attribute contains an expression that is evaluated. When the evaluation of the specified expression returns a result tree fragment, the <xsl:copy-of> element copies the tree fragment to the result tree.

An XSLT stylesheet consists of one or more template rules, which identify a pattern that you need to match in a source tree, and describe the structure of the result tree. Each template rule is applicable to a specific set of nodes, and executes a set of instructions to create the structure of the nodes in the result tree.

Top-level Variables and Parameters

A top-level variable is a sub-element of the <xsl:stylesheet> element. A top-level variable creates a global variable and parameter. You cannot change the value of a top-level variable. The <xsl:variable> and <xsl:param> elements are the examples of top-level elements. The <xsl:param> element only creates a parameter of the stylesheet, but does not pass the parameter to the stylesheet. You can assign a value to the parameter of the stylesheet using various forms, such as the top-level parameter, the <xsl: with-param > element, or a default value, as shown in Listing 4-8:

Listing 4-8: Assigning the Value to a Parameter
start example
 <xsl:template match="/"> <xsl:call-template name="Temp> <xsl:with-param name="paramName" select="any-value"/> </xsl:call-template> </xsl:template> <xsl:template name="Temp"> <xsl:param name="paramName" select=default-value"/> <xsl:value-of select="$param"/> </xsl:template> 
end example
 

The above listing shows that the <xsl:call-template> element invokes the Temp template, which accepts the paramName parameter. The Temp template assigns a default value to the parameter.

Variables and Parameters in a Template

XSLT contains the <xsl:variable> element that creates variables in stylesheets. The syntax to declare an XSLT variable is:

 <xsl:variable name="var" select="0"/> 

In the above syntax, you declare a variable using an XPath expression. The var variable, declared in the above syntax, stores the numeric value, 0.

Another syntax for declaring an XSLT variable is:

 <xsl:variable name="var">0</xsl:variable> 

In the above syntax, the value of the var variable is specified within the starting and ending tags.

You can use a variable either as a top-level element or as a template-level element. A template-level element is defined within the template, and all the elements positioned after the variable declaration can use this element. You can change the value of a variable by declaring it inside the for-each loop, where its value changes on every iteration. The template-level elements remain in effect only within their scope, which is represented by the following-sibling nodes and the descendent nodes of the elements.

The parameter element in XSLT is similar to the variable element. Both these elements share the same namespace and syntax to assign names and values to an element. The keyword, param, denotes the parameter element. The param element consists of a name attribute and a select attribute. The value of the select attribute in a parameter element specifies the default value of the element. This means that if you pass a new value to the select attribute, then the default value is replaced with the new value. You can assign value to a parameter element either from the process that invokes the stylesheet, or from the <xsl:with-param> element.

To pass the value of a parameter to another template, you pass a parameter using the <xsl:with-param> element. The process of passing a parameter to a template calls a specific template using the <xsl:call-template> element.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

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