saxonxslt-3.0

In XSLT 3.0 whilst merging to node sets using saxon:compile-query/saxon:query


I would like to merge 2 node-sets together using XQuery via the saxon:compile-query/saxon:query. Ideally I would like to use the 2nd argument (context-item) as my 1st node-set and then I would like to set another node-set to the XQuery too. This doesn't appear to be possible using the 3rd argument as this seems to only cater for atomic values. One idea I had was to put both node-set sources into a new root/parent and then pass it as the 2nd the argument (context-item). This does work but wondering if there is a better approach here? Using Saxon EE 9.9


Solution

  • I have now tried what I suggested in a comment, i.e. to pass in an array of two nodes as the context item, it seems to work fine:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
      expand-text="yes">
    
      <xsl:param name="xquery1" as="xs:string" expand-text="no"><![CDATA[<root>{
        for $node in (?1/*, ?2/*)
        group by $name := node-name($node) 
        return
         element { $name} { $node/node() }
      }</root>]]></xsl:param>
    
      <xsl:output method="xml" indent="yes"/>  
    
      <xsl:template match="/" name="xsl:initial-template">
         <xsl:variable name="node1"><foo>foo 1</foo><bar>bar 1</bar></xsl:variable>
         <xsl:variable name="node2"><foo>foo 2</foo><bar>bar 2</bar></xsl:variable>
         <xsl:variable name="compiled-xquery" select="saxon:compile-query($xquery1)"/>
         <xsl:sequence select="saxon:query($compiled-xquery, [$node1, $node2])"/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Whether you need XQuery from XSLT 3 to merge nodes is a different question, I don't know what your requirements are exactly, there is xsl:merge.