The Ground Cero Guide to XSL
Henrik Aasted Sorensen
<< Introducing XPathXSL-processors >>
7. More XSLT
Having touched upon XPath, let's move forward to slightly more advanced XSLT-topics.
Conditional processing
It's possible to use conditional processing in XSLT by applying the <xsl:if>-tag. This tag contains the test-attribute, which in turn contains an XPath-expression. This can be used to test for tree-structure, variable values and other things. A short example for the address-book:
<xsl:if test="add:email">
    ...
</xsl:if>
If this example is put around the e-mail-printing lines in the person-template, it will only print the e-mail-addres if it is available. This way it's possible to have a really dynamic address-book where not all entries need to contain every piece of information: only the available information will be printed. This can also be achieved by making templates for every tag that will be printed.
Iteration
The <xsl:for-each>-tag iterates through a collection of nodes selected by the select-attribute. The contents of the tag will be used as a template on each node. The <xsl:for-each> tag is in many ways very similar to the <xsl:apply-templates>-tag. Use the <xsl:for-each>-tag if you don't feel like making a complete template for the processing of some nodes or if you want to do processing that's very different from the one available in the template.
Sorting
The normal ordering of nodes-processing is what's called "document-ordering", which means that nodes are processed in the order that they appear in the XML-document. A different sorting of nodes can be achieved with the <xsl:sort>-tag. Putting an <xsl:sort>-tag inside a <xsl:for-each>- or <xsl:apply-template>-tag will change the processing-order of the nodes.
Example:
<xsl:apply-templates select="add:person">
    <xsl:sort select="@name"/>
</xsl:apply-templates>
This will sort each person in the address-book alphabetically by their name. Notice how the <xsl:apply-templates> tag is having both an opening and a closing tag in this example. Looking at this makes me realize that it would have been nice to split the name-attribute into a "first-name" and a "last-name" for more correct sorting. I'll leave that as a an exercise to the reader. :) The <xsl:sort>-tag also contains the attribute order, which can take the two values "ascending" or "descending".
If more <xsl:sort>-tags are used, the first one will be the primary sort key, the second will be the secondary sort key and so on. The following example will sort the people in the addressbook firstly by country and secondly by name.
<xsl:apply-templates select="add:person">
    <xsl:sort select="add:address/add:country"/>
    <xsl:sort select="@name"/>
</xsl:apply-templates>
Parameters
It may be relevant to be able to affect the processing from "outside", ie. without changing the document. This can be done through parameters. The syntax of using parameters is strikingly similar to that of using variables:
<xsl:param name="parameter1">value</xsl:value>
A param-tag must be the direct child of the root-node. After instantiating a parameter, it can be used like a variable in the rest of the document. If the parameter is not set by the environment when calling the XSL-processor, the parameter will just take on the value in the tag. Passing parameters to an XSL-transformation is done differently according to which XSL-processor you're using. Look to the section on XSL with PHP to see how to do it in PHP.
<< Introducing XPathXSL-processors >>
Index