xsl sum兄弟值

sto*_*oic 4 xslt

我有xml:

<people>
  <man age="20" />
  <man age="40" />
  <man age="30" />
  <man age="80" />
<people>
Run Code Online (Sandbox Code Playgroud)

用xsl我试图输出:

first age:20
first and second age (combined): 60
first second and third age(combined) :110
first second third and fouth age(combined) :190
Run Code Online (Sandbox Code Playgroud)

我知道如何选择年龄,但我如何将它们加在一起并写出来?

另请注意,<man>元素可以超过4个.

d-l*_*ive 6

好的,我刚刚读到你只需要数字,所以下面剥离了xslt

<xsl:stylesheet
        version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes" />

    <xsl:variable name="elements" select="/people/man"/>
    <xsl:variable name="count" select="count($elements)"/>

    <!-- Main Entry point -->
    <xsl:template match="/">
        <xsl:call-template name="addthem">
            <xsl:with-param name="pos" select="1"/>
            <xsl:with-param name="sum" select="$elements[1]/@age"/>
        </xsl:call-template>
    </xsl:template>

    <!-- recursive calling template to sum up the ages -->
    <xsl:template name="addthem">
        <xsl:param name="pos"/>
        <xsl:param name="sum"/>

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

        <!-- recursive call to sum up the ages -->
        <xsl:if test="$pos lt number($count)">
            <xsl:call-template name="addthem">
                <xsl:with-param name="pos" select="$pos + 1"/>
                <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

产生以下样本输入 -

20
60
90
170
Run Code Online (Sandbox Code Playgroud)

模板(带标签和东西的原始模板):

<xsl:stylesheet
        version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes" />

    <xsl:variable name="txtlabels" select="tokenize('first,second,third,fourth,fifth,sixth,seventh,eights,ninth,tenth,eleventh,twelveth,thirteenth,fourteenth,fifteenth', ',')"/>

    <!-- Util template to generate labels -->
    <xsl:template name="getlabel">
        <xsl:param name="startat" select="1"/>
        <xsl:param name="idx"/>

        <xsl:if test="number($startat) lt number($idx)">
            <xsl:value-of select="$txtlabels[$startat]"/>
            <xsl:text> </xsl:text>
            <xsl:call-template name="getlabel">
                <xsl:with-param name="startat" select="$startat + 1"/>
                <xsl:with-param name="idx" select="$idx"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>


    <!-- Main Entry point -->
    <xsl:template match="/">
        <xsl:variable name="count">
            <xsl:value-of select="count(/people/man)"/>
        </xsl:variable>

        <xsl:call-template name="addthem">
            <xsl:with-param name="count" select="count(/people/man)"/>
            <xsl:with-param name="pos" select="1"/>
            <xsl:with-param name="sum" select="/people/man[1]/@age"/>
            <xsl:with-param name="elements" select="/people/man"/>
        </xsl:call-template>
    </xsl:template>

    <!-- recursive calling template to sum up the ages -->
    <xsl:template name="addthem">
        <xsl:param name="count"/>
        <xsl:param name="pos"/>
        <xsl:param name="sum"/>
        <xsl:param name="elements"/>

        <!-- get the label prefix, without the 'and' clause -->
        <xsl:variable name="thelabelprefix">
            <xsl:call-template name="getlabel">
                <xsl:with-param name="startat" select="1"/>
                <xsl:with-param name="idx" select="$pos"/>
            </xsl:call-template>
        </xsl:variable>

        <!-- Now append the 'and' clause, if required, to the labels!!! -->
        <xsl:variable name="thelabel">
            <xsl:choose>
                <xsl:when test="number($pos) eq 1">
                    <xsl:value-of select="$txtlabels[$pos]"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of 
select="concat($thelabelprefix, ' and ', $txtlabels[$pos])"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

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

        </xsl:text>

        <!-- recursive call to sum up the ages -->
        <xsl:if test="$pos lt number($count)">
            <xsl:call-template name="addthem">
                <xsl:with-param name="count" select="$count"/>
                <xsl:with-param name="pos" select="$pos + 1"/>
                <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
                <xsl:with-param name="elements" select="$elements"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

为您的输入xml生成以下输出:

first : 20
first  and second : 60
first second  and third : 90
first second third  and fourth : 170
Run Code Online (Sandbox Code Playgroud)

我在里面添加了评论,如果您需要进一步的帮助,请告诉我.它基本上使用两个递归模板,每个模板用于"标签",另一个用于添加.

并且,您的样本输出应该是90和170而不是110和190,或者您的样本输入应该说年龄= 50而不是年龄= 30