最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In XSLT 3.0 whilst merging to node sets using saxon:compile-querysaxon:query - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Feb 6 at 16:49 Alex asked Feb 5 at 20:15 AlexAlex 175 bronze badges 2
  • I would consider to pass in an array containing the two nodes (e.g. [$node1, $node2]) as the context item, or a map with two properties, each being a node. Untested but might work, for an array ?1 gives the first item and ?2 the second, for a map it depends how you name the properties but if you pass in e.g. map { 'node1' : $node1, 'node2' : $node2 } in the XQuery you just use ?node1 and ?node2. – Martin Honnen Commented Feb 5 at 20:44
  • It's time we modernised the saxon:compile-query interface. I see we still have both saxon:query and saxon:xquery with overlapping functionality, but neither allows supplying a map to bind global externl variables declared in the query. These days we might make saxon:compile-query return a function item that can be called with a dynamic function call to execute the query, probably with a single map-valued argument to supply the context item and external variables. – Michael Kay Commented Feb 6 at 8:59
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论