question

GregM avatar image
GregM asked GregM posted

How to extract the index of a response

Given an response such as:

 

 

Block 1 - here's my value: 1000
Block 2 - here's my value: 11
Block 3 - here's my value: 2
Block 4 - here's my value: 17

 

I'd need a query that tells me which block the value "2" appears in.  The query "(.//myValue)[. = 2]" finds the block, but doesn't return the block index.

iTestresponse map
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 PaulD posted

Assuming that you are extracting the block number from the same line as, say, "blockNumber", and these are both under the same node, say, "block", then you can probably do something like this:

 

//blockNumber[../myValue = '2']

 

But I agree with dclaar that one would need more context to be able to fully answer this.

1 comment
10 |950

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

KumarS avatar image KumarS commented ·

XPATH does not have a built in function to get the index of a node in a tree. This is because the document on which XPATH function operates on is a "tree" and it is hard to define an index for a node. XPATH does have a function called "position()" to find the "nth" item from a list of matched nodes. So you can do:

 

//block[position() = 2] 

 

and it will return the second block node. So in some sense, you can iterate over a list of blocks using iTest "query" command and XPATH "position" function.

0 Likes 0 ·
dclaar avatar image
dclaar Suspended answered dclaar Suspended posted

At least for me, you need to give a bit more context. Do you have a response map? Is it an array?

I think it would be something like //line[myValue='2']/Block, assuming that each line matches the line XPath, and then Block and myValue are pieces of that line.

10 |950

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

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.