This is a sample of my XML, it can possibly have thousands of rows of items in a range of categories.
<store>
  <products type="computer">
    <item desc="text" amount="99"></c>
    <item desc="text" amount="69.95"></c>
    <item desc="text" amount="4.50"></c>
    <item desc="text" amount="10"></c>
    <item desc="text" amount="9.99"></c>
    <item desc="text" amount="24"></c>
  </products>
  <products type="books">
    <item desc="text" amount="5"></c>
    <item desc="text" amount="9.99"></c>
    <item desc="text" amount="24"></c>
  </products>      
  <products type="music">
    <item desc="text" amount="5"></c>
    <item desc="text" amount="1"></c&开发者_JS百科gt;
    <item desc="text" amount="4.50"></c>
    <item desc="text" amount="10"></c>
    <item desc="text" amount="9.99"></c>
  </products>
</store>
What I want is some sort of loop that will allow me to create a header for every 100 items regardless of the product type. That's easy enough but in this header I want a sum of the amounts of just the 100 items directly under that header.
Currently I have tried to slpit the tree into groups of 100 so I can perform a sum function on every group. I have been a few days at this problem but I fail to produce a complete working solution.
My output should look something like this (if the n was 3):
Header Total=173.45
text,99
text,69.95
text,4.50
Header Total=43.99
text,10
text,9.99
text,24
Header Total=38.99
text,5
text,9.99
text,24
Header Total=10.50
text,5
text,1
text,4.50
Header Total=19.99
text,10
text,9.99
Probably not the most efficient approach, but here's one way:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:variable name="newline" select="'
'" />
<xsl:variable name="items" select="//item" />
<xsl:variable name="items-count" select="count($items)" />
<xsl:variable name="loop-size" select="3" />
<xsl:template match="/">
    <xsl:call-template name="group-sum" />
</xsl:template>
<xsl:template name="group-sum">
    <xsl:param name="position" select="1" />
    <xsl:param name="sum" select="0" />
    <xsl:param name="group" select="''" />
    <xsl:variable name="current" select="$items[$position]" />
    <xsl:if test="$position != 1 and 
        (($position - 1) mod $loop-size = 0 or $position = $items-count + 1)">
        <xsl:value-of select="concat('Header Total=', $sum, $newline, $group)" />
    </xsl:if>
    <xsl:choose>
        <xsl:when test="$position != 1 and ($position - 1) mod $loop-size = 0">
            <!-- Start a new group -->
            <xsl:call-template name="group-sum">
                <xsl:with-param name="position" select="$position + 1" />
                <xsl:with-param name="sum" select="$current/@amount" />
                <xsl:with-param name="group" select="concat($current/@desc, ',', $current/@amount, $newline)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:when test="$position <= $items-count">
            <!-- Append to the current group -->
            <xsl:call-template name="group-sum">
                <xsl:with-param name="position" select="$position + 1" />
                <xsl:with-param name="sum" select="$sum + $current/@amount" />
                <xsl:with-param name="group" select="concat($group, $current/@desc, ',', $current/@amount, $newline)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <!-- Finished -->
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
Cheers Carlos
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论