function-available() Function |
Determines if a given function is available to the XSLT processor. This function allows you to design stylesheets that react gracefully if a particular function is not available to process an XML document.
Inputs
The name function's name. The name is usually qualified with a namespace; if the namespace of the function name is non-null, the function is an extension function. Otherwise, the function is one of the functions defined in the XSLT or XPath specifications.
Output
The boolean value true if the function is available, false otherwise.
Defined in
XSLT section 15, Fallback.
Example
We'll use the following XML document to test the function-available() function:
    <?xml version="1.0"?>
    <list>
      <title>A few of my favorite albums</title>
      <listitem>A Love Supreme</listitem>
      <listitem>Beat Crazy</listitem>
      <listitem>Here Come the Warm Jets</listitem>
      <listitem>Kind of Blue</listitem>
      <listitem>London Calling</listitem>
      <listitem>Remain in Light</listitem>

      <listitem>The Joshua Tree</listitem>
      <listitem>The Indestructible Beat of Soweto</listitem>
    </list>
Here's our stylesheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:jpeg="class:JPEGWriter"
      extension-element-prefixes="jpeg">

      <xsl:output method="text"/>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="list/listitem">
          <xsl:choose>
            <xsl:when test="function-available('jpeg:createJPEG')">
              <xsl:value-of
                select="jpeg:createJPEG(., 'bg.jpg',
                concat('album', position(), '.jpg'),
                'Swiss 721 Bold Condensed', 'BOLD', 22, 52, 35)"/>
              <xsl:text>See the file </xsl:text>
              <xsl:value-of select="concat('album', position(), '.jpg')"/>
              <xsl:text> to see the title of album #</xsl:text>
              <xsl:value-of select="position()"/>
              <xsl:value-of select="$newline"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="position()"/>
              <xsl:text>. </xsl:text>
              <xsl:value-of select="."/>
              <xsl:value-of select="$newline"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>
In our stylesheet, if the createJPEG() function is available, we'll invoke it to create JPEG files for the titles of all our favorite albums. If the function is not available, we'll simply write those titles to the output stream. Here are the results we get when the createJPEG() function is available:
    See the file album1.jpg to see the title of album #1
    See the file album2.jpg to see the title of album #2
    See the file album3.jpg to see the title of album #3
    See the file album4.jpg to see the title of album #4
    See the file album5.jpg to see the title of album #5
    See the file album6.jpg to see the title of album #6
    See the file album7.jpg to see the title of album #7
    See the file album8.jpg to see the title of album #8
All album titles (the text of the <listitem> elements) are converted to JPEG graphics. In this example, the file album8.jpg looks like Figure C-3.

Generated graphic for the eighth <listitem> element

If we delete the file JPEGWriter.class (if the .class file is missing, the function isn't available), we get these results instead:

    1. A Love Supreme
    2. Beat Crazy
    3. Here Come the Warm Jets
    4. Kind of Blue
    5. London Calling
    6. Remain in Light
    7. The Joshua Tree
    8. The Indestructible Beat of Soweto