RE: [xsl] sequence as function parameter

Subject: RE: [xsl] sequence as function parameter
From: "Fabien Tillier" <f.tillier@xxxxxxxx>
Date: Mon, 4 Jul 2011 17:07:01 +0200
Hi David.
Nice one. Simpler, probably tougher (tests if a sequence is empty...) and
working :)
I will keep this solution.
Thank you for your help
Best regards,
Fabien


-----Message d'origine-----
De : David Carlisle [mailto:davidc@xxxxxxxxx]
Envoyi : lundi 4 juillet 2011 17:02
@ : xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Cc : Fabien Tillier
Objet : Re: [xsl] sequence as function parameter

On 04/07/2011 15:46, Fabien Tillier wrote:
> Hi List.
> I am trying to create a simple function to multiply each item in a
> sequence by each other (like a sum()).
> My problem is that I am unable to have the parameter treated as a node
> sequence (using saxon, so XSL2.0)
>
> I have
> <xsl:template name=" multiplie">
> 	<xsl:param name="serie"/>
> 	<xsl:param name="result" as="xs:double"/>
> 	<!--some test output -->
> 	<xsl:value-of select="$serie"/>-<xsl:value-of
> select="$result"/>--
> 	###<xsl:value-of select="count($serie)"/>###
> 	<!-- -->
> 	<xsl:choose>
> 		<xsl:when test="count($serie) gt 1">
> 			<!-- make the multiplication between result and
> the first item in the serie, then remove it and call again -->
> 			<xsl:variable name="comp">
> 				<xsl:value-of select="number($serie[1])
> * $result"/>
> 			</xsl:variable>
> 			<xsl:call-template name=" multiplie">
> 			<xsl:with-param name="serie"><xsl:copy-of
> select="subsequence($serie,2)"/></xsl:with-param>
> 			<xsl:with-param name="result"><xsl:value-of
> select="$comp"/></xsl:with-param>
> 			</xsl:call-template>
> 		</xsl:when>
> 		<xsl:otherwise>
> 			<xsl:value-of select="number($serie[1]) *
> $result"/>
> 		</xsl:otherwise>
> 	</xsl:choose>
> </xsl:template>
>
> That I can call (for testing) with
> <xsl:variable name="seq" select="(6.8E-06,5.5E-06,7.1E-06,7.3E-06)"/>
> <xsl:value-of select=" multiplie($seq)"/>
>
> And the output I get
> #############################################
> 6.8E-06 5.5E-06 7.1E-06 7.3E-06
> #############################################
> 0.0000068 0.0000055 0.0000071 0.0000073-1--
> ####
> NaN
>
> #############################################
>
> So, the part where I do a count gets me 1, when I should have 4...
> I have tried with
> <xsl:param name="serie" as="node()*"/>  but I have an error message
> XTTE0790: Required item type of first argument of multiplie() is node();
> supplied
>    value has item type xs:double
>
> I am a bit out of ideas....
> Thank you in advance for any help.
>
> Best regards,
> Fabien
>


don't use value-of and don't use xsl:variable/xsl:param/xsl:with-param
with content rather than a select attribute.

you want a sequence of doubles but
value-of makes a text node and
<xsl:with-param name="serie"><xsl:copy-of
 > select="subsequence($serie,2)"/></xsl:with-param>

,,,

sets the param to a single document node with text child the string
value of the content.


I'd make this a function rather than a template something like


<xsl:function name="f:prod">
<xsl:param name="s"/>
<xsl:sequence select="if(empty($s)) then
		      1
		      else
		      $s[1] * f:prod($s[position()!=1])"/>
</xsl:function>


then <xsl:value-of select="f:prod((1,2,3,4))"/>

returns 24.

David


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

Current Thread