XPath for parsing SOAP response
Given the SOAP answer below, how do I use XPATH to test or validate 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 return the correct value with: / soap: Envelope / soap: Body But that doesn't take me far beyond "something exists in the body". I want to determine if something contains a "GetValidDataExtractResponse" node, also if the "etValidRentalUnitIdsForExtractResult" node contains X number of elements or if the node contains certain values.
source to share
You can check if there is a child node like parent[child]
. So, some ideas, assuming you have a x
namespace alias set up for http://some.namespace
, and that you've made a typo in the closing tags):
- "Find
GetValidDataExtractResponse
with daughterGetValidDataForExtractResult
":
x:GetValidDataExtractResponse[x:GetValidDataForExtractResult]
- "Find
GetValidDataForExtractResult
exactly two childrenlong
:
x:GetValidDataForExtractResult[count(x:long)=2]
- Find
GetValidDataForExtractResult
with childlong
with "1001" as text value
x:GetValidDataForExtractResult[x:long/text()='1001']
I personally don't use it RunScope
, but I would imagine it has a way to check if the xpath nodes are selecting null nodes (or a null element for a single node selection).
source to share
Okay, this isn't pretty, but it might work for you. Using the scripting capabilities of Runscope tests, you can extract values ββfrom the body. Here's an example that retrieves 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());
source to share