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 February 29th, 2012, 10:46 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Question XSLT1.0 Arithmetic Function

Hello All,

I have been struggling to do an Addition of 2 values in my XSLT v1.0. I have tried Sum (not a valid XSLT function it says), tried to add with a simple artihmetic operator (+) between nodes but is of no use.

I tired the following the following and many more
<TestData>
<xsl:value-of select="('/root/Line[2]/Element[2]')+'/root/Line[3]/Element[2]')"></xsl:value-of> </TestData>

Result: Value 'NaN' could not be coerced to the data type of field 'Testdata'. This Testdata has the datatype float.

Can anyone please guide me to bring this topic to closure with better understanding of what am i missing in my code?

The XML

<?xml version="1.0" encoding="utf-8"?>
<root version="4.5.0.534" Filename="actual.prn">
<Line Number="1">
<Element>ExpNo</Element>
<Element>TestData</Element>
<Element>Comment</Element>
<Element>Operator</Element>
</Line>
<Line Number="2">
<Element>A12804_4</Element>
<Element>12.3</Element>
<Element>nach WC</Element>
<Element>mwelz</Element>
<Element />
</Line>
<Line Number="3">
<Element>A12804_5</Element>
<Element>24.2</Element>
<Element>nach WC</Element>
<Element>mwelz</Element>
</Line>
</root>

Thanks in advance.
srcbie
 
Old February 29th, 2012, 10:53 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well don't put XPath expressions into string literals, so instead of
Code:
'/root/Line[2]/Element[2]')+'/root/Line[3]/Element[2]
simply use
Code:
/root/Line[2]/Element[2] + /root/Line[3]/Element[2]
And the name of the function is lower-case "sum" so
Code:
sum(/root/Line[2]/Element[2] | /root/Line[3]/Element[2])
is also possible.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 29th, 2012, 11:07 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Unhappy XSLT1.0 Arithmetic Function

Thanks for the quick guidance.

I tried <xsl:value-of select="sum(/root/Line[2]/Element[2] | /root/Line[3]/Element[2])"></xsl:value-of>

Sadly, it doesnt work.

Just to check, i tired <xsl:value-of select="/root/Line[2]/Element[2])"></xsl:value-of> and it worked. Where could I be doing wrong?
 
Old February 29th, 2012, 11:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

So what happens when "it doesn't work"? Do you get an error, which one exactly? I am afraid
Code:
<xsl:value-of select="/root/Line[2]/Element[2])"></xsl:value-of>
is not even syntactically correct (closed parenthesis ')' without opening one) so even if you say "it worked" I can't see why it should.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 29th, 2012, 11:24 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Martin, Sorry my mistake.

<xsl:value-of select="/root/Line[2]/Element[2]"></xsl:value-of> works with a value in the DB table.

The error i get is
"Value 'NaN' could not be coerced to the data type of field 'Testdata'." Testdata has a float datatype.
 
Old February 29th, 2012, 11:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>i tired <xsl:value-of select="/root/Line[2]/Element[2])"></xsl:value-of> and it worked.

I would expect this to give a syntax error message. If you got a syntax error, then it worked. If you got anything else, then it didn't.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 29th, 2012, 11:32 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
Originally Posted by srcbie View Post

<xsl:value-of select="/root/Line[2]/Element[2]"></xsl:value-of> works with a value in the DB table.

The error i get is
"Value 'NaN' could not be coerced to the data type of field 'Testdata'." Testdata has a float datatype.
Please explain in more detail in which context you use XSLT 1.0. I am not aware of any XSLT 1.0 doing validation of output at all so currently I don't understand why you get such error messages at all, usually an XSLT 1.0 processor simply outputs a result document (XML or HTML or plain text).
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
srcbie (February 29th, 2012)
 
Old February 29th, 2012, 11:56 AM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Here I go in detail, a robot/machine generates a source xml file as a result of its process. One XSLT transforms this source xml file to a resulting well formated xml file that gets loaded into the DB directly using some system services.

The source xml file structure example is what i have shown in my first message. There is a case where i have to add the two values and store it in DB via formated XML.

If I use <xsl:value-of select="/root/Line[2]/Element[2]"></xsl:value-of> then the resulting xml gets created and the values are stored correctly into the DB table.

If I use <xsl:value-of select="sum(/root/Line[2]/Element[2] | /root/Line[3]/Element[2])"></xsl:value-of> then I get an error "Value 'NaN' could not be coerced to the data type of field 'Testdata'."
If the sum function works correctly then a single value should be passed to the resulting xml and gets stored intothe DB table.

My point is the xslt function i use seems to be not working to do a simple arithmetic operation.
 
Old February 29th, 2012, 12:04 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If that is a real example of your source XML then the XPath you have used should work fine.

Here's a wild stab in the dark: Line[2] does not select the Line element where Number=2, just the second one in the list. For that you would want Line[@Number=2].
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
srcbie (February 29th, 2012)
 
Old February 29th, 2012, 12:24 PM
Registered User
 
Join Date: Feb 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="utf-8"?>
<root version="4.5.0.534" Filename="actual.prn">
<Line Number="1">
<Element>ExpNo</Element>
<Element>TestData</Element>
<Element>Comment</Element>
<Element>Operator</Element>
</Line>
<Line Number="2">
<Element>A12804_4</Element>
<Element>12.3</Element>
<Element>nach WC</Element>
<Element>mwelz</Element>
<Element />
</Line>
<Line Number="3">
<Element>A12804_5</Element>
<Element>24.2</Element>
<Element>nach WC</Element>
<Element>mwelz</Element>
</Line>
</root>
---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<body>
<h2>My test output</h2>
<table border="1">
<tr>
<th>TestData01</th>
<th>TestData02</th>
<th>TestData03</th>
</tr>
<tr>
<td><xsl:value-of select="/root/Line[@Number=2]/Element[2]"/></td>
<td><xsl:value-of select="(/root/Line[2]/Element[2] + /root/Line[3]/Element[2])"/></td>
<td><xsl:value-of select="sum(/root/Line[2]/Element[2] | /root/Line[3]/Element[2])"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------------------------
Result:
My test output
TestData01 TestData02 TestData03
12.3 36.5 36.5
----------------------------------
All your inputs and advices seems to be working correctly if i create a separate xml, xslt.
I have to investigate what wrong with the file i am ON. Anyways thanks a lot guys for your kind support.
Cheers,
srcbie

Last edited by srcbie; February 29th, 2012 at 12:26 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
grouping problem at xslt1.0 joshuaa XSLT 4 July 15th, 2008 08:08 AM
NESTED GROUPING USING XSLT1.0 jhansib4u Classic ASP XML 0 November 19th, 2007 09:12 AM
date functions in xslt1.0 pradeepn XSLT 4 July 26th, 2007 04:28 AM
multiple param values in XSLT1.0 NEO1976 XSLT 5 February 5th, 2007 07:43 AM
A problem with predicate in XSLT1.0:( sureshk XSLT 1 March 8th, 2006 01:13 PM





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