Home > Net >  XSLT with nested for-each-group to display unique combination of sibling elements
XSLT with nested for-each-group to display unique combination of sibling elements

Time:10-27

Heading ##Input XML:

<?xml version="1.0" encoding="utf-8"?>
<Report_Data>
    <Report_Entry>
    
        <cost_center>
            <ID>CC</ID>
        </cost_center>
        
        <cost_center_site>
            <ID>CCS1</ID>
        </cost_center_site>
        
        <cost_center_site>
            <ID>CCS2</ID>
        </cost_center_site>
        
        <cost_center_site>
            <ID>CCS3</ID>
        </cost_center_site>
        
        <gl_company>
            <ID>G1</ID>
        </gl_company>
        
        <gl_company>
            <ID>G2</ID>
        </gl_company>
        
    </Report_Entry>
</Report_Data>

Applied XSLT:

<?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"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output method = "text"/>
    <xsl:variable name="linefeed" select="'&#xA;'"/>
    <xsl:variable name="delim" select="','"/>
    
    
    <xsl:template match="/*">
        <xsl:apply-templates select="Report_Entry" />
    </xsl:template>
    
    <xsl:template match="Report_Entry">
        <xsl:text>cost_center,cost_center_site,gl_company</xsl:text>
        <xsl:value-of select="$linefeed"/>
        
        <xsl:for-each-group select="cost_center" group-by="ID">
            
            <xsl:for-each-group select="../cost_center_site" group-by="ID">
   
                <xsl:for-each-group select="../gl_company" group-by="ID">
                    
                    <xsl:value-of select="../cost_center/ID"/>
                    <xsl:value-of select="$delim"/>
                    <xsl:value-of select="../cost_center_site/ID"/>
                    <xsl:value-of select="$delim"/>
                    <xsl:value-of select="ID"/>
                    <xsl:value-of select="$linefeed"/>                    
                
                </xsl:for-each-group>
                
            </xsl:for-each-group>
            
        </xsl:for-each-group>
        


    </xsl:template>
    
</xsl:stylesheet>

Getting Following Results:

cost_center cost_center_site gl_company
CC CCS1 CCS2 CCS3 G1
CC CCS1 CCS2 CCS3 G2
CC CCS1 CCS2 CCS3 G1
CC CCS1 CCS2 CCS3 G2
CC CCS1 CCS2 CCS3 G1
CC CCS1 CCS2 CCS3 G2

Expected Results:

cost_center cost_center_site gl_company
CC CCS1 G1
CC CCS1 G2
CC CCS2 G1
CC CCS2 G2
CC CCS3 G1
CC CCS3 G2

I tried using current-grouping-key() and for-each in inner loop to display unique cost center site value on each row but couldn't get the cost center to repeat on each row. Would appreciate if someone can assist in providing inputs to the xslt in order to get achieve the expected results.

Thanks!

CodePudding user response:

I think it should be more like

<xsl:template match="Report_Entry">
    <xsl:text>cost_center,cost_center_site,gl_company</xsl:text>
    <xsl:value-of select="$linefeed"/>
    <xsl:for-each-group select="." group-by="cost_center/ID">
        <xsl:variable name="cc-id" select="current-grouping-key()"/>
        <xsl:for-each-group select="current-group()" group-by="cost_center_site/ID">
            <xsl:variable name="ccs-id" select="current-grouping-key()"/>
            <xsl:for-each-group select="current-group()" group-by="gl_company/ID">
                <xsl:value-of select="$cc-id, $ccs-id, current-grouping-key()" separator="{$delim}"/>
                <xsl:value-of select="$linefeed"/>                    
            </xsl:for-each-group>
        </xsl:for-each-group>
    </xsl:for-each-group>
</xsl:template>
  • Related