Home > database >  How to fix this in XSLT 3.0? (<xsl:merge>)
How to fix this in XSLT 3.0? (<xsl:merge>)

Time:09-26

I wish to explain <xsl:merge>.

This used to work years ago, but not anymore:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="3.0" 
      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">
   <xsl:template match="/">
     <xsl:merge bind-group="g" bind-key="k">
       <xsl:merge-source for-each="(1,100)" select="(.*5,.*3,.*1,.*7,.*9)">
          <xsl:merge-key select="."/>
       </xsl:merge-source>
       <xsl:merge-source select="(4,10,2,8,6)">
          <xsl:merge-key select=". * 10" order="descending"/>
       </xsl:merge-source>
       <xsl:merge-source select="(5)">
          <xsl:merge-key select=". * 10"/>
       </xsl:merge-source>
       <xsl:merge-action>
         <xsl:value-of select="$g"/>
         <xsl:text>&#32;</xsl:text>
         <xsl:value-of select="$k"/>
         <xsl:text>&#10;</xsl:text>
       </xsl:merge-action>
     </xsl:merge>
   </xsl:template>
</xsl:transform>

How can I create a demo that best explains XSLT merging for my readers?

CodePudding user response:

The spec since 2017 is online at https://www.w3.org/TR/xslt-30/#merge-instruction so perhaps start there, I am not sure where your syntax comes from, whether that is from an older working draft or from some other implementation.

The closest I could come up with is e.g.

   <xsl:template match="/" name="xsl:initial-template">
     <xsl:merge>
       <xsl:merge-source for-each-item="(1,100)" select="(.*5,.*3,.*1,.*7,.*9)" sort-before-merge="yes">
          <xsl:merge-key select="."/>
       </xsl:merge-source>
       <xsl:merge-source select="(4,10,2,8,6)" sort-before-merge="yes">
          <xsl:merge-key select=". * 10"/>
       </xsl:merge-source>
       <xsl:merge-source select="(5)">
          <xsl:merge-key select=". * 10"/>
       </xsl:merge-source>
       <xsl:merge-action>
         <xsl:value-of select="current-merge-group()"/>
         <xsl:text>&#32;</xsl:text>
         <xsl:value-of select="current-merge-key()"/>
         <xsl:text>&#10;</xsl:text>
       </xsl:merge-action>
     </xsl:merge>
   </xsl:template>

Works &input=&input-type=None" rel="nofollow noreferrer">online with SaxonJS.

  • Related