Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl sum sibling values

Tags:

xslt

i have xml:

<people>
  <man age="20" />
  <man age="40" />
  <man age="30" />
  <man age="80" />
<people>

with xsl i am trying to output:

first age:20
first and second age (combined): 60
first second and third age(combined) :110
first second third and fouth age(combined) :190

i know how to select the ages, but how do i add them together and write it out?

Also note that the <man> elements can be more than just 4.

like image 620
stoic Avatar asked Dec 27 '22 20:12

stoic


2 Answers

Ok, I just read that you just need the numbers, so the following stripped 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>

produces following on your sample input-

20
60
90
170

The template (Original one with labels and stuff):

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

produces following output for your input xml:

first : 20
first  and second : 60
first second  and third : 90
first second third  and fourth : 170

I have added comments inside, let me know if you need further help. It basically uses two recursive templates one each for the 'labels' and other for the addition.

And, Your sample output should read 90 and 170 instead of 110 and 190 or your sample input should say age=50 instead of age=30

like image 129
d-live Avatar answered Jan 23 '23 10:01

d-live


The following short stylesheet produces exactly the output you first asked for, including the ordinal numbers:

Stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
    version="2.0">

<xsl:output method="text"/>

<xsl:template match="/" >
   <xsl:for-each select="people/man">
      <xsl:for-each select=".|preceding-sibling::man">
         <xsl:value-of select="if (position() = last() and last() != 1) 
                               then ' and ' else ' '"/>
         <xsl:number format="w" ordinal="yes"/>
      </xsl:for-each>
      <xsl:text> age </xsl:text>
      <xsl:if test="position() > 1">(combined)</xsl:if>
      <xsl:value-of select="':', sum((.|preceding-sibling::man)/@age), '&#xa;'"/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Output:

 first age : 20 
 first and second age (combined): 60 
 first second and third age (combined): 90 
 first second third and fourth age (combined): 170 
like image 33
Michael Kay Avatar answered Jan 23 '23 10:01

Michael Kay