Search code examples
xsltcounterxslt-3.0

Multiple Counters in XSLT


I need to maintain 2 counters in my xslt - EntryID and RowID. I have a xml which contains Name and Certifications the person holds. Now I need to maintain one counter for each person (EntryID) and one for each certification (RowID). And on top of the certifications, I need to add one more certification "New" which will have a RowID one number higher than the maximum number certifications the person holds.

Below is the XML:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/bsvc">
    <wd:Report_Entry>
        <Name>Ram</Name>
        <Certifications>
            <Certificate>AWS</Certificate>
            <Certificate>Workday</Certificate>
            <Certificate>SAP</Certificate>
        </Certifications>    
    </wd:Report_Entry>
    <wd:Report_Entry>
        <Name>Nitin</Name>
        <Certifications>
            <Certificate>Workday</Certificate>
        </Certifications>    
    </wd:Report_Entry>
    <wd:Report_Entry>
        <Name>Joe</Name>
        <Certifications>
            <Certificate>SAP</Certificate>
            <Certificate>AWS</Certificate>
        </Certifications>    
    </wd:Report_Entry>
</wd:Report_Data>

The expected output is below. The name should appear only in the first row.

EntryID,Name,RowID,Certification
1,Ram,1,AWS
1,,2,Workday
1,,3,SAP
1,,4,NEW          --> New certificate with row id 4 as Ram already has 3 certifications
2,Nitin,1,Workday --> Entry ID is 2 for Nitin and Row ID restarts from 1
2,,2,NEW          
3,Joe,1,SAP
3,,2,AWS
3,,3,NEW          

The XSLT I am able to build so far, but not giving desired output.

<?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/bsvc" 
    xmlns:etv="urn:com.workday/etv"  
    exclude-result-prefixes="xs wd" version="3.0">
   
    <xsl:output method="text" indent="yes"/>  
    
    <xsl:template match="/">
        <xsl:variable name="delimeter" select="','"/>
        <xsl:variable name="lineFeed" select="'&#xa;'"/>
        <root>                
            <xsl:for-each select="wd:Report_Data/wd:Report_Entry">
                
                <EntryID><xsl:value-of select="position()"/></EntryID>
                <xsl:value-of select="$delimeter"/>
                
                <Name><xsl:value-of select="Name"/></Name>
                <xsl:value-of select="$delimeter"/>                    
                
                <xsl:for-each select="Certifications/Certificate"> 
                    <RowID><xsl:value-of select="position()"/></RowID>
                    <xsl:value-of select="$delimeter"/>
                    <xsl:value-of select="."/>
                </xsl:for-each>
                <xsl:value-of select="$lineFeed"/>
                
                <EntryID><xsl:value-of select="position()"/></EntryID>
                <xsl:value-of select="$delimeter"/>
                <xsl:value-of select="$delimeter"/>
                <text>NEW</text>
                <xsl:value-of select="$lineFeed"/>                                 
            </xsl:for-each>                
        </root>            
    </xsl:template>        
</xsl:stylesheet>

Please help me with correct XSLT.


Solution

  • The XML result you show can be produced quite easily using:

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday.report/bsvc" 
    exclude-result-prefixes="wd">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/wd:Report_Data">
        <root>
            <xsl:for-each select="wd:Report_Entry">
                <row>
                    <EntryID>
                        <xsl:value-of select="position()" />
                    </EntryID>
                    <Name>
                        <xsl:value-of select="Name" />
                    </Name>
                    <xsl:for-each select="Certifications/Certificate">
                        <Certificate>
                            <RowID>
                                <xsl:value-of select="position()" />
                            </RowID>
                            <cName>
                                <xsl:value-of select="." />
                            </cName>
                        </Certificate>
                    </xsl:for-each>
                    <Certificate>
                        <RowID>
                            <xsl:value-of select="count(Certifications/Certificate) + 1" />
                        </RowID>
                        <cName>NEW</cName>
                    </Certificate>
                </row>
            </xsl:for-each>
        </root>
    </xsl:template>
    
    </xsl:stylesheet>
    

    To get a "flat" CSV output, you can do:

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday.report/bsvc">
    <xsl:output method="text" encoding="UTF-8"/>
    
    <xsl:template match="/wd:Report_Data">
        <!-- header -->
        <xsl:text>EntryID,Name,RowID,Certification&#10;</xsl:text>
        <!-- data -->
        <xsl:for-each select="wd:Report_Entry">
            <xsl:variable name="entry-data">
                <xsl:value-of select="position()" />
                <xsl:text>,</xsl:text>
                <xsl:value-of select="Name" />
                <xsl:text>,</xsl:text>
            </xsl:variable>
            <!-- output -->
            <xsl:for-each select="Certifications/Certificate">
                <xsl:copy-of select="$entry-data"/>
                <xsl:value-of select="position()" />
                <xsl:text>,</xsl:text>
                <xsl:value-of select="." />
                <xsl:text>&#10;</xsl:text>
            </xsl:for-each>
            <!-- new certificate -->
            <xsl:copy-of select="$entry-data"/>
            <xsl:value-of select="count(Certifications/Certificate) + 1" />
            <xsl:text>,NEW&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    

    which in XSLT 3.0 can be reduced to:

    <xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:wd="urn:com.workday.report/bsvc" 
    expand-text="yes">
    <xsl:output method="text" encoding="UTF-8"/>
    
    <xsl:template match="/wd:Report_Data">
        <!-- header -->
        <xsl:text>EntryID,Name,RowID,Certification&#10;</xsl:text>
        <!-- data -->
        <xsl:for-each select="wd:Report_Entry">
            <xsl:variable name="entry-data">{position()},{Name}</xsl:variable>
            <!-- output -->
            <xsl:for-each select="Certifications/Certificate, 'NEW'">{$entry-data},{position()},{.}&#10;</xsl:for-each>
        </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>