Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 3rd, 2006, 09:05 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default Am I missing the obvious??

Hi,

I almost feel silly asking this question, but I all I want to do is add 4 numbers together in XSL.

for example the XML would be something like

Code:
<root>
  <val1>1</val2>
  <val2>2</val2>
  <val3>3</val3>
  <val4>4</val4>
XSL (easy)
Code:
  <xsl:value-of select="root/val1 + root/val2 ..etc" />
but if say val2 was empty then we get NaN.

I have tried writing a template that checks each value and if its not a number then it assigns as a zero, but I went wrong somewhere and got myself stuck in some recursive loop..

Any ideas??

Many Thanks

Darren

 
Old August 3rd, 2006, 09:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try sum(root/*[text()]) which sums the values of those children that have content.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 3rd, 2006, 09:34 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Ah, sorry I've not explained myself too good, I think it was my XML example that through it. the example i gave was a nice easy bit of xml, the actual xml is not so nice and the nodes are scattered all over the place, and could not even exsist for some cases.

so i need to check the node exsists, check the value is a number and add them all together..

Cheers

Darren

 
Old August 3rd, 2006, 10:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0, write a function:

<xsl:function name="f:default-number" as="xs:double">
  <xsl:param name="node" as="node()?"/>
  <xsl:sequence select="if ($node and ($node castable as xs:double))
       then number($node) else 0.0e0"/>
</xsl:function>

then apply this function to every node before including it in the summation.

In 1.0, as always, it's the same principle but a lot more tedious.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 4th, 2006, 03:17 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Excellent..

Cheers

Darren






Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there some ':, missing ??? saban SQL Server ASP 1 May 28th, 2007 06:32 AM
Obvious Oversite? TheTinSoldier C# 1 February 14th, 2007 08:52 PM
What is missing? grstad SQL Language 11 March 6th, 2006 01:53 AM
what am i missing? m002864 Crystal Reports 0 July 6th, 2004 07:24 AM
Missing code halifaxdal Wrox Book Feedback 2 May 26th, 2004 02:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.