Search code examples
xsltxml-parsingapache-camelxslt-2.0

XSLT dynamic Sum based on a parameter


I am calling this XSL from Apache Camel and I set the header of the message to the parameter, but still I don't get the result.

I want an XSLT which gives me the sum based on a parameter the problem is that the parameter could be multiple values or one value

<EmpJob>
<EmpJob>
<userId>testID</userId>
<EmpPayCompRecurring>
            <payComponent>1010</payComponent>
            <endDate>2020-06-30T00:00:00.000</endDate>
            <paycompvalue>3025.67</paycompvalue>
            <userId>testID</userId>
            <currencyCode>EUR</currencyCode>
            <startDate>2020-06-01T00:00:00.000</startDate>
        </EmpPayCompRecurring>
 <EmpPayCompRecurring>
            <payComponent>6097</payComponent>
            <endDate>2019-12-31T00:00:00.000</endDate>
            <paycompvalue>100.0</paycompvalue>
            <userId>testID</userId>
            <currencyCode>EUR</currencyCode>
            <startDate>2018-12-06T00:00:00.000</startDate>
        </EmpPayCompRecurring>
</EmpJob>
</EmpJob>

I created an XSLT transformation

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name = "custReturnDate" />
    <xsl:template match="/EmpJob">
        
         <xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="payrecur">'1010','6097' </xsl:variable>

     <mainroot>
     <xsl:for-each select="EmpJob">
            <root>
                    <__metadata>
                      
                        <uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
                    </__metadata>
                  
                    <customDouble13><xsl:value-of select="sum(EmpPayCompRecurring[$payrecur]/paycompvalue) "/></customDouble13>

                </root>
           
     
        </xsl:for-each>
    </mainroot>
    </xsl:template>
</xsl:stylesheet>

"payrecur" should be the parameter later on according to this parameter I should sum node values.

P.S. I can change the parameter to anything because I am calling the template from code.


Solution

  • Given XSLT 2 or 3, I would declare the parameter as a sequence of strings or integers and then compare sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue).

    So declare e.g <xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>.

    A minimal stylesheet would be

    <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:param name = "custReturnDate" />
        
        <xsl:param name="payrecur" as="xs:string*" select="'1010','6097'"/>
        
        <xsl:output indent="yes"/>
        
        <xsl:template match="/EmpJob">
            
             <xsl:variable name="apos">'</xsl:variable>
    
         <mainroot>
         <xsl:for-each select="EmpJob">
                <root>
                        <__metadata>
                          
                            <uri><xsl:value-of select="concat('EmpCompensation(seqNumber=1L,startDate=datetime',$apos ,$custReturnDate, $apos,',userId=',$apos,userId,$apos,')')"/></uri>
                        </__metadata>
                      
                        <customDouble13>
                            <xsl:value-of select="sum(EmpPayCompRecurring[payComponent = $payrecur]/paycompvalue)"/>
                            </customDouble13>
    
                    </root>
               
         
            </xsl:for-each>
        </mainroot>
        </xsl:template>
    </xsl:stylesheet>
    

    At https://xsltfiddle.liberty-development.net/bEzkTcM I get <customDouble13>3125.67</customDouble13>. You will probably want to add exclude-result-prefixes="#all" on the xsl:stylesheet element.