xmlwrapp
Lightweight C++ XML parsing library
|
Context in which XPath expressions can be evaluated. More...
#include <xpath.h>
Public Member Functions | |
xpath_context (const xml::document &doc) | |
Create XPath context for the given document. More... | |
void | register_namespace (const std::string &prefix, const std::string &href) |
Register a namespace with prefix. More... | |
const_nodes_view | evaluate (const std::string &expr, error_handler &on_error=throw_on_error) |
Execute an XPath query in the document scope. More... | |
const_nodes_view | evaluate (const std::string &expr, const xml::node &n, error_handler &on_error=throw_on_error) |
Execute an XPath query in the scope of XML node n. More... | |
nodes_view | evaluate (const std::string &expr, xml::node &n, error_handler &on_error=throw_on_error) |
Execute an XPath query in the scope of XML node n. More... | |
Context in which XPath expressions can be evaluated.
The context object can (and indeed, should) be reused for multiple XPath queries on the same document.
xml::xpath_context::xpath_context | ( | const xml::document & | doc | ) |
Create XPath context for the given document.
doc | Document for the context to work with. The lifetime of the document must be greater than the context object's. |
const_nodes_view xml::xpath_context::evaluate | ( | const std::string & | expr, |
error_handler & | on_error = throw_on_error |
||
) |
Execute an XPath query in the document scope.
Calling this function is exactly equivalent to using the overload taking an xml::node argument with xml::document::get_root_node().
In particular, this implies that if you need to modify nodes in the returned set, you can simple use the non-const overload taking xml::node& argument and pass xml::document::get_root_node() result to it.
const_nodes_view xml::xpath_context::evaluate | ( | const std::string & | expr, |
const xml::node & | n, | ||
error_handler & | on_error = throw_on_error |
||
) |
Execute an XPath query in the scope of XML node n.
expr | XPath expression. |
n | The context node for the expression. |
on_error | Error handler throwing an exception if an error occurs during the expression evaluation. Notice that absence of matches isn't considered to be an error, in this case the function just returns an empty set. |
nodes_view xml::xpath_context::evaluate | ( | const std::string & | expr, |
xml::node & | n, | ||
error_handler & | on_error = throw_on_error |
||
) |
Execute an XPath query in the scope of XML node n.
This overload is identical to the one taking const n argument, except that it returns a set of nodes that can be modified.
void xml::xpath_context::register_namespace | ( | const std::string & | prefix, |
const std::string & | href | ||
) |
Register a namespace with prefix.
This function has to be called in order to be able to evaluate XPath expressions that match nodes in a non-default namespace. Must be called before evaluate().
prefix | The prefix used in XPath expressions for the namespace. (Notice that it doesn't have to be the same prefix as used in the XML document.) |
href | URI of the namespace used in the document. |