[xsl] The f:sqrt() and stdDev() functions of FXSL (Was: Re: RE: [xsl] Creating XPath2 functions:passing a set of nodes, what is the signature construction?)

Subject: [xsl] The f:sqrt() and stdDev() functions of FXSL (Was: Re: RE: [xsl] Creating XPath2 functions:passing a set of nodes, what is the signature construction?)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 26 Jan 2006 16:54:59 +1100
There's also an stdDev function (only coded as a template at present) in
FXSL.

Here's an example of using it:

When this transformation

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 >

 <xsl:import href="../f/stdDev.xsl"/>

 <!-- To be applied on numList.xml -->

 <xsl:output method="text"/>

  <xsl:template match="/">
   <xsl:call-template name="stdDev">
     <xsl:with-param name="pNums" select="/*/*"/>
   </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>

is applied on this source.xml

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

 the wanted result is produced:

Saxon 8.6.1 from Saxonica
Java version 1.5.0_04
Warning: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
Stylesheet compilation time: 516 milliseconds
Processing file:/C:\CVS-DDN\fxsl-xslt2\data\numList.xml
Building tree for file:/C:\CVS-DDN\fxsl-xslt2\data\numList.xml using
class net.sf.saxon.tinytree.TinyBuilder
Tree built in 0 milliseconds
Tree size: 35 nodes, 51 characters, 0 attributes
Building tree for file:/C:/CVS-DDN/fxsl-xslt2/f/stdDev.xsl using class
net.sf.saxon.tinytree.TinyBuilder
Tree built in 0 milliseconds
Tree size: 76 nodes, 304 characters, 37 attributes
Building tree for file:/C:/CVS-DDN/fxsl-xslt2/f/sqrt.xsl using class
net.sf.saxon.tinytree.TinyBuilder
Tree built in 0 milliseconds
Tree size: 78 nodes, 262 characters, 42 attributes
Building tree for file:/C:/CVS-DDN/fxsl-xslt2/f/withinRelative.xsl
using class net.sf.saxon.tinytree.TinyBuilder
Tree built in 0 milliseconds
Tree size: 64 nodes, 203 characters, 36 attributes
Execution time: 47 milliseconds
Memory used: 2837392
NamePool contents: 77 entries in 71 chains. 13 prefixes, 13 URIs
-------------------------------


3.027668803381241


The f:sqrt() function and stdDev templates have been part of FXSL for years.


Cheers,
Dimitre Novatchev.

On 1/26/06, Dimitre Novatchev <dnovatchev@xxxxxxxxx> wrote:
> There's an
>
>   f:sqrt()
>
> function in FXSL.
>
> Its signature:
>
>  <xsl:function name="f:sqrt" as="xs:double">
>    <xsl:param name="N"/>
>    <xsl:param name="Eps"/>
> ............................................................
>  </xsl:function>
>
> here the first argument is the double to be squared and the second
> argument is the desired accuracy (such as 0.0001)
>
> Example of its use:
>
> <xsl:stylesheet version="2.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>  xmlns:f="http://fxsl.sf.net/";
>  exclude-result-prefixes="f"
>
> >
>  <xsl:import href="../f/func-sqrt.xsl"/>
>
>  <!-- To be applied on any source xml.
>       This also tests the within() function
>  -->
>
>  <xsl:output indent="yes" omit-xml-declaration="yes"/>
>
>  <xsl:template match="/*" name="initial">
>     sqrt(0.25): <xsl:value-of select="f:sqrt(0.25, 0.00001)"/>
>     sqrt(1): <xsl:value-of select="f:sqrt(1, 0.00001)"/>
>     sqrt(2): <xsl:value-of select="f:sqrt(2, 0.00001)"/>
>     sqrt(4): <xsl:value-of select="f:sqrt(4, 0.00001)"/>
>     sqrt(9): <xsl:value-of select="f:sqrt(9, 0.00001)"/>
>     sqrt(13): <xsl:value-of select="f:sqrt(13, 0.00001)"/>
>     sqrt(16): <xsl:value-of select="f:sqrt(16, 0.00001)"/>
>     sqrt(25): <xsl:value-of select="f:sqrt(25, 0.00001)"/>
>     sqrt(36): <xsl:value-of select="f:sqrt(36, 0.00001)"/>
>     sqrt(49): <xsl:value-of select="f:sqrt(49, 0.00001)"/>
>     sqrt(64): <xsl:value-of select="f:sqrt(64, 0.00001)"/>
>     sqrt(81): <xsl:value-of select="f:sqrt(81, 0.00001)"/>
>     sqrt(100): <xsl:value-of select="f:sqrt(100, 0.00001)"/>
>     sqrt(121): <xsl:value-of select="f:sqrt(121, 0.00001)"/>
>  </xsl:template>
>
> </xsl:stylesheet>
>
>
> Result:
>
> Saxon 8.6.1 from Saxonica
> Java version 1.5.0_04
> Stylesheet compilation time: 500 milliseconds
> Processing file:/C:/Program%20Files/Java/jre1.5.0_04/bin/marrowtr.xml
> Building tree for
> file:/C:/Program%20Files/Java/jre1.5.0_04/bin/marrowtr.xml using class
> net.sf.saxon.tinytree.TinyBuilder
> Tree built in 0 milliseconds
> Tree size: 25 nodes, 31 characters, 10 attributes
> Building tree for file:/C:/CVS-DDN/fxsl-xslt2/f/func-sqrt.xsl using
> class net.sf.saxon.tinytree.TinyBuilder
> Tree built in 0 milliseconds
> Tree size: 80 nodes, 270 characters, 44 attributes
> Execution time: 125 milliseconds
> Memory used: 5076808
> NamePool contents: 62 entries in 59 chains. 11 prefixes, 12 URIs
> -------------------------------
>
>     sqrt(0.25): 0.5000000795866174
>     sqrt(1): 1.0000000464611474
>     sqrt(2): 1.4142156862745097
>     sqrt(4): 2
>     sqrt(9): 3.0000000000393214
>     sqrt(13): 3.6055512902583184
>     sqrt(16): 4.0000001858445895
>     sqrt(25): 5.000000000016778
>     sqrt(36): 6.000000002793968
>     sqrt(49): 7.000000094930961
>     sqrt(64): 8.000001273385879
>     sqrt(81): 9.000009415515176
>     sqrt(100): 10.000000000107445
>     sqrt(121): 11.000000001323027
>
> Cheers,
> Dimitre Novatchev
>
> On 1/26/06, cknell@xxxxxxxxxx <cknell@xxxxxxxxxx> wrote:
> > Thanks, I couldn't find that. On closer examination, it seems the standard
set of XPath2 functions does not include a square root function. I don't feel
up to writing my own, so I won't procede with the standard deviation
function.
> > --
> > Charles Knell
> > cknell@xxxxxxxxxx - email
> >
> >
> >
> > -----Original Message-----
> > From:     Xia Li <xli@xxxxxxxxxxxxx>
> > Sent:     Wed, 25 Jan 2006 13:46:02 -0800
> > To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> > Subject:  RE: [xsl] Creating XPath2 functions:passing a set of nodes, what
is the signature construction?
> >
> > You may use occurrence indicator "*" or "+" in <xsl:param>, such as
> >
> > <xsl:param ... as="xs:double*">
> >
> > to indicate that a sequence of double is expected.
> >
> >
> > Lisa
> >
> >
> > -----Original Message-----
> > From: cknell@xxxxxxxxxx [mailto:cknell@xxxxxxxxxx]
> > Sent: Wednesday, January 25, 2006 1:26 PM
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: [xsl] Creating XPath2 functions:passing a set of nodes, what is
> > the signature construction?
> >
> > I am learning XPath2 using Saxon 8.6. I'd like to create a funtion to
> > compute the standard deviation of the value of a set of nodes. Is there
> > some special way to construct the signature of the function when the
> > argument is a set of nodes as opposed to a single node?
> >
> > I have written a funtion that takes a single node as an argument of type
> > xs:double. Is there some special way to state that the function will
> > take a set of nodes of type xs:double, or do I simply state that the
> > argument will be of type xs:double?
> >
> > --
> > Charles Knell
> > cknell@xxxxxxxxxx - email
> >
> >
>
>
> --
> Cheers,
> Dimitre Novatchev
> ---------------------------------------
> To avoid situations in which you might make mistakes may be the
> biggest mistake of all.
>


--
Cheers,
Dimitre Novatchev
---------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all.

Current Thread