4

How do I make the unparsed-text-lines() function effectively available to both XSLT 2.0 and XSLT 3.0 processors in the one style-sheet?

I thought that I could use the function-available() function like so, but this returns a syntax error for an XSLT 2.0 processor.

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:fn="http://www.w3.org/2005/xpath-functions" 
  xmlns:local="local" 
  version="2.0" exclude-result-prefixes="xs fn local">

<xsl:function name="local:unparsed-text-lines" as="xs:string+">
 <xsl:param name="href" as="xs:string" />
 <xsl:choose>
  <xsl:when test="function-available('fn:unparsed-text-lines')">
   <!-- XSLT 3.0 -->
   <xsl:sequence select="fn:unparsed-text-lines($href)" />
  </xsl:when>
  <xsl:otherwise>
   <!-- XSLT 2.0 -->
   <xsl:sequence select="fn:tokenize(fn:unparsed-text($href), '\r\n|\r|\n')[not(position()=last() and .='')]" />
  </xsl:otherwise>
 </xsl:choose>
</xsl:function>

etc.
Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65

1 Answers1

5

The problem is:

<xsl:when>

is a run-time operator, and the compiler doesn't know at compile time that its result will be true() or false().

Solution: Use the use-when attribute.

The transformation becomes something like this:

<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:local="local"
      version="2.0" exclude-result-prefixes="xs local">

    <xsl:function name="local:unparsed-text-lines" as="xs:string+">
     <xsl:param name="href" as="xs:string" />
       <xsl:sequence select="fn:unparsed-text-lines($href)"
             use-when="function-available('unparsed-text-lines')" />
       <xsl:sequence use-when="not(function-available('unparsed-text-lines'))"
        select="tokenize(unparsed-text($href), '\r\n|\r|\n')
                    [not(position()=last()
                        and
                          .=''
                        )
                    ]" />
    </xsl:function>
</xsl:stylesheet>

and now no error is raised.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431