Using Conditional XSL Statements and Embedding Scripting Code to Perform Data Queries
In your work with XSL templates so far, you've used statements such as xsl:apply-templates and xsl:value-of select="NodeName". These are examples from the list of available XSL elements, which are statements that can be used to extract data and insert nodes into the node tree, among other operations. This hour will use some conditional XSL elements and will list all those that are currently supported.
Conditional elements are those that allow for the selective processing of segments of code. The selection can be based on the value of a Boolean, or logical, expression, or the value of a variable or expression. Conditional expressions are very powerful tools, allowing decisions to be made as the program is running.
It seems to be a law of programming that no matter what a system provides, there's always some bit of functionality that's lacking. Although XSL is powerful, it's no exception. Therefore, this hour will examine an XSL element that allows segments of scripting code to be embedded inside the XSL template.
In this hour, you'll learn the following:
-
The complete set of XSL elements that can be used in templates
-
How to use the conditional elements to selectively extract data from an XSL file
-
How to embed segments of scripting code in an XSL template
The XSL Elements
So far, you've used only a few of the XSL elements that are supported. See Table 15.1 for more of them.
Table 15.1 XSL Elements
Element |
Function |
xsl:apply-templates |
Commands the XSL processor to find and execute the appropriate template(s). |
xsl:attribute |
Creates an attribute node and applies it to the output node structure. |
xsl:cdata |
Inserts a CDATA section into the output. |
xsl:choose |
Is used for conditional testing and processing. Generally is used in combination with the xsl:otherwise and xsl:when elements. |
xsl:comment |
Inserts a comment into the output node structure. |
xsl:copy |
Makes a copy of a source node. |
xsl:define-template-set |
Defines a set of templates. |
xsl:element |
Inserts an element into the output with the specified name. |
xsl:entity-ref |
Inserts an entity reference with the specified name into the output. |
xsl:eval |
Evaluates a string of text; more specifically, is used to insert sections of scripting code into the file. |
xsl:for-each |
Applies the template(s) to each child node of the specified parent node. |
xsl:if |
Is used to apply a true/false test and perform conditional processing. |
xsl:node-name |
Inserts the name of the current node into the output structure. |
xsl:otherwise |
Is used to specify an alternative when conditional processing is performed. |
xsl:pi |
Inserts a processing instruction into the output structure. |
xsl:script |
Is used to define global variables. |
xsl:stylesheet |
Delimits an XSL style sheet. |
xsl:template: |
Delimits a template for generating output based on a pattern or patterns. |
xsl:value-of |
Extracts the current value of the item specified in the SELECT attribute and returns it as text. |
xsl:when |
Is used to test an attribute in conditional processing. |