Home > Software design >  Xslt - how to group by and show output in same row in XSLT only partial output is showing with curre
Xslt - how to group by and show output in same row in XSLT only partial output is showing with curre

Time:10-07

Can you pls help me with this code resolution

My scenario is to display results based on Location I need to output two fields for each location. I am using grouping to achieve this. The Hours fields is same for all the locations and payrate combination but the Employee count field is different. That is causing the issue.

my XML code:

<wd:Report_Data
    xmlns:wd="urn:com.workday.report/TEST_PB">
    <wd:Report_Entry>
        <wd:location>Canada</wd:location>
        <wd:payRate>Salary</wd:payRate>
        <wd:salcount>10</wd:salcount>
        <wd:hours>250</wd:hours>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:location>Canada</wd:location>
        <wd:payRate>Hourly</wd:payRate>
        <wd:hrlycount>3</wd:hrlycount>
        <wd:hours>120</wd:hours>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:location>Canada</wd:location>
        <wd:payRate>CWR</wd:payRate>
        <wd:cwrcount>2</wd:cwrcount>
        <wd:hours>100</wd:hours>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:location>USA</wd:location>
        <wd:payRate>Salary</wd:payRate>
        <wd:salcount>7</wd:salcount>
        <wd:hours>200</wd:hours>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:location>USA</wd:location>
        <wd:payRate>Hourly</wd:payRate>
        <wd:hrlycount>5</wd:hrlycount>
        <wd:hours>500</wd:hours>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:location>USA</wd:location>
        <wd:payRate>CWR</wd:payRate>
        <wd:cwrcount>10</wd:cwrcount>
        <wd:hours>700</wd:hours>
    </wd:Report_Entry>
</wd:Report_Data>

my xsl code:

<?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:wd="urn:com.workday.report/TEST_PB" exclude-result-prefixes="xs"
    version="2.0" xmlns:functx="http://www.functx.com">

 <xsl:output method="text" indent="no"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="NEWLINE" select="'&#xa;'"/>
 <xsl:variable name="COMMA" select="','"/>

    <xsl:template match="/">

        <xsl:call-template name="DetailRecords"/>
    </xsl:template>
    
   <xsl:template name="DetailRecords">
        
        <xsl:for-each-group select="wd:Report_Data/wd:Report_Entry" group-by="wd:location">
         <xsl:sort select="wd:location"/>       

                <xsl:value-of select="concat(wd:location,$COMMA)"/>
                <xsl:if test="wd:payRate = 'Salary'">
                               <xsl:value-of select="current-group()/wd:hours" separator="," />
                </xsl:if>
                <xsl:if test="wd:payRate = 'Hourly'">
                               <xsl:value-of select="current-group()/wd:hours" separator="," />
                </xsl:if>
                <xsl:if test="wd:payRate = 'CWR'">
                               <xsl:value-of select="current-group()/wd:hours" separator="," />
                </xsl:if>
                <xsl:if test="wd:payRate = 'Salary'">
                               <xsl:value-of select="current-group()/wd:salcount" separator="," />
                </xsl:if>
                <xsl:if test="wd:payRate = 'Hourly'">
                               <xsl:value-of select="current-group()/wd:hrlycount" separator="," />     
                </xsl:if>
                <xsl:if test="wd:payRate = 'CWR'">
                               <xsl:value-of select=" wd:cwrcount" separator="," />                            
                </xsl:if>   
               <xsl:value-of select="$NEWLINE"/>    
             </xsl:for-each-group>  
    </xsl:template>

</xsl:stylesheet>
***Current Output:***
Canada,250,120,10010
USA,200,500,7007

***Expected Output:***
Canada,250,10,120,3,100,2
USA,200,7,500,5,700,10

CodePudding user response:

I think you want e.g. (XSLT 3)

 <xsl:for-each-group select="wd:Report_Data/wd:Report_Entry" group-by="wd:location">
     <xsl:sort select="wd:location"/>       
     <xsl:value-of select="current-grouping-key(), current-group()!(wd:hours, wd:salcount, wd:hrlycount, wd:cwrcount)" separator=","/>
     <xsl:value-of select="$NEWLINE"/>    
  </xsl:for-each-group>

or (XSLT 2)

 <xsl:for-each-group select="wd:Report_Data/wd:Report_Entry" group-by="wd:location">
     <xsl:sort select="wd:location"/>       
     <xsl:value-of select="current-grouping-key(), for $e in current-group() return ($e/wd:hours, $e/wd:salcount, $e/wd:hrlycount, $e/wd:cwrcount)" separator=","/>
     <xsl:value-of select="$NEWLINE"/>    
  </xsl:for-each-group>

I am not sure why the question is tagged as xslt-1.0 if it uses xsl:for-each-group introduced in XSLT 2.0

  • Related