Forum Discussion
Thanks for your suggestion KarelHusa but here is an example of just one of the elements I want to inspect. You can see the [1] in the response .. but there are (in this example I am looking at) 7 entries I need to check to see if a match exists. Xpath (without writing a script) can only point to 1 value .. unless I am mistaken?
${AddService#Response#declare namespace ns2='http://xyz.com/schema'; //ns2:AddServiceResponse[1]/ns2:Response[1]/ns2:ServiceDetail[1]/ns2:TotalCost[1]}
- KarelHusa2 years agoChampion Level 2
you can use XPath predicates in your assertion, such as:
//ns2:BuyTotal/ns2:Amount[text() = 742.50]
will select the XML element amount with a value of 742.50.
You can also check the count of nodes:
count(//ns2:BuyTotal/ns2:Amount[text() = 742.50]) > 0
.
XPath gives you multiple options to select the node, see e.g. https://www.w3schools.com/xml/xpath_syntax.asp .
- thatsthat2 years agoContributor
Thanks for your patience KarelHusa .. I will need to do some more reading it appears (thanks for the URL you sent) .. I attempted to implement your graceful solution with the count .. but no luck:
count(//ns2:BuyTotal/ns2:Amount[text() = 742.50]) > 0
But to no avail .. the below is what I am using to match a 'single' amount record that is being returned .. but I can't get it to 'count' up all the records and find if > 0 of them = 742.50
${FastSearchPackage#Response#declare namespace ns2='http://www.xyz.com/gateway/fire/schema'; //ns2:SearchResponse[1]/ns2:Response[1]/ns2:Suppliers[1]/ns2:Supplier[1]/ns2:Products[1]/ns2:Product[1]/ns2:Rates[1]/ns2:RateTotals[1]/ns2:BuyTotal[1]/ns2:Amount[1]}
I get a 1 for 1 match .. i tried adding count blah blah but could not land the correct answer!!
I did also try with an 'Count' xpatj expression
declare namespace ns2='http://www.xyz.com/gateway/fire/schema'; ${FastSearchPackage#Response#declare namespace ns2='http://www.xyz.com/gateway/fire/schema'; //ns2:SearchResponse[1]/ns2:Response[1]/ns2:Suppliers[1]/ns2:Supplier[1]/ns2:Products[1]/ns2:Product[1]/ns2:Rates[1]/ns2:RateTotals[1]/count(ns2:BuyTotal[1]/ns2:Amount[text()=742.50])>0}
But no joy.
- KarelHusa2 years agoChampion Level 2
relying on a fixed position (using indexes in your XPath) in XML may be fragile. Next time you may get additional elements, and your XPath will no longer work.
That's where a query helps.
What kind of response do you get if you test the following expressions:
//ns2:BuyTotal/ns2:Amount[text() = 742.50]
count(//ns2:BuyTotal/ns2:Amount[text() = 742.50]) > 0
Related Content
- 2 years ago
- 11 months ago
- 9 years ago
- 8 years ago