15.6 Checking for Extension Availability

XSLT provides two functions that allow you to check for the availability of extension elements or functions. Both the element-available( ) and function-available( ) functions take a single argument, a string identifying the element or function by QName. Example 15-9, check.xsl, uses both these functions in a simple way.

Example 15-9. A stylesheet testing for the availability of extensions
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:date="http://exslt.org/dates-and-times" xmlns:math="http://exslt.org/math" xmlns:xalan="http://xml.apache.org/xalan"> <xsl:output method="text" encoding="UTF-8"/> <xsl:param name="avail"/>     <xsl:template match="/"> <xsl:choose>  <xsl:when test="element-available($avail)">   Element <xsl:value-of select="$avail"/> is available with this processor.  </xsl:when>  <xsl:when test="function-available($avail)">   Function <xsl:value-of select="$avail"/> is available with this processor.  </xsl:when>  <xsl:otherwise>   The element or function you requested is not available with this processor.  </xsl:otherwise> </xsl:choose>     </xsl:template>     </xsl:stylesheet>

check.xsl tests to see whether a particular extension element or function is available with a given processor. There isn't much to this stylesheet by itself, but it could easily be included with another stylesheet using the include mechanism.

Here's a simple test (use any XML document on the command line you like; it doesn't really matter):

saxon keywords.xml check.xsl avail=date:date

This command line tests to see if the Instant Saxon processor (saxon) can support the EXSLT function date:date( ). The result should be:

Function date:date is available with this processor.

Now try a Saxon extension function:

saxon keywords.xml check.xsl avail=saxon:assign

The answer is as follows:

Element saxon:assign is available with this processor.

Finally, try an MSXSL function:

saxon keywords.xml check.xsl avail=msxsl:node-set

The news you'll get back won't be unexpected:

The element or function you requested is not available with this processor.


Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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