Adding a calculation to an XSLT Transformation file

Viewed 37

I created a report and I am using a transformation to display the data in a certain format (.csv). When the file is opened, there are two fields, Employee Id and SSN. In instances there are two of the same record, I would like to add a third field to identify the record count of each row. For example, I would like the output in .csv to display as:

Employee ID    SSN    Row Count
00001          2342    1
00001          2342    2
00003          0202    1
00004          9349    1
00005          4567    1
00005          1234    2

Is it possible to create a calculation within the XSL transformation?

Here is the code that I am using and it works without error.

<xsl:output method="text"/>
<xsl:variable name="NEWLINE" select="'&#xD;&#xA;'"/>
<xsl:variable name="DELIMITER" select="','"/>
<xsl:variable name="QUOTE">"</xsl:variable>

<!--Function: FormatPhone - Remove non-number characters and add in dashes.
        Will also remove the +1 country code if it is present.-->
    <xsl:function name="local:FormatPhone">
        <xsl:param name="phone"/>
        <xsl:variable name="strippedNbr" select="substring(replace(replace(replace($phone, '[^0- 
    9]', ''), '^1', ''),'(\d{3})(\d{3})(\d{4})','$1-$2-$3'),1,12)"/>
        <xsl:value-of select="$strippedNbr"/>
    </xsl:function>
    
    
    <xsl:template match="/">
        <xsl:call-template name="header"/>
        <xsl:call-template name="body"/>
    </xsl:template>


    <xsl:template name="header">
        <xsl:value-of select="concat($QUOTE,'EmployeeID',$QUOTE,$DELIMITER)"/>
        <xsl:value-of select="concat($QUOTE,'SSN',$QUOTE,$DELIMITER)"/>
        <xsl:value-of select="$NEWLINE"/>
    </xsl:template>


    <xsl:template name="body">
        <xsl:for-each select="/wd:Report_Data/wd:Report_Entry">
            <!--Start of employee rows-->
            <xsl:value-of select="concat($QUOTE,wd:EmployeeID,$QUOTE,$DELIMITER)"/>
            <xsl:value-of select="concat($QUOTE,wd:SSN,$QUOTE)"/>
            <xsl:value-of select="$NEWLINE"/> 
        </xsl:for-each>

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

Any help is appreciated.

Updated: @Michael, thank you for the response. Here is the source code

I appreciate your response. The source code is below. I ran a report for our system and the XML is:

<wd:Report_Data xmlns:wd="urn:com.sql.report/Workers">
<wd:Report_Entry>

    <wd:EmployeeID>047098</wd:EmployeeID>
    <wd:firstName>Joseph</wd:firstName>
    <wd:LastName>Montana</wd:LastName>
    <wd:SSN>888888</wd:SSN>
    <wd:Gender>M</wd:Gender>
    <wd:MaritalStatus>S</wd:MaritalStatus>
    <wd:Birthdate>1948-10-14-07:00</wd:Birthdate>
</wd:Report_Entry>
<wd:Report_Entry>

    <wd:EmployeeID>047098</wd:EmployeeID>
    <wd:firstName>Joseph</wd:firstName>
    <wd:LastName>Montana</wd:LastName>
    <wd:SSN>888888</wd:SSN>
    <wd:Gender>M</wd:Gender>
    <wd:MaritalStatus>S</wd:MaritalStatus>
    <wd:Birthdate>1948-10-14-07:00</wd:Birthdate>
</wd:Report_Entry>
</wd:Report_Data>
1 Answers

You haven't shown your source XML, so it's hard to see exactly what you mean by "two of the same record". Same EmployeeID? Same SSN? or both?

But it's a standard grouping question. In XSLT 2.0 use

<xsl:for-each-group select="/wd:Report_Data/wd:Report_Entry"
   group-by="wd:SSN (: or whatever :)">
            <!--Start of employee rows-->
            <xsl:value-of select="concat($QUOTE,wd:EmployeeID,$QUOTE,$DELIMITER)"/>
            <xsl:value-of select="concat($QUOTE,wd:SSN,$QUOTE,$DELIMITER)"/>
            <xsl:value-of select="count(current-group())"/> 
            <xsl:value-of select="$NEWLINE"/>

</xsl:for-each-group>

If you're still on XSLT 1.0, and can't move forwards, then read about Muenchian grouping.

Related