XPath, SUm of Attributes for certain Elemnets

Discussion in 'XML' started by Joe McIntier, Jan 13, 2004.

  1. Joe McIntier

    Joe McIntier Guest

    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.
     
    Joe McIntier, Jan 13, 2004
    #1
    1. Advertising

  2. 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 |
    -----------------------
     
    Philippe Poulard, Jan 13, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    667
  2. Replies:
    2
    Views:
    439
    Stan Kitsis [MSFT]
    Mar 24, 2005
  3. iliad
    Replies:
    4
    Views:
    1,958
    iliad
    Sep 25, 2005
  4. Replies:
    2
    Views:
    538
    bruce barker
    Mar 25, 2008
  5. SAN CAZIANO
    Replies:
    8
    Views:
    188
    Dr John Stockton
    Oct 15, 2004
Loading...

Share This Page