question

ChrisB avatar image
ChrisB asked mterry answered

Custom Query: How to get max when numbers have commas?

The XPath function max works great on numbers without punctuation. For example, max(//Mem_Usage) provided that there is a Mem_Usage column that will look like 1, 1578, or 1039274.

 

However, if those values are 1, 1,578, and 1,039,274 then all bets are off. I just get back NaN (not a number).

 

Any ideas on how to:

 - Have iTest ignore the comma's, so the structured data has just the numbers? A custom parser maybe? I'm using a Pattern map if that matters.

 - Write a more intelligent XPath statement so that commas will be stripped for the node-set sent to max? I've tried translate, but can't get it to work in conjuction with max; node-set/string value mismatch.

 - Any other ideas?

 

This seemed trivial at first glance, but I've spent hours now and I'm totally stumped! :-(

 

Regards,

chris

iTestresponse mapxpath
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mterry avatar image
mterry answered
The previous response did not format well. See screenshot. ![alt text][1] [1]: /storage/temp/3576-max_min.jpg

max_min.jpg (62.7 KiB)
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

PaulD avatar image
PaulD answered mterry commented

You can use the XPATH replace function to get rid of the quotes.  Then you may need the number function to get the numeric version of it. 

 

So something like:

 

number(replace(//Mem_Usage,",", ""))

 

(I haven't checked this.)

5 comments
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ChrisB avatar image ChrisB commented ·

I think you mean the translate function (there is no replace function).

 

Unfortunately this doesn't work. For example, I tried max(translate(//Mem_Usage,',','')) but it gave me this error in the queries window:

 

The argument to the max function must be a node-set

 

I believe the issue is that max expects a list of nodes, but translate returns a single string value.

 

I need some way of getting a list of nodes with the comma's removed, and I'm not sure it is possible in straight XPath. I had to settle for getting the list out and looping through it in my test case to find the max value by hand. Not elegant but it works...

 

The only way I can see to make this work with just a response map query is to have the response map parse out only the digits - that way the query would run against structured data containing numeric values without comma's in them. I'm not sure that's possible either.

 

Regards,

chris

0 Likes 0 ·
ChrisB avatar image ChrisB ChrisB commented ·

XPath 2.0 has a ton of new features that are very powerful. For example, For Expression would make short work of this problem. Does Fanfare plan to add XPath 2.0 support any time soon?

 

Regards,

chris

0 Likes 0 ·
PaulD avatar image PaulD ChrisB commented ·

That's possible.  Unfortunately, XPATH 2.0 is not purely backward-compatible with XPATH 1.0.  (You can read about this in the xpath spec here.)

 

It's possible, perhaps, to add a property to be able to choose which version to use -- but I'm sure you agree that we have to be very careful about creating problems for our existing account base.

0 Likes 0 ·
KumarS avatar image KumarS PaulD commented ·
In iTest 3.4, you can store the value in a variable and then use "split" and "max" functions to find the max. These are iTest interpreter functions and not XPATH functions.
0 Likes 0 ·
mterry avatar image mterry commented ·
Put this procedure in your iTest: procedure max (args: valueList) scriptEval package require math scriptSet valueList $valueList scriptEval set valueList [string map {, ""} $valueList] scriptEval set result [eval ::math::min $valueList] scriptGet result {$result} return $result call max -valueList "1, 1578, 1,039,274" (response will be 1039274)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.