Previous Topic: Graphical XML Side-by-Side ComparisonNext Topic: Ensure XML Validation


XML XPath Assertion

The XML XPath assertion lets you use an XPath query that runs on the response. When this assertion is selected, the last response is loaded into the content panel.

Think of XPath as the "SQL for XML." XPath is a powerful query language that makes parsing the XML simple. XPath assertions are useful for validating a web service response in a more sophisticated way than simply parsing the entire result for a specific string. For example, you can ensure the second and third order item contains "ITKO" and the value of those line items is greater than ten.

You can view the response as an XML document or as a DOM Tree. However, you can only make the XPath selection from the DOM Tree view.

You can construct the XPath query in the following ways:

Complete the following fields:

Name

Defines the name of the assertion.

If

Specifies the behavior of the assertion from the drop-down list.

then

Specifies the step to which to redirect if the assertion fires.

Log

Identifies event text to print if the assertion fires.

Now construct the XPath query using one of the methods that was described previously.

After an XPath query has been constructed, test it by clicking the Run Assertion button on the top of the panel. The results of the query are displayed in the Query Results panel.

The previous example uses the fourth occurrence of the <wsdl:part> tag.

A common use case is to select an XPath node in a web service result and compare the node to a text value. You can then assert that a response contains the expected value. For this common use case, you can add an equality operator to the end of the initial expression that is provided. For example, if we select the new password that is returned in the response (BobPass):

DevTest builds the following XPath expression:

string(/env:Envelope/env:Body/ns2:updatePasswordResponse/[name()='return']/[name()='pwd'])=

Adding ='NewPassword' (as in the following example) compares the string of the result to the value of the property that is used to set the new password. If the equality test does not match, the assertion fails.

string(/env:Envelope/env:Body/ns2:updatePasswordResponse/[name()='return']/[name()='pwd'])='NewPassword'

This expression checks the web service response, looking for the new password and making sure it matches as expected.