xsl-list
[Top] [All Lists]

Re: [xsl] Getting average

2008-05-27 14:49:27

 I am using XSLT 1.0.
shame as it's _much_ easier in xslt2, as single function call:


 A double pass is not practical.
why not? doing two passes would be by far the easiest way in xslt 1.
Can you not use a x:node-set() extension function to make this
practical?

If neither is possible you have to do a recursive walk over the tree.

I have an XML file in the following format:
It helps if the samples are well formed, then anyone trying a solution
will use the same data.

here's a well formed input and a stylesheet that demonstrates the three
possible solution styles (written as xslt2 but the second will run in
xslt1 if you supply the appropriate namespace for the node-set
extension, and the third solution would run unchanged in xslt1



<customers>
        <group income="1000">
                <person age="10"/>
                <person age="15"/>
                <person age="30"/>
        </group>

        <group income="2000">
                <person age="10"/>
                <person age="40"/>
        </group>        
        
        <group income="5000">
                <person age="20"/>
                <person age="20"/>
        </group>
</customers>



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:x="data:,x">
 
<xsl:param name="age" select="10"/>

<xsl:function name="x:node-set">
 <xsl:param name="n"/>
 <xsl:sequence select="$n"/>
</xsl:function>

<xsl:template match="/">

 <xsl:text>&#10; xslt2            : </xsl:text>
 <xsl:value-of select="avg(for $p in customers/group/person[(_at_)age=$age] 
return $p/../@income)"/>

 <xsl:text>&#10; xslt1 (node set) : </xsl:text>
 <xsl:variable name="x">
  <xsl:for-each select="customers/group/person[(_at_)age=$age]">
    <i><xsl:value-of select="../@income"/></i>
  </xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(x:node-set($x)/i) div count(x:node-set($x)/i)"/>


 <xsl:text>&#10; xslt 1 (one pass): </xsl:text>
 <xsl:apply-templates select="(customers/group/person[(_at_)age=$age])[1]" 
mode="avg"/>
</xsl:template>

<xsl:template match="person" mode="avg">
<xsl:param name="s" select="0"/>
<xsl:param name="c" select="0"/>
<xsl:variable name="n" select="following::person[(_at_)age=$age][1]"/>
<xsl:choose>
 <xsl:when test="$n">
  <xsl:apply-templates select="$n" mode="avg">
   <xsl:with-param name="s" select="$s + ../@income"/>
   <xsl:with-param name="c" select="$c + 1"/>
  </xsl:apply-templates>
 </xsl:when>
 <xsl:otherwise>
  <xsl:value-of select="($s + ../@income) div ($c + 1)"/>
 </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>



$ saxon9 avg.xml avg.xsl age=10
<?xml version="1.0" encoding="UTF-8"?>
 xslt2            : 1500
 xslt1 (node set) : 1500
 xslt 1 (one pass): 1500

$ saxon9 avg.xml avg.xsl age=20
<?xml version="1.0" encoding="UTF-8"?>
 xslt2            : 5000
 xslt1 (node set) : 5000
 xslt 1 (one pass): 5000

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>