How To Use
 All Modules Pages
Selecting interesting parts with XPath

Overview

Sometimes you are only interested on specific parts of an XML tree. You can use functionality of XmlCheck and XPath expressions to select the data to be checked.

Sample data:

static const std::string SHORT_DEMO = R"(
<SomeNode>
<Child Id="1001">Contents of child 1</Child>
<Child Id="1002">Contents of child 2</Child>
<Child Id="1003">Contents of child 3</Child>
<Child Id="1004">Contents of child 4>
<SubChild color="red">Red contents</SubChild>
<SubChild color="blue">Blue contents</SubChild>
<SubChild color="green">Green contents</SubChild>
</Child>
<Child Id="1005">Contents of child 5</Child>
</SomeNode>
)";

Check first child element

XmlCheck::CheckSubNode implicitly looks for the first found node of a given name:

pugi::xml_document doc;
TTB::TheXmlCheck()->ReadFromString(doc, SHORT_DEMO);
TTB_INFO("Check first child (using relative xpath starting from root node)");
TTB::TheXmlCheck()->CheckSubNode(doc, "Child");
TTB_EXP("Child");
TTB_EXP(" Id: 1001");
TTB_EXP(" Contents of child 1");

Check second child element

Using XPath expression "SomeNodeName[2]" you can address the position of the element you are interested in:

TTB_INFO("\nCheck only second child (using separate call to NodeFromXPath with relative xpath)");
pugi::xml_node subNode = TTB::TheXmlCheck()->NodeFromXPath(doc.document_element(), "Child[2]");
TTB::TheXmlCheck()->CheckNode(subNode);
TTB_EXP("Child");
TTB_EXP(" Id: 1002");
TTB_EXP(" Contents of child 2");
TTB_INFO("\nCheck only second child (using absolute xpath)");
TTB::TheXmlCheck()->CheckSubNode(doc, "/SomeNode/Child[2]");
TTB_EXP("Child");
TTB_EXP(" Id: 1002");
TTB_EXP(" Contents of child 2");

Check first three child elements

XmlCheck::CheckSubNodeWithMaxChilds offers the possibility to check only a limited number of child entries:

TTB_INFO("\nCheck the first three childs");
TTB::TheXmlCheck()->CheckSubNodeWithMaxChilds(3, doc, "/SomeNode");
TTB_EXP("SomeNode");
TTB_EXP(" Child");
TTB_EXP(" Id: 1001");
TTB_EXP(" Contents of child 1");
TTB_EXP(" Child");
TTB_EXP(" Id: 1002");
TTB_EXP(" Contents of child 2");
TTB_EXP(" Child");
TTB_EXP(" Id: 1003");
TTB_EXP(" Contents of child 3");

Check last child element

Using XPath expression "SomeNodeName[last()]" you can search for the last child element:

TTB_INFO("\nCheck last child");
TTB::TheXmlCheck()->CheckSubNode(doc, "Child[last()]");
TTB_EXP("Child");
TTB_EXP(" Id: 1005");
TTB_EXP(" Contents of child 5");

Check only elements with given attribute values

Using XPath expression "SomeNodeName[@someAttributeName='someValue']" you can search for a child element with a specific attribute value:

TTB_INFO("\nCheck child with attribute Id=1003");
TTB::TheXmlCheck()->CheckSubNode(doc, "Child[@Id='1003']");
TTB_EXP("Child");
TTB_EXP(" Id: 1003");
TTB_EXP(" Contents of child 3");
TTB_INFO("\nCheck blue subchild of 4th child");
TTB::TheXmlCheck()->CheckSubNode(doc, "Child[4]/SubChild[@color='green']");
TTB_EXP("SubChild");
TTB_EXP(" color: green");
TTB_EXP(" Green contents");
TTB_INFO("\nCheck not existing black subchild of 4th child");
TTB::TheXmlCheck()->CheckSubNode(doc, "Child[4]/SubChild[@color='black']");
// No output, nothing to verify