473,385 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XPath, SUm of Attributes for certain Elemnets

I am trying to sum the amounts attributes of a transaction element but
only for transaction elements where the SaleType attribute is equal to
SALE
XML Snippet:

<Root>
<Transaction Amount="1.23" SaleType="SALE">
<OptionalElelment/>
</Transaction>
<Transaction Amount="2.23" SaleType="SALE">
<OptionalElelment/>
</Transaction>
<Transaction Amount="3.23" SaleType="RETURN">
<OptionalElelment/>
</Transaction>
<Transaction Amount="4.23" SaleType="SALE">
<OptionalElelment/>
</Transaction>
</Root>

CODE Snippet:

//TRANS_TYPE_SALE is a constant = "SALE"

// Calculate the total of the SALE orders.
//<Transaction TransactionId="2" TransactionType="SALE"
// Amount="36.46">

XPathNavigator navSales = xDoc.CreateNavigator();
string xPathString = "sum(/Root/Transaction[@Transaction Type=" +
TRANS_TYPE_SALE.ToUpper() + "]/@Amount)";

XPathExpression exprSales = navSales.Compile( xPathString );
double totalAmount = (double) navSales.Evaluate(exprSales);
// Calculate the total of the RETURN orders.
//<Transaction TransactionId="2" TransactionType="RETURN"
// Amount="36.46">

XPathNavigator navReturns = xDoc.CreateNavigator();

XPathExpression exprReturns = navReturns.Compile (
"sum(/Root/Transaction/@Amount)");

double returnAmount = (double) navReturn.Evaluate(exprReturns);

The returnAmount is equal to all the Amoutns added up (expected but
not desired).

The totalAMount is equal to 0 (not expected and not desired).

I have tried many many combinations for the exprSales but have not
found the right combination that will let me sum the amounts when the
saletype = "SALES". I am quite sure that this is a simple querry but
i just cannot see the forest for the trees right now.

Any help is appreciated.

Nov 12 '05 #1
1 3639
Joe McIntier wrote:
I am trying to sum the amounts attributes of a transaction element but
only for transaction elements where the SaleType attribute is equal to


See answer in microsoft.public.xml newsgroup. And please, don't post the
same question to several newsgroups.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: Joe McIntier | last post by:
I am trying to sum the amounts attributes of a transaction element but only for transaction elements where the SaleType attribute is equal to SALE XML Snippet: <Root> <Transaction...
2
by: Marina | last post by:
Hi I have the following top most node: <Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
9
by: DBC User | last post by:
Hi, I have an xml and I am able to use xpath to identify each node that statisfy the selection criteria. I got the node list. I would like to know is it possible to do the following for the...
6
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return...
1
by: Sharat Koya | last post by:
I have a the following xml node. <doc tag1="a" tag2="b" tag3="c" docTag1="d" docTag2="e"/> I would like to output "abc" I have the following XPATH2.0 so far //doc/@*] which returns a node...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.