get xml query result

Query an XML string by XPath.
NameDescriptionTypeModifier
xml

XML target string that the query runs against.

Tip: 
To access content from an XML file, use the get text file action to load the XML document into a variable, then reference the variable in this argument.

StringNone
xpath

XPath query string.

StringNone
variable

(Optional) Variable to receive the returned query result

StringNone
The following settings are applicable to this action: remove double quotes from cells.
This action is applicable to the following controls: none.
Example - Case 1: Using an XPath predicate in an XPath query

The following example retrieves the text from the title element of the first book element that is a child of a bookstore element.

<?xml version="1.0"?>
<bookstore>
  <book id="bk101" category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30</price>
  </book>
  <book id="bk102" category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>35</price>
  </book>
</bookstore>
Action lines
      
        	file	variable	
get text file	D:\bookstore.txt	bookstore	
&nbsp			
	xml	xpath	expected
get xml query result	# bookstore	/bookstore/book[1]/title/text()	book_title
      
  
Result
Example - Case 2: Using an XPath function in an XPath query

The following example returns the sum of all price nodes.

<?xml version="1.0"?>
<bookstore>
  <book id="bk101" category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30</price>
  </book>
  <book id="bk102" category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>35</price>
  </book>
</bookstore>
Action lines
      
        	file	variable	
get text file	D:\bookstore.txt	bookstore	
&nbsp			
	xml	xpath	expected
get xml query result	# bookstore	sum(/bookstore/book/price)	sum 
      
  
Result
Example - Case 3: Using a data set

The following example repetitively retrieves the text from the price element based on the title variable defined within a data set.
As an example, the first iteration of the use data set loop issues an XPath query of //price[../title="Everyday Italian"]/text().

Important: 
:To have TestArchitect parse a quotation mark, enter four consecutive quotation marks into the editor cell, that is, """".

<?xml version="1.0"?>
<bookstore>
  <book id="bk101" category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30</price>
  </book>
  <book id="bk102" category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>35</price>
  </book>
</bookstore>
Action lines
      
        	file	variable	
get text file	D:\bookstore.txt	bookstore	
&nbsp			
	name		
use data set	/books_ds		
&nbsp			
	xml	xpath	variable
get xml query result	# bookstore	#"//price[../title=" & """" & title & """" & "]/text()"	price
&nbsp			
repeat for data set			
      
  
Result
Example - Case 4: Passing an entire XML file’s content into the xml argument

The following example passes an entire well-formed XML string into the xml argument. Whitespace and line breaks between XML nodes within the document have been removed beforehand, as required.

Action lines
      
        	xml	xpath	variable
get xml query result	&lt;bookstore&gt;  &lt;book id="bk101" category="COOKING"&gt;    &lt;title lang="en"&gt;Everyday Italian&lt;/title&gt;    &lt;author&gt;Giada De Laurentiis&lt;/author&gt;    &lt;year&gt;2005&lt;/year&gt;    &lt;price&gt;30&lt;/price&gt;  &lt;/book&gt;  &lt;book id="bk102" category="CHILDREN"&gt;    &lt;title lang="en"&gt;Harry Potter&lt;/title&gt;    &lt;author&gt;J K. Rowling&lt;/author&gt;    &lt;year&gt;2005&lt;/year&gt;    &lt;price&gt;35&lt;/price&gt;  &lt;/book&gt;&lt;/bookstore&gt;	/bookstore/book[1]/title/text()	book_title
      
  
Result
Example - Case 5: Using get text line to extract content

The following example extracts a single XML line, <title lang=“en”>Harry Potter</title>, from a variable holding multiple XML lines by using the get text line action.

<?xml version="1.0"?>
<bookstore>
  <book id="bk101" category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30</price>
  </book>
  <book id="bk102" category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>35</price>
  </book>
</bookstore>
Action lines
      
        	file	variable	
get text file	D:\bookstore.txt	bookstore	
&nbsp			
	xml	xpath	variable
get xml query result	# bookstore	/bookstore/book[@category="CHILDREN"]	book_list
&nbsp			
	value	number	variable
get text file	#book_list	2	xml line 
      
  
Result
Example - Case 6: Adding a single root element

The following example encloses the returned XML document in a single root element to ensure a well-formed XML document. The modified XML document is then used to calculate the sum of all price nodes.

<?xml version="1.0"?>
<bookstore>
  <book id="bk101" category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30</price>
  </book>
  <book id="bk102" category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>35</price>
  </book>
</bookstore>
      
        	file	variable	
get text file	D:\bookstore.txt	bookstore	
&nbsp			
	xml	xpath	variable
get xml query result	# bookstore	/bookstore/book[@category="COOKING" or @category="CHILDREN"]	book_list
&nbsp			
	variable	value	
set global variable	book_list	#"&lt;root&gt;" & book_list & "&lt;/root&gt;"	
	xml	xpath	variable
get xml query result	#book_list	sum(/root/book/price)	sum
      
  
Result
Example - Case 7: Resolving the problem of namespaces

Let’s see an example of an XML file with a namespace:

<cr:cricketers xmlns:cr="http://www.example.com/">
   <cr:cricketer type="righty">
	<name>MS Dhoni</name>
	<role>Captain</role>
	<position>Wicket-Keeper</position>
   </cr:cricketer>
</cr:cricketers>

Use the local-name() function for your XPath, in order to retrieve the content of the <name> element of the cricketer node despite its enclosure in a namespace. For example, //*[local-name()=‘cricketer’]/name/text():

Action lines
      
        	file	variable	
get text file	D:\cricketTeam_info.xml	team	
&nbsp			
	xml	xpath	variable
get xml query result	# team	//*[local-name()='cricketer']/name/text()	name
      
  
Result
  • It is possible to insert an entire XML file’s content into the xml argument, however, all whitespace and line breaks between XML nodes must be removed manually to avoid automation failure (see example #4 above).
  • Depending upon the XPath query, some of the following notes for the variable argument may apply:
    • The returned value may contain multiple XML nodes. To extract or check the content of a single line from variable, use the get text line or check text line actions. (See example #5.)
    • If the returned value contains multiple XML nodes, and is to be used in another XPath query, ensure that it is well-formed XML by adding a single root element at the top level (see example #6).
  • If the variable in argument variable has not been declared, the action creates it as a global.
  • If the variable argument is left empty, TestArchitect supplies a global variable with the name _result.
  • If your XML target string has a namespace (defined by the xmlns attribute in the start tag of an element), yet no namespace is configured in the XPath expression, your query strings will not return any result. To resolve this namespace problem, you may use the local-name() function. This function returns the query results as if no namespace is declared in the target string (see example #7 above).
  • This action supports the <ignore> modifier. If the string <ignore> is present as the value of any of the arguments, or any argument contains an expression that evaluates to <ignore>, the action is skipped during execution.

Copyright © 2023 LogiGear Corporation. All rights reserved. LogiGear is a registered trademark, and Action Based Testing and TestArchitect are trademarks of LogiGear Corporation. All other trademarks contained herein are the property of their respective owners.

LogiGear Corporation

1730 S. Amphlett Blvd. Suite 200, San Mateo, CA 94402

Tel: +1(800) 322-0333