cancel
Showing results for 
Search instead for 
Did you mean: 

how to sum response values

Highlighted
Frequent Contributor

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 5
Highlighted
Super Contributor

Re: how to sum response values

Hi!

you should be able to use the xpath 2.0 sum function (http://www.saxonica.com/documentation/f ... n_sum.html), is this for an assertion or from inside a groovy-script?

regards!

/Ole
eviware.com
Highlighted
Frequent Contributor

Re: how to sum response values

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?
Highlighted
Frequent Contributor

Re: how to sum response values

something like this (although this doesnt work, doesn't like [$z]):

declare namespace fn='http://www.w3.org/2005/xpath-functions';
fn:sum(for $z in //ns1:QueryResponse[1]/ns1:Record[$z]/ens:Payment[1]/ens:Amount[1])
Highlighted
Super Contributor

Re: how to sum response values

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
Highlighted
Frequent Contributor

Re: how to sum response values

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!
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors