xmlwrapp
Lightweight C++ XML parsing library
Loading...
Searching...
No Matches
xpath.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Jonas Weber (mail@jonasw.de)
3 * 2013 Vaclav Slavik <vslavik@gmail.com>
4 * All Rights Reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of the Author nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/**
35 @file
36
37 This files contains definitions for XPath-related classes.
38 */
39
40#ifndef _xmlwrapp_xpath_h_
41#define _xmlwrapp_xpath_h_
42
43
44// xmlwrapp includes
45#include "xmlwrapp/init.h"
46#include "xmlwrapp/errors.h"
47#include "xmlwrapp/export.h"
48#include "xmlwrapp/nodes_view.h"
49
50#include <memory>
51#include <string>
52
53XMLWRAPP_MSVC_SUPPRESS_DLL_MEMBER_WARN
54
55namespace xml
56{
57
58class node;
59class document;
60
61namespace impl
62{
63struct xpath_context_impl;
64}
65
66/**
67 Context in which XPath expressions can be evaluated.
68
69 The context object can (and indeed, should) be reused for multiple XPath
70 queries on the same document.
71
72 @since 0.8.0
73 */
74class XMLWRAPP_API xpath_context
75{
76public:
77 /**
78 Create XPath context for the given document.
79
80 @param doc Document for the context to work with. The lifetime
81 of the document must be greater than the context object's.
82 */
84
86
87 /**
88 Register a namespace with prefix.
89
90 This function has to be called in order to be able to evaluate XPath
91 expressions that match nodes in a non-default namespace. Must be
92 called before evaluate().
93
94 @param prefix The prefix used in XPath expressions for the namespace.
95 (Notice that it doesn't have to be the same prefix as
96 used in the XML document.)
97 @param href URI of the namespace used in the document.
98 */
99 void register_namespace(const std::string& prefix, const std::string& href);
100
101 /**
102 Execute an XPath query in the document scope.
103
104 Calling this function is exactly equivalent to using the overload
105 taking an xml::node argument with xml::document::get_root_node().
106
107 In particular, this implies that if you need to modify nodes in the
108 returned set, you can simple use the non-const overload taking
109 xml::node& argument and pass xml::document::get_root_node() result to
110 it.
111 */
112 const_nodes_view evaluate(const std::string& expr,
113 error_handler& on_error = throw_on_error);
114
115 /**
116 Execute an XPath query in the scope of XML node @a n.
117
118 @param expr XPath expression.
119 @param n The context node for the expression.
120 @param on_error Error handler throwing an exception if an error occurs
121 during the expression evaluation. Notice that absence of matches
122 isn't considered to be an error, in this case the function just
123 returns an empty set.
124
125 @return Const set of matching nodes.
126 */
127 const_nodes_view evaluate(const std::string& expr,
128 const xml::node& n,
129 error_handler& on_error = throw_on_error);
130
131 /**
132 Execute an XPath query in the scope of XML node @a n.
133
134 This overload is identical to the one taking const @a n argument,
135 except that it returns a set of nodes that can be modified.
136 */
137 nodes_view evaluate(const std::string& expr,
138 xml::node& n,
139 error_handler& on_error = throw_on_error);
140
141private:
142 // no copying
143 xpath_context(const xpath_context&) = delete;
144 xpath_context& operator=(const xpath_context&) = delete;
145
146 std::unique_ptr<impl::xpath_context_impl> pimpl_;
147};
148
149} // namespace xml
150
151XMLWRAPP_MSVC_RESTORE_DLL_MEMBER_WARN
152
153#endif // _xmlwrapp_xpath_h_
This class implements a read-only view of XML nodes.
Definition nodes_view.h:256
The xml::document class is used to hold the XML tree and various bits of information about it.
Definition document.h:88
The xml::error_handler class is used to handle libxml2 errors and warnings emitted during parsing,...
Definition errors.h:89
The xml::node class is used to hold information about one XML node.
Definition node.h:92
This class implements a view of XML nodes.
Definition nodes_view.h:82
Context in which XPath expressions can be evaluated.
Definition xpath.h:75
void register_namespace(const std::string &prefix, const std::string &href)
Register a namespace with prefix.
xpath_context(const xml::document &doc)
Create XPath context for the given document.
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.
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.
const_nodes_view evaluate(const std::string &expr, error_handler &on_error=throw_on_error)
Execute an XPath query in the document scope.
This file contains errors-handling classes: xml::exception and xml::error_handler and derived classes...
XML library namespace.
Definition attributes.h:55
This file contains the definition of the xml::nodes_view and xml::const_nodes_view classes.
This file contains the definition of the xml::init class.