Forum Discussion

Awesome's avatar
Awesome
Frequent Contributor
16 years ago

how to sum response values

hi all,

if i have a Response that contains many records, ex record:


            1001
           
              45
           



how can I collect all the inner elements of each record and run a Sum function all all. So in the above example, I would get the total amount of all purchases?

thanks!

5 Replies

  • Awesome's avatar
    Awesome
    Frequent Contributor
    I guess an assertion would be the way to go? sorry still learning
    thanks for your help!

    lets say the response returns 100 records each records with a different amount. if i know the total amounts should = 3000. then i guess an assertion that would sum up all the amounts of each records and make sure it == 3000.

    so to do this would I:
    in the TestRequest: add an assertion 'XPath Match'

    1) how would the XPath expression be formatted?

    if this is the query response for just the first record

    //ns1:QueryResponse[1]/ns1:Record[1]/ens:Payment[1]/ens:Amount[1]

    i guess i would need a var to plugin for Record[x] above? and loop through it and then sum at the end?

    so something like:

    {
    fn:sum(
    for $z in //ns1:QueryResponse[1]/ns1:Record[$z]/ens:Payment[1]/ens:Amount[1])
    }


    ---------------fn:sum-------------------------
    fn:sum($srcval as xs:anyAtomicType*) → xs:anyAtomicType?
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    Maybe the following should is sufficient;

    declare namespace ns1 = '...';
    declare namespace ens = '...';
    sum( //ns1:QueryResponse[1]/ns1:Record[$z]/ens:Payment[1]/ens:Amount[1]/text() )

    regards,

    /Ole
    eviware.com
  • Awesome's avatar
    Awesome
    Frequent Contributor
    yup some point late last night i figured out that this worked:

    sum(//ns1:QueryResponse/ns1:Record/ens:Payment/ens:Amount)


    i couldn't use $z as a var in the element index though..

    thanks!