xsltxquery

Is there a way to run XQuery functions / or XQuery scripts locally within an XSLT script file?


The previous question I made in stack overflow was about how to successfully use load-query-module to run XQuery in XSLT, however the runtime has to be able know how use the location-hint to find the XQuery module to load it.

If I assume my SAP Platform runtime doesn't support this then I need another way to run XQuery within XSLT. Is there away to pass the XQuery as a parameter and then run this within the XSLT or can the XSLT refer to another part of XSLT that has the XQuery embedded elsewhere within the script file?


Solution

  • Here is an example for Saxon 12 (PE/EE) using https://www.saxonica.com/html/documentation12/functions/saxon/xquery.html:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:saxon="http://saxon.sf.net/"
      exclude-result-prefixes="#all">
    
      <xsl:param name="xquery-param1" as="xs:string" expand-text="no"><![CDATA[
      declare variable $x external; declare variable $y external; <z>{$x + $y + .}</z>
      ]]></xsl:param>
    
      <xsl:output indent="yes"/>
    
      <xsl:template match="/" name="xsl:initial-template">
        <result>
          <xsl:variable name="xquery1" select="saxon:xquery($xquery-param1)"/>
          <xsl:sequence select="$xquery1(4, map{xs:QName('x'): 3, xs:QName('y'): 2})"/>
        </result>
      </xsl:template>
    
    </xsl:stylesheet>
    

    And for saxon:compile-query/saxon:query:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:saxon="http://saxon.sf.net/"
      exclude-result-prefixes="#all">
    
      <xsl:param name="xquery-param1" as="xs:string" expand-text="no"><![CDATA[
      declare variable $x external; declare variable $y external; <z>{$x + $y + .}</z>
      ]]></xsl:param>
    
      <xsl:output indent="yes"/>
    
      <xsl:template match="/" name="xsl:initial-template">
        <result>
          <xsl:variable name="xquery1" select="saxon:compile-query($xquery-param1)"/>
          <xsl:variable name="xquery1-params" as="element()*">
             <x>3</x>
             <y>2</y>
          </xsl:variable>
          <xsl:sequence select="saxon:query($xquery1,4, $xquery1-params)"/>
        </result>
      </xsl:template>
    
    </xsl:stylesheet>