coach_rob
coach_rob

Reputation: 901

XPath for parsing SOAP response

Given the below SOAP response, how would I use XPATH to do some testing/validation of the content of the response? NOTE: I am using RunScope to test our API.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetValidDataExtractResponse xmlns="http://some.namespace">
<GetValidDataForExtractResult>
<long>1001</long>
<long>1002</long>
  </GetValidDataForExtractResult>
</GetValidDataExtractResponse>
</soap:Body>
</soap:Envelope>

I can get a valid value back by using: /soap:Envelope/soap:Body But, this doesn't get me very far beyond "does something exist in the body". I'd like to be able to determine if the "GetValidDataExtractResponse" node contains something, also if the "etValidRentalUnitIdsForExtractResult" node contains X number of items or if that node contains certain values.

Upvotes: 3

Views: 2052

Answers (2)

Darrel Miller
Darrel Miller

Reputation: 142044

Ok, this is not pretty, but it may just work for you. Using the scripts capability in Runscope tests you can extract the values from the body. Here is an example that extracts the first "long" value.

var parser = new marknote.Parser();
var doc = parser.parse(response.body);

var envelope = doc.getRootElement();
var body = envelope.getChildElement("soap:Body");
var resp = body.getChildElement("GetValidDataExtractResponse");
var result = resp.getChildElement("GetValidDataForExtractResult");
var long = result.getChildElement("long");
variables.set("id", long.getText());

Upvotes: 1

StuartLC
StuartLC

Reputation: 107247

You can check for the existence of a child node as parent[child]. So here's some ideas, assuming you have a namespace alias x set up for http://some.namespace, and that you've made a typo in the closing tags):

  • "Find GetValidDataExtractResponse with a GetValidDataForExtractResult child":

x:GetValidDataExtractResponse[x:GetValidDataForExtractResult]
  • "Find GetValidDataForExtractResult with exactly two long children":

x:GetValidDataForExtractResult[count(x:long)=2]
  • Find the GetValidDataForExtractResult with a long child with a '1001' as a text value

x:GetValidDataForExtractResult[x:long/text()='1001']

I don't personally use RunScope, but I would imagine it has a way to test if an xpath nodes select returns zero nodes (or a null element for a single node select).

Upvotes: 1

Related Questions