XML: Extracting a variable

Handling returned XML content

Key Concepts

With an API that returns XML content, the XML can be parsed, manipulated and items extracted from either for a validation check in a specific test or as part of a workflow.

XPath Syntax

Standard XPath syntax is used.

ItemDescription
tagSelects all child elements with the given tag.

For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children named spam.
*Selects all child elements.

For example, */egg selects all grandchildren named egg.
.Selects the current node. This is mostly useful at the beginning of the path to indicate that it is a relative path.
//Selects all subelements on all levels beneath the current element.

For example, .//egg selects all egg elements in the entire tree.
..Selects the parent element.
[@attrib]Selects all elements that have the given attribute.
[@attrib='value']Selects all elements for which the given attribute has the given value. The value cannot contain quotes.
[tag]Selects all elements that have a child named tag. Only immediate children are supported.
[tag='text']Selects all elements that have a child named tag whose complete text content, including descendants, equals the given text.
[position]Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression last() (for the last position) or a position relative to the last position (e.g. last()-1).

📘

How the selector works

The selector returns the first matching element. After the first element, it treats the rest of the result like it would a JSON structure and anything after the ] is handled by the JSON parser.

Accessing Element Information

The XPath selector will return the first matching XML element. This object is then passed on to the JSON parser as the following object:

            {
                'tag': << element tag >>,
                'text': << element textual content >>,
                'attrib': << element attributes in a dictionary >>
            }

So, for instance, to obtain the attribute list from the first XML node called slide, 'slide|attire' could be entered.

Namespaces

XML elements must be addressed using a fully qualified tag, so the namespace must be included by using braces {}. The namespace must be specified by URI.

For example:

<foo:tag xmlns:foo="http://me.com/namespaces/foofoo"> 
  <foo:head> 
    <foo:title>An example document</foo:title> 
  </foo:head> 
  <foo:body> 
    <foo:e1>a simple document</foo:e1> 
    <foo:e2> 
      Another element 
    </foo:e2> 
  </foo:body> 
</foo:tag> 

Element e2 can be matched by:.//{http://me.com/namespaces/foofoo}e2

Selecting or Validating a Variable

<?xml version="1.0" ?>
<!--  A SAMPLE set of slides  -->
<slideshow author="Yours Truly" date="Date of publication" title="Sample Slide Show">
    <!-- TITLE SLIDE -->
    <slide type="all">
        <title style="bold">Wake up to WonderWidgets!</title>
    </slide>
</slideshow>

'.//*[@date]|tag' will match the slideshow element and return the tag slideshow.

'.//*[style]|attrib.style' will find the element (as it is the only tag with a style attribute) and then after the | the attribute style is selected.