Home > Software engineering >  Let is not supported in XPath
Let is not supported in XPath

Time:12-02

Recently I upgraded my server and the following XSLT with Saxonb-XSLT stopped working:

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

  <xsl:output cdata-section-elements="title"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="title[ends-with(., 'Apple') or ends-with(., 'Samsung') or ends-with(., 'Banana')]">
      <xsl:copy>
          <xsl:value-of select="let $words := tokenize(., '\s ') 
               return (subsequence($words, 1, count($words) - 2), $words[last()], $words[last() - 1])"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

I get error:

  XPST0003: XPath syntax error at char 0 on line 13 in {let $}:
    'let' is not supported in XPath

I haven't upgraded saxonb-xslt (Saxon 9.1.0.8J from Saxonica). Anyone has idea why it is not working properly?

CodePudding user response:

You could bypass the need for let and any related version or licensing complications by rewriting your template to use an XSLT xsl:variable statement rather than an XPath 3.0 let statement:

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

  <xsl:output cdata-section-elements="title"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="title[   ends-with(., 'Apple') 
                             or ends-with(., 'Samsung') 
                             or ends-with(., 'Banana')]">
    <xsl:variable name="words" select="tokenize(., '\s ')"/>
    <xsl:copy>
        <xsl:value-of select="subsequence($words, 1, count($words) - 2), 
                              $words[last()], 
                              $words[last() - 1]"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

CodePudding user response:

The let binding is part of XPath 3 I think, as that it is not supported completely in XSLT 3.0 before Saxon 9.8 HE as that is the first Saxon open-source release to support the final XSLT 3.0 with XPath 3 recommendation. You might find that using version="3.0" and then XPath 3 expressions using let work also in 9.7 and perhaps 9.6 but in even older releases Saxon (at least the open-source edition) is an XSLT 2.0 processor with XPath 2.0 support and doesn't support any XPath 3 expressions (like let).

The literal error message 'let' is not supported in XPath might suggest that that release had some support for let in XQuery but I don't recall details and I haven't checked.

It is not clear which version of Saxon you used before your upgrade and which one after it or what kind of "update" you did.

CodePudding user response:

You mention Saxon 9.1.0.8J from Saxonica. That is a very old release indeed (2009), and it would never have been able to run this stylesheet.

Somehow your "upgrade" has left you running an older Saxon release, I fear.

The current version is 10.6.

  • Related