Header Ads Widget

Responsive Advertisement

How to increment a count and do sum of all data using xslt

To increment a count and sum all data using XSLT, you can use XSLT 1.0 or XSLT 2.0 features depending on your specific requirements and environment. Below are examples for both versions.

Example using XSLT 1.0

In XSLT 1.0, you typically use recursion and templates to achieve this, as it doesn't have built-in support for aggregating values in a straightforward way like XSLT 2.0.

Sample XML

xml

<data>

    <value>10</value>

    <value>20</value>

    <value>30</value>

</data>

 

XSLT 1.0 Stylesheet

xml

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" indent="yes"/>

   

    <!-- Template to match the root element -->

    <xsl:template match="/">

        <result>

            <xsl:call-template name="calculate-sum">

                <xsl:with-param name="nodes" select="//value"/>

                <xsl:with-param name="sum" select="0"/>

                <xsl:with-param name="count" select="0"/>

            </xsl:call-template>

        </result>

    </xsl:template>

   

    <!-- Recursive template to calculate the sum and count -->

    <xsl:template name="calculate-sum">

        <xsl:param name="nodes"/>

        <xsl:param name="sum"/>

        <xsl:param name="count"/>

       

        <xsl:choose>

            <xsl:when test="count($nodes) = 0">

                <total-sum><xsl:value-of select="$sum"/></total-sum>

                <total-count><xsl:value-of select="$count"/></total-count>

            </xsl:when>

            <xsl:otherwise>

                <xsl:variable name="first" select="$nodes[1]"/>

                <xsl:variable name="rest" select="$nodes[position() > 1]"/>

                <xsl:call-template name="calculate-sum">

                    <xsl:with-param name="nodes" select="$rest"/>

                    <xsl:with-param name="sum" select="$sum + number($first)"/>

                    <xsl:with-param name="count" select="$count + 1"/>

                </xsl:call-template>

            </xsl:otherwise>

        </xsl:choose>

    </xsl:template>

</xsl:stylesheet>

 

Example using XSLT 2.0

In XSLT 2.0, you can use the sum() function and more powerful XPath expressions, making the solution more straightforward.

Sample XML

Xml

<data>

    <value>10</value>

    <value>20</value>

    <value>30</value>

</data>

 

XSLT 2.0 Stylesheet

xml

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes"/>

   

    <!-- Template to match the root element -->

    <xsl:template match="/">

        <result>

            <total-sum><xsl:value-of select="sum(//value)"/></total-sum>

            <total-count><xsl:value-of select="count(//value)"/></total-count>

        </result>

    </xsl:template>

</xsl:stylesheet>

 

Explanation

XSLT 1.0

  1. Recursive Template: The calculate-sum template processes each <value> node recursively, adding its value to the running total (sum) and incrementing the count (count).
  2. Base Case: When no more nodes are left (count($nodes) = 0), it outputs the final sum and count.
  3. Variables: Temporary variables are used to hold the first node and the rest of the nodes for each recursive step.

XSLT 2.0

  1. sum() Function: The built-in sum() function calculates the sum of the <value> elements.
  2. count() Function: The count() function calculates the number of <value> elements.
  3. XPath Expressions: The XPath expressions (//value) are used to select the relevant nodes for summation and counting directly.

Conclusion

Using XSLT 2.0 is generally more straightforward and less error-prone for tasks like summing values and counting nodes due to its richer feature set. XSLT 1.0 requires more verbose and complex templates to achieve the same result but is still powerful for environments where only XSLT 1.0 is supported.

 

<AlertMessage>
 
<message>
<body>
<weeklyExpenseSumaryReport>
<projectedTransactionViews>
 <projectedTransactionView>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
   <siteName>DagBank</siteName>
  </transactionAcountView>
  <yMoney>
   <amount>1000</amount>
   <currencyCode>USD</currencyCode>
  </yMoney>
  <categorizationKeyword>iMusic Education</categorizationKeyword>
  <transactionCategory>
   <categoryId>6</categoryId>
   <categoryName>Education</categoryName>
   <categoryTypeId>3</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </transactionCategory>
  <postDate>12-05-2017</postDate>
  <currentDate>05-05-2017</currentDate>
  <transactionBaseType>credit</transactionBaseType>
  <dayLeft>2 Days</dayLeft>
 </projectedTransactionView>
 <projectedTransactionView>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
   <siteName>DagBank</siteName>
  </transactionAcountView>
  <yMoney>
   <amount>1000</amount>
   <currencyCode>USD</currencyCode>
  </yMoney>
  <categorizationKeyword>Global Charitable</categorizationKeyword>
  <transactionCategory>
   <categoryId>3</categoryId>
   <categoryName>Charitable Giving</categoryName>
   <categoryTypeId>3</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </transactionCategory>
  <postDate>12-05-2017</postDate>
  <currentDate>05-05-2017</currentDate>
  <transactionBaseType>credit</transactionBaseType>
  <dayLeft>2 Days</dayLeft>
 </projectedTransactionView>
 <projectedTransactionView>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
   <siteName>DagBank</siteName>
  </transactionAcountView>
  <yMoney>
   <amount>1000</amount>
   <currencyCode>USD</currencyCode>
  </yMoney>
  <categorizationKeyword>Global Reg. Salary</categorizationKeyword>
  <transactionCategory>
   <categoryId>75</categoryId>
   <categoryName>Salary</categoryName>
   <categoryTypeId>3</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </transactionCategory>
  <postDate>12-05-2017</postDate>
  <currentDate>05-05-2017</currentDate>
  <transactionBaseType>credit</transactionBaseType>
  <dayLeft>2 Days</dayLeft>
 </projectedTransactionView>
 <totalInflow>
  <totalInflowSign>+</totalInflowSign>
  <totalInflowAmount>20000</totalInflowAmount>
  <currencyCode>USD</currencyCode>
 </totalInflow>
 <totalOutflow>
  <totalOutflowSign>-</totalOutflowSign>
  <totalOutflowAmount>1500</totalOutflowAmount>
  <currencyCode>USD</currencyCode>
 </totalOutflow>
 <totalEvent>6</totalEvent>
 <toatlBillPayEvent>5</toatlBillPayEvent>
 <fromDate>08-05-2017</fromDate>
 <toDate>13-05-2017</toDate>
</projectedTransactionViews>
<itemSummaryResponse>
 
</itemSummaryResponse>
<expenseTransactions>
 <expenseTransaction>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
  </transactionAcountView>
  <facadeAmount>
   <amount>999.999</amount>
   <currencyCode>USD</currencyCode>
  </facadeAmount>
  <transactionBaseType>credit</transactionBaseType>
  <facadeTransactionCategory>
   <categoryId>27</categoryId>
   <categoryName>Deposite</categoryName>
   <categoryTypeId>2</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </facadeTransactionCategory>
  <txnDisplayDate>04-05-2017</txnDisplayDate>
 </expenseTransaction>
 <expenseTransaction>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
  </transactionAcountView>
  <facadeAmount>
   <amount>6.36</amount>
   <currencyCode>USD</currencyCode>
  </facadeAmount>
  <transactionBaseType>credit</transactionBaseType>
  <facadeTransactionCategory>
   <categoryId>96</categoryId>
   <categoryName>Interest Income</categoryName>
   <categoryTypeId>2</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </facadeTransactionCategory>
  <txnDisplayDate>04-05-2017</txnDisplayDate>
 </expenseTransaction>
 <expenseTransaction>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
  </transactionAcountView>
  <facadeAmount>
   <amount>250.0</amount>
   <currencyCode>USD</currencyCode>
  </facadeAmount>
  <transactionBaseType>credit</transactionBaseType>
  <facadeTransactionCategory>
   <categoryId>14</categoryId>
   <categoryName>Insurance</categoryName>
   <categoryTypeId>3</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </facadeTransactionCategory>
  <txnDisplayDate>04-05-2017</txnDisplayDate>
 </expenseTransaction>
 <expenseTransaction>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
  </transactionAcountView>
  <facadeAmount>
   <amount>59.99</amount>
   <currencyCode>USD</currencyCode>
  </facadeAmount>
  <transactionBaseType>credit</transactionBaseType>
  <facadeTransactionCategory>
   <categoryId>1</categoryId>
   <categoryName>Uncategorized</categoryName>
   <categoryTypeId>1</categoryTypeId>
   <categoryLevelId></categoryLevelId>
  </facadeTransactionCategory>
  <txnDisplayDate>04-05-2017</txnDisplayDate>
 </expenseTransaction>
 <expenseTransaction>
  <transactionAcountView>
   <accountName>ACC-SC1</accountName>
   <accountNickName>Kartik</accountNickName>
   <accountNumber>xxxx1234</accountNumber>
  </transactionAcountView>
  <facadeAmount>
   <amount>100.999</amount>
   <currencyCode>USD</currencyCode>
  </facadeAmount>
  <transactionBaseType>credit</transactionBaseType>
  <facadeTransactionCategory>
   <categoryId>11</categoryId>
   <categoryName>Healthcare/Medical</categoryName>
   <categoryTypeId>3</categoryTypeId>
   <categoryLevelId>3</categoryLevelId>
  </facadeTransactionCategory>
  <txnDisplayDate>04-05-2017</txnDisplayDate>
 </expenseTransaction>
</expenseTransactions>
</weeklyExpenseSumaryReport>
</body>
</message>
</AlertMessage>

 

XSLT 2.0 Stylesheet for count

xml

<!-- Total Bill and Not Bill Payment Count Start -->
   <xsl:variable name="totalEventCount"> 
         <xsl:for-each select="/AlertMessage/message/body/weeklyExpenseSumaryReport/projectedTransactionViews/projectedTransactionView">
   <xsl:variable name="transactionBaseType" select="transactionBaseType/text()" />
        <cost>
            <xsl:value-of select="123"/>
                 </cost> 
   </xsl:for-each>
   </xsl:variable>
 
   <xsl:variable name="totalEventData">
       <xsl:value-of select="count(exsl:node-set($totalEventCount)/cost)"/>
   </xsl:variable>

 

XSLT 2.0 Stylesheet for summation

xml

<!-- Total  Bill Payment sum Start -->
   <xsl:variable name="totalSumOfNotCredit"> 
         <xsl:for-each select="/AlertMessage/message/body/weeklyExpenseSumaryReport/projectedTransactionViews/projectedTransactionView">
   <xsl:variable name="transactionBaseType" select="transactionBaseType/text()" />
   <xsl:variable name="yMoneyAmount" select="yMoney/amount/text()" />
   <xsl:variable name="yMoneyCurrencyCode" select="yMoney/currencyCode/text()" />
    <xsl:if test="$transactionBaseType!=$cr">
        <cost>
            <xsl:value-of select="$yMoneyAmount"/>
                 </cost>
              </xsl:if>    
   </xsl:for-each>
   </xsl:variable>
 
   <xsl:variable name="totalSumOfNotCreditData">
       <xsl:value-of select="sum(exsl:node-set($totalSumOfNotCredit)/cost)"/>
   </xsl:variable>
 
   <!-- Total  Bill Payment sum End -->

 


increment a count and do sum
increment a count and do sum
















Post a Comment

0 Comments