function-available() and xsl:fallback

Here should go questions about transforming XML with XSLT and FOP.
ahenket
Posts: 3

function-available() and xsl:fallback

Sat Sep 26, 2015 11:13 am

I would like to have an XSL that calls out to saxon:evaluate when it can, and falls back to something else when it cannot. I thought that's what function-available() is for, but although I can check availability with that function, I cannot get the XSL to compile with Saxon-PE (doesn't do extensions) when the function itself is in it.

Example fragment:

Code: Select all

    <xsl:variable name="hl7date" select="'{format-date(current-date()-xs:yearMonthDuration(''P9Y2M'')-xs:dayTimeDuration(''P30D''),''[Y0001][M01][D01]'')}'"/>
    <xsl:if test="function-available('saxon:evaluate')">
        <xsl:value-of select="saxon:evaluate(substring($hl7date,2,string-length($hl7date)-2))"/>
    </xsl:if>
   

or even

Code: Select all

    <xsl:value-of select="if (function-available('saxon:evaluate')) then saxon:evaluate(substring($hl7date,2,string-length($hl7date)-2)) else ($hl7date)"/>

This feels like catch22. The other option I though of was xsl:fallback, but I could not figure out where to put it as this does not compile either:

Code: Select all

        <xsl:value-of select="saxon:evaluate(substring($hl7date,2,string-length($hl7date)-2))">
            <xsl:fallback><xsl:value-of select="$hl7date"/></xsl:fallback>
        </xsl:value-of>

Anyone know if my use case is possible, and if so how?
adrian
Posts: 2387

Re: function-available() and xsl:fallback

Tue Sep 29, 2015 11:11 am

Hi,

ahenket wrote:I would like to have an XSL that calls out to saxon:evaluate when it can, and falls back to something else when it cannot. I thought that's what function-available() is for, but although I can check availability with that function, I cannot get the XSL to compile with Saxon-PE (doesn't do extensions) when the function itself is in it.

I'm guessing you're referring to Saxon-HE since that is the one that doesn't support extensions. Saxon-PE should work just fine.

I found an explanation and the solution for Saxon-HE here:
[saxon] Saxon-HE and function-available

The problem seems to be that references to functions are resolved statically at compile time and Saxon-HE does not resolve Saxon extension functions.
A simple solution is to use use-when="function-available()":

Code: Select all

<xsl:value-of select="saxon:evaluate(substring($hl7date,2,string-length($hl7date)-2))" use-when="function-available('saxon:evaluate')"/>

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests