xmlwrapp Lightweight C++ XML parsing library

Public Member Functions | List of all members
xml::xpath_context Class Reference

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...

## Detailed Description

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.

Since
  0.8.0

## Constructor & Destructor Documentation

## ◆ xpath_context()

xml::xpath_context::xpath_context | ( | const xml::document & | doc | ) |

Create XPath context for the given document.

Parameters

  doc | Document for the context to work with. The lifetime of the document must be greater than the context object's.

## Member Function Documentation

## ◆ evaluate() [1/3]

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.

Parameters

  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.

Returns
  Const set of matching nodes.

## ◆ evaluate() [2/3]

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.

## ◆ evaluate() [3/3]

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.

## ◆ register_namespace()

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().

Parameters

  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.

The documentation for this class was generated from the following file:

- xmlwrapp/xpath.h