[xsl] Re: RE: newbie: xsl:key and summation of substrings

Subject: [xsl] Re: RE: newbie: xsl:key and summation of substrings
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 9 Dec 2001 12:34:33 -0800 (PST)
> Here are three ways to sum over a computed quantity:
> * Create a result tree fragment containing nodes whose value is the computed
> number, and use sum(xx:node-set($rtf//value)) to do the summation, where
> xx:node-set() is your vendor's extension function for converting an RTF to a
> node-set
> * Use a recursive named template [this is the only standard XSLT 1.0
> solution]
> * Use the saxon extension function saxon:sum($nodes, saxon:expression(...))

> > I want to sum the "freespace", but sum(//disk/freespace)
> > won't work because
> > freespace is not a number due to the trailing "MB".

The first of the above solutions requires inappropriate amount of memory and time.

The third ties us to a proprietary (although one of the best) XSLT processor.

The second is too-general, as one can achieve almost anything in XSLT with
recursion. It would be almost equivalent to say: "Use arithmetic rules".

Here's another, more practical way, since it uses a "library function" --


This is ***the*** general solution for all such problems belonging to this
"transform and sum" class of problems...

Having such a general solution available in pure XSLT shows that any "dynamic
module" effort is very limited and therefore not applicable (== of any use) in the
general cases.

Re-Using the ready-made "transform and sum" function described in the above link
gives us the following:

<xsl:stylesheet version="1.0"
exclude-result-prefixes="xsl func-transform"
   <xsl:import href="transform-and-sum.xsl"/>

   <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:call-template name="transform-and-sum">
        <xsl:with-param name="pFuncTransform" 
        <xsl:with-param name="pList" select="//freespace"/>
    <xsl:template match="func-transform:*">
      <xsl:param name="arg" select="0"/>
      <xsl:value-of select="substring-before($arg, ' ')"/>


When this transformation is applied on the following (your) source xml:

    <freespace>1235 MB</freespace>
    <freespace>40 MB</freespace>
    <freespace>75 MB</freespace>

The result is:


Hope this helped.

Dimitre Novatchev.

Do You Yahoo!?
Send your FREE holiday greetings online!

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread