Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xsl循环每n次拆分一个片段树_Xml_Recursion_Xslt_Tree_Loops - Fatal编程技术网

Xml xsl循环每n次拆分一个片段树

Xml xsl循环每n次拆分一个片段树,xml,recursion,xslt,tree,loops,Xml,Recursion,Xslt,Tree,Loops,这是我的XML示例,它可能在一系列类别中包含数千行项目 <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 de

这是我的XML示例,它可能在一系列类别中包含数千行项目

<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>

可能不是最有效的方法,但有一种方法:

<?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>
干杯
卡洛斯

我真的完全不知道你想要的输出与你的输入是如何对应的。请仔细解释。我已使输出准确反映输入。请看一看。我目前正在尝试将您的答案改编为另一个问题,我会看看它是如何进行的。
<?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>