English 中文(简体)
xsl loop to split a fragment tree every n times
原标题:

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>
    <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 &lt;= $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

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签