'Use of Maps and Lookups in XSLT 3.0

I am new to XSLT 3.0 and I am exploring xslt maps and accumulators for one of my requirement. I have an input aggregated XML as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Aggregate>
   <File>
       <Info>
           <emplid>0081781</emplid>
           <Number>12345_A</Number>
       </Info>        
        
         
       <Info>
           <emplid>009892</emplid> 
           <Number>12345_B</Number>
       </Info>   
    
        <Info>
           <emplid>0044371</emplid>
           <Number>12345_C</Number>
       </Info>  
   </File>


   <Employee_Data>        
       <Employee>            
           <emplid>0081781</emplid>            
           <empl_Status>A</empl_Status>
       </Employee>
    
       <Employee>            
           <emplid>009892</emplid>
           <empl_Status>T</empl_Status>
       </Employee>
    
   </Employee_Data>
</Aggregate>

Expected Result:

<Root>
   <Output_Data>
       <Employee>
           <emplid>0081781</emplid>            
           <empl_Status>A</empl_Status>
           <Number>12345_A</Number>
       </Employee>
    
       <Employee>
           <emplid>009892</emplid>
           <empl_Status>T</empl_Status>
           <Number>12345_B</Number>
       </Employee>
   </Output_Data>
</Root>

Although this may look like a fairly simple xsl:key based solution on the <File> element, I am looking to find a streaming solution with XSLT 3.0 because the volume of data is very high and I want to avoid high consumption of memory in my environment.

Any help appreciated!



Solution 1:[1]

As far as I am aware, XSLT 3 with streaming is only supported by Saxon EE so you could make your life easy and rely on the extension to capture a node during streaming:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:saxon="http://saxon.sf.net/"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:mode on-no-match="shallow-copy" streamable="yes" use-accumulators="infos"/>
    
    <xsl:accumulator name="infos" as="map(xs:string, xs:string)" initial-value="map{}" streamable="yes">
        <xsl:accumulator-rule saxon:capture="yes" phase="end"
            match="Info"
            select="map:put($value, string(emplid), string(Number))"/>
    </xsl:accumulator>
    
    <xsl:template match="File"/>
    
    <xsl:template match="Aggregate">
        <Root>
            <xsl:apply-templates/> 
        </Root>
    </xsl:template>
    
    <xsl:template match="Employee_Data">
        <Output_Data>
            <xsl:apply-templates/>
        </Output_Data>
    </xsl:template>
    
    <xsl:template match="Employee">
        <xsl:apply-templates select="copy-of()" mode="burst"/>
    </xsl:template>
    
    <xsl:mode name="burst" on-no-match="shallow-copy"/>
    
    <xsl:template match="Employee" mode="burst">
        <xsl:copy>
            <xsl:apply-templates/>
            <Number>{accumulator-before('infos')(string(emplid))}</Number>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

I am not sure, however, at which version (9.9 or 10 of Saxon EE) the saxon:capture mechanism was introduced.

To do it without the Saxon extension, I would use two accumulators, one to store the id, the second to build the map:

<xsl:accumulator name="emp-id" as="xs:string?" initial-value="()" streamable="yes">
    <xsl:accumulator-rule match="file/Info/emplid/text()" select="normalize-space()"/>
</xsl:accumulator>

<xsl:accumulator name="Employees" as="map(xs:string,xs:string)" streamable="yes" initial-value="map{}">
    <xsl:accumulator-rule match="file/Info/Number/text()" select="map:put($value, accumulator-before('emp-id'), normalize-space())"/>
</xsl:accumulator>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1