XPath Function


$XPath function in WAPT supports all features of XML Path Language. In particular, you can use it to select nodes or node-sets in the XML document by specifying some path expression. The node is selected by following the specified path through the document.

Follow the steps below to add a new variable calculated as a result of $XPath function.

Click the Add button in the list of variables displayed on the Response processing tab of request properties.



This button opens the Add Variable dialog where you can create a new variable and select a function for its calculation. Enter the name of new variable and click the Add button to add a function.



You will see the Add Function dialog where you should specify function properties.

Type: Select XPath as function type.

XPath: Here you should enter the sequence of opening tags that stand before a desired node or node-set in the XML document. This sequence can contain variables. Use $Var(var-name) function to insert the necessary variables in the path expression. Enter the $ sign in the XPath field and you will see a prompt with the list of available variables (defined on the previous pages). Select a desired variable in the list to insert it in the path expression.

Only value: Leave this option unchecked if you wish to select the desired node together with the opening and closing tags. If you need to receive only value (without the opening and closing tags), you should check this option.

Now you can insert the newly created variable in the XML code of subsequent requests.

The Syntax of XPath Function


$XPath function has the following syntax:

$XPath({path})

Here path is the sequence of opening tags that stand before the desired node or node-set.
In this case the desired node will be selected together with the opening and closing tags.

$XPath({path},1)

In this case the same node will be selected without the opening and closing tags.

Examples of Usage


Example 1.

Suppose that the body of server response to some request is the following XML document:



And you define a variable var1 equal to

$XPath(/s:Envelope/s:Header/a:RelatesTo)

to get the content of a:RelatesTo node from this XML document.


Here we test the work of $XPath function by clicking the Test button. A new window opens with the result of function work on the basis of recorded server response (received during the recording process).

So, the var1 variable is equal to

<a:RelatesTo xmlns:a="http://www.w3.org/2005/08/addressing">
urn:uuid:885d7fd7-0aac-4b31-99d3-412cce99770f</a:RelatesTo>


Example 2.

In the previous example you could define the $XPath function in the following way:

$XPath(/s:Envelope/s:Header/a:RelatesTo,1)

In this case the option Only value is turned on in function properties:


And the var1 variable is equal to

urn:uuid:885d7fd7-0aac-4b31-99d3-412cce99770f

Example 3.

WAPT supports the usage of namespaces in XML documents. Here you can read more about XML namespaces.

If XML document contains namespaces, you should also specify namespaces in the path of $XPath function to find the necessary element. In this example we will show how $XPath function works with namespaces.

Suppose that the body of server response to some request is the following XML document:



Here you can see namespaces bk and isbn. Let's receive the value of number element of isbn namespace. We define $XPath function in the following way:

$XPath(/bk:book/isbn:number)



This function will return the value:

<isbn:number xmlns:isbn="urn:ISBN:0-395-36341-6">1568491379</isbn:number>.

Click the Test button in function properties to see how it works:



You can turn on the option Only value in function properties if you need to receive only value (without the opening and closing tags):



Now the result of this function is equal to 1568491379.



If you need to receive the value of title element of bk namespace, you should define $XPath function in the following way:

$XPath(/bk:book/bk:title)


You can turn on the option Only value in function properties if you need to receive only value (without the opening and closing tags):


Example 4.

WAPT supports the usage of default namespaces in XML documents. There is a special default namespace in WAPT which is called DEFNS. It is used in the path of $XPath function to find elements which can be accessed only with the help of default namespaces. If your XML code contains some element contained within the default namespace, you should use DEFNS to access it. If an XML document has several default namespaces, you can access them with the help of corresponding WAPT default namespaces: DEFNS, DEFNS1, DEFNS2 etc.

In this example you can see how $XPath function works with DEFNS. Suppose that the body of server response to some request is the following XML document:



Let's receive the value of <d:RawPersonId> element. It is the value of userPid variable:

userPid=$XPath(/s:Envelope/s:Body/DEFNS:AuthorizeResponse/DEFNS:AuthorizeResult/b:Person/DEFNS1:PersonId/d:RawPersonId,1)



We turn on the option Only value in function properties to receive only value (without the opening and closing tags).

This function returns the value 19800101TF01. Click the Test button in function properties to see how it works:



Example 5.

If some namespace has several occurrences in the XML document, then the number of its occurrence is included in the path expression, for example: NS - for the first occurrence, NS1 - for the second occurrence, NS2 - for the third occurrence and so on.

Suppose that the body of server response to some request is the following XML document:


<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
	<s:Body>
		<AuthorizeResponse xmlns="http://tempuri.org/">
			<AuthorizeResult xmlns:b="http://schemas.datacontract.org/2004/07/WE.ElderlyCare.Services.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
				<b:Person xmlns:c="http://schemas.datacontract.org/2004/07/WE.ElderlyCare.Services.Contracts.Entities">
					<Address xmlns="http://schemas.datacontract.org/2004/07/WE.ElderlyCare.Services.Contracts.Models">
						<c:CareOfAddress></c:CareOfAddress>
						<c:City></c:City>
						<c:StreetAddress></c:StreetAddress>
						<c:ZipCode></c:ZipCode>
					</Address>
				</b:Person>
				<b:Roles xmlns:c="http://schemas.datacontract.org/2004/07/WE.Security">
					<c:Role>
						<c:Groups>
							<c:FunctionGroup>
								<c:Description i:nil="true"></c:Description>
								<c:IconClass>hmm</c:IconClass>
								<c:Id>Misc</c:Id>
								<c:IsVisible>true</c:IsVisible>
								<c:Name>Övrigt</c:Name>
								<c:Order>997</c:Order>
							</c:FunctionGroup>
						</c:Groups>
						<c:Name>EC.Utförarpersonal</c:Name>
						<c:RoleId>EC.Utförarpersonal</c:RoleId>
					</c:Role>
				</b:Roles>
			</AuthorizeResult>
		</AuthorizeResponse>
	</s:Body>
</s:Envelope>

To find the <c:RoleId> tag inside the second occurrence of c namespace, you should use the following path:

/s:Envelope/s:Body/DEFNS:AuthorizeResponse/DEFNS:AuthorizeResult/b:Roles/c1:Role/c1:RoleId

Next page