File: xpath-sample.cpp

package info (click to toggle)
libzeep 5.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,596 kB
  • sloc: cpp: 27,393; xml: 7,798; javascript: 180; sh: 37; makefile: 8
file content (29 lines) | stat: -rw-r--r-- 827 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//[ xpath_example

#include <iostream>
#include <zeep/xml/document.hpp>
#include <zeep/xml/xpath.hpp>

int main()
{
    using namespace zeep::xml::literals;

    auto doc = R"(<bar xmlns:z="https://www.hekkelman.com/libzeep">
        <z:foo>foei</z:foo>
    </bar>)"_xml;

    /*<< Create an xpath context and store our variable >>*/
    zeep::xml::context ctx;
    ctx.set("ns", "https://www.hekkelman.com/libzeep");

    /*<< Create an xpath object with the specified XPath using the variable `ns` >>*/
    auto xp = zeep::xml::xpath("//*[namespace-uri() = $ns]");

    /*<< Iterate over the result of the evaluation of this XPath, the result will consist of zeep::xml::element object pointers >>*/
    for (auto n: xp.evaluate<zeep::xml::element>(doc, ctx))
        std::cout << n->str() << std::endl;

    return 0;
}
//]