Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath, SUm of Attributes for certain Elemnets

Reply
Thread Tools

XPath, SUm of Attributes for certain Elemnets

 
 
Joe McIntier
Guest
Posts: n/a
 
      01-13-2004
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.
 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      01-13-2004
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
> 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)";



string xPathString = "sum(/Root/Transaction[@SaleType=" +
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.



--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista cannot obtain an IP address from certain routers or from certain non-Microsoft DHCP Brian W Wireless Networking 7 01-31-2010 03:46 AM
Binding certain rows to certain columns in GridView? bernard.oflynn@gmail.com ASP .Net 2 03-25-2008 03:49 PM
Finding one certain line in a file is easy but how to look forheadlines and something just under this certain headline? kazaam Ruby 3 08-26-2007 03:34 PM
Expanding certain path to certain node in a JTree arun.hallan@gmail.com Java 0 01-08-2005 08:26 PM
field validation as float with certain integer and certain decimal SAN CAZIANO Javascript 8 10-15-2004 05:24 PM



Advertisments