【问题标题】:xsl loop to split a fragment tree every n timesxsl循环每n次分割一个片段树
【发布时间】:2010-12-17 18:18:25
【问题描述】:

这是我的 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>

我想要的是某种循环,它允许我为每 100 个项目创建一个标题,而不管产品类型如何。这很容易,但在这个标题中,我想要一个直接在该标题下的 100 个项目的总和。

目前我试图将树分成 100 个组,以便我可以对每个组执行求和函数。我已经在这个问题上待了几天,但我未能提供一个完整的工作解决方案。

我的输出应该是这样的(如果 n 是 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

【问题讨论】:

  • 我真的不知道根本你想要的输出如何与你的输入相对应。请详细说明。
  • 我已经让我的输出准确地反映了输入。请看一看。
  • 我目前正在尝试调整您对另一个问题的回答,我会看看情况如何。

标签: xml recursion xslt tree loops


【解决方案1】:

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

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

干杯 卡洛斯

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2018-10-30
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 2020-06-09
    相关资源
    最近更新 更多