XML Glossary - XSL

XSLT Basics, structure and apply - choose

This section only deals with the outlines. More detail on constructing XML/XSLT documents appears elsewhere on this site. See links in the navigation bar on the left. XSLT elements which are not currently supported by the only current XML capable browser, IE5, are not included here. (I see little point in delving into areas I cannot test or apply!) If you would like information on other tags and cannot make head nor tail of the official W3C definitions, please email me using the link in the navigation bar.

XSLT Structure

A cut-down .xsl stylesheet

All contents of the XSL stylesheet must be enclosed within the <xsl:stylesheet> tag - although this tag is usually preceded by the xml declaration:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<!-- All XSL content goes here -->

XSL elements

XSL:APPLY-IMPORTS. Used alongside <xsl:import>, this applies the imported XSL stylesheet to the current node and each child node beneath the current. The import has no effect on sibling or ancestor nodes / elements.

<xsl:apply-imports />

XSL:APPLY-TEMPLATES. Apply one or more templates, according to the value of the select attribute. Templates need to be described elsewhere in the XSLT stylesheet, see xsl:template. The template dictates how the element is transformed. apply-templates dictates WHICH elements are transformed according to which template. Use logical pattern matching to select only those elements that should be transformed in one particular way. xsl:apply-templates select=CODEHELP/NAVIGATE will use the template matching CODEHELP/NAVIGATE on all occurrences of the NAVIGATE tag which occur within the CODEHELP tag. Note that the NAVIGATE tag must occur as a direct child of CODEHELP. If the NAVIGATE tag is nested within another tag, the select attribute will not match. Also, if the NAVIGATE tag occurs outside the CODEHELP tag, the select attribute will not match. The application of templates continues at the current position in the transformed file until the match fails. So even if you include a NAVIGATE tag as the last tag in the XML file, it will be processed along with all the other NAVIGATE tags according to the position of the xsl:apply-templates element in the XSLT stylesheet.

<xsl:apply-templates [select="pattern"]>

XSL:ATTRIBUTE. Extends the use of XML attributes by dynamically adding an attribute to the current tag during XSLT processing. Only one attribute with a given name per element. The contents of the XSL:ATTRIBUTE tag form the contents of the XML attribute in the current document tree. Further XSL elements can then process the attribute using the same templates that would apply if the attribute already existed in the XML itself, depending on where during processing the XSL:ATTRIBUTE is added. The attribute added must still comply with the current DTD. Note that if xsl:attribute is used, all files containing the matching tag will contain the same attribute, set to the same value.

<xsl:element name="navigate">
<xsl:attribute name="myextrainfo">Extra attribute
info can go here</xsl:attribute>

this transforms <navigate /> into:

<navigate myextrainfo="Extra attribute info can go here" />

XSL:CHOOSE. Use alongside XSL:WHEN and XSL:OTHERWISE to run multiple conditional tests: when choose results in a true test, one match is made, otherwise an alternative match can be made. Some conditional tests can be made without XSL:CHOOSE. The following code matches each instance of CODEHELP/NAVIGATE elements but uses a test (in this case, whether NAVIGATE contains an attribute called WWW) to determine whether this transformation should apply. Note that if a separate template match lacking the test [@WWW] is present in the XSL file, this will match all instances whether the test is true or false - overwriting transformations made in previous matches so use alternative tests like [@LOCAL] to cope with other possibilities.

<xsl:template match="CODEHELP/NAVIGATE[@WWW]">
<a><xsl:attribute name="href"><xsl:value-of select="./@FILEBASE"/>
<xsl:attribute name="title">
<xsl:value-of select="."/>
<xsl:value-of select="./@FILELINK" />

XSL:CHOOSE allows improvement over the simple test above by ensuring that transformations are not overwritten. The tests available are one of four sequential tests:


<xsl:template match="CODEHELP/NAVIGATE[@WWW]">
<xsl:when test=".[first-of-type()]">
First Link:
Subsequent Links:
<!-- all treated precisely the same way -->
<xsl:apply-templates />

The complementary options of [] and xsl:choose will not cover all eventualities. XSLT cannot be seen in the light as a full programming or scripting language like C, PHP or Javascript. There may be times when it is easier to change the XML structure rather than the XSL - not an ideal situation as there may be times when the XML format is not available to you to change. Such problems would need a full programmable parser like PHP which can parse XML directly and apply more complex rules and conditions to the data extracted.

This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.