Thomas W
Thomas W

Reputation: 15371

Fallbacks for XSLT 2.0 processor in 3.0 stylesheet

For parsing some strings, I want to use analyze-string(), if available, and provide a slower fallback that works with XSLT 2.0. This is a quick test I did:

<xsl:stylesheet  version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template name="tokenize" 
      use-when="not(function-available('analyze-string'))">
    2.0
  </xsl:template>

  <xsl:template name="tokenize" version="3.0"
      use-when="function-available('analyze-string')">
    3.0
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="tokenize"/>
    <xsl:value-of select="function-available('analyze-string')"/>
  </xsl:template>

</xsl:stylesheet>

I ran this inside oXygen 15.2 with Saxon-PE/EE 9.5.1.3. Strangely, the output is

2.0
true

which indicates that the first template was included rather than the second, although its use-when attribute should have evaluated to true, as the second line of the output indicates. What went wrong?

Are there standard best practices when developing a stylesheet targeted at both 2.0 and 3.0 processors? Saxon HE accepts the stylesheet if I use the option -xsltversion:2.0 to override the version attribute on the xsl:stylesheet element. I also tried to set the version attribute to 2.0 to make overriding superfluous, but then function-available('analyze-string') will evaluate to false in Saxon PE/EE, even if I prefix it with the XPath functions namespace.

Upvotes: 0

Views: 135

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

In Saxon 9.5, for reasons which now escape me, the static context for use-when expressions includes only the core XPath 2.0 functions plus a few selected XSLT functions such as element-available, system-property, and function-available; it doesn't include XPath 3.0 functions such as analyze-string, even though these are available in "ordinary" XPath expressions within the stylesheet. I suspect this reflects the state of the XSLT 3.0 specification at the time Saxon 9.5 was released in 2013. The situation will have changed in 9.6 and 9.7.

Upvotes: 1

Related Questions