“Mutation: it is the key to our evolution. It has enabled us to evolve from a single-celled organism into the dominant species on the planet. This process is slow, and normally taking thousands and thousands of years. But every few hundred millennia, evolution leaps forward. Prof.” – Charles Francis Xavier
XPath allows you to find different parts of XML documents. This is powerful, because it enables you to write expressions that return specific info in an XML document. Using VBScript in QTP or UFT will unleash this new power for you.
Xpath and Trees
The classic method for visualizing an XML document is to imagine an upside down tree. The trunk is considers the ROOT, and the branches are nodes. Location paths are a way to select the node(s) that you’re interested in. To try these examples download the xmen.xml
1. The Simplest Location Path
To return all the text values found in the XML’s nodes you can use a Root location path, which is a forward slash (/). For example, to return all the values in our XMEN XML we would use:
Should return all text values in the XML file
2. The Second Easiest Path – Child Elements
The Child element location allows you to obtain a single element name. It will select all child elements that match the name specified. So, to return the first person’s ‘name’ element value in our XMEN doc we would use:
To return the first XMEN name value match:
Result: Should return the name ‘Professor X’
3. To return the all the XMEN name values
Should return all the XMEN names
4. Elements Attributes
You can also return a particular attribute of an element using the @ sign.
To return all the ‘power’ elements text values that have ability type equal to two:
Should return all the power value “Superhuman senses.”
5. To return the attribute text value of an element:
Should return the type attribute value of 1
This should be enough to get you started. Congratulations!! You are now an official XMEN XPath QTP Mutant.
For everything you ever wanted to know about XPath check out: