[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Re: RE: newbie: xsl:key and summation of substring

Subject: Re: Re: RE: newbie: xsl:key and summation of substrings
From: "Christopher Giblin" <CGI@xxxxxxxxxxxxxx>
Date: Mon, 10 Dec 2001 11:40:12 +0100
xlst substring
Thanks,  but your solution seems like home-brew  - I prefer  a portable
100% XLST solution.
Sorry if I misinterpret your recommendation.
Cheers, chris



                                                                                                                                 
                    Dimitre Novatchev                                                                                            
                    <dnovatchev@xxxxxxxxx>             To:     michael.h.kay@xxxxxxxxxxxx                                        
                    Sent by:                           cc:     xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                   
                    owner-xsl-list@xxxxxxxxxxxx        Subject:      Re: RE: newbie: xsl:key and summation of substrings    
                    rytech.com                                                                                                   
                                                                                                                                 
                                                                                                                                 
                    12/09/2001 09:34 PM                                                                                          
                    Please respond to xsl-list                                                                                   
                                                                                                                                 
                                                                                                                                 



> 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" --
transformAndSum:

http://sources.redhat.com/ml/xsl-list/2001-11/msg00831.html

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func-transform="f:func-transform"
exclude-result-prefixes="xsl func-transform"
>
   <xsl:import href="transform-and-sum.xsl"/>

   <xsl:output method="text"/>

   <func-transform:func-transform/>

    <xsl:template match="/">
      <xsl:call-template name="transform-and-sum">
        <xsl:with-param name="pFuncTransform"
                        select="document('')/*/func-transform:*[1]"/>
        <xsl:with-param name="pList" select="//freespace"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template match="func-transform:*">
      <xsl:param name="arg" select="0"/>
      <xsl:value-of select="substring-before($arg, ' ')"/>
    </xsl:template>

</xsl:stylesheet>


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

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

The result is:

1350


Hope this helped.

Cheers,
Dimitre Novatchev.




__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://greetings.yahoo.com

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






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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.