XPath, SUm of Attributes for certain Elemnets

J

Joe McIntier

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

Philippe Poulard

Joe said:
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 |
-----------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top