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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include <XPath/XPath.hpp>
#include <DOM/Simple/DOMImplementation.hpp>
#include <DOM/io/Stream.hpp>
#include <iostream>
#include <string>
#define string_type std::string
#define string_adaptor Arabica::default_string_adaptor<std::string>
typedef string_adaptor SA;
class NodeSetVariableResolver : public Arabica::XPath::VariableResolver<string_type, string_adaptor> {
//typedef string_adaptorstring_adaptor;
public:
virtual Arabica::XPath::XPathValue<string_type, string_adaptor> resolveVariable(const string_type& /* namepace_uri */,
const string_type& name) const {
using namespace Arabica::XPath;
VarMap::const_iterator n = map_.find(name);
if(n == map_.end())
throw UnboundVariableException(string_adaptor::asStdString(name));
return XPathValue<string_type, string_adaptor>(new NodeSetValue<string_type, string_adaptor>((*n).second));
} // resolveVariable
void setVariable(const string_type& name, const Arabica::XPath::NodeSet<string_type, string_adaptor>& value) {
map_[name] = value;
} // setVariable
private:
typedef std::map<string_type, Arabica::XPath::NodeSet<string_type, string_adaptor> > VarMap;
VarMap map_;
}; // class NodeSetVariableResolver
Arabica::XPath::XPath<string_type, string_adaptor> parser;
Arabica::DOM::DOMImplementation<string_type, string_adaptor> factory_;
Arabica::DOM::Document<string_type, string_adaptor> document_;
Arabica::DOM::Element<string_type, string_adaptor> root_;
Arabica::DOM::Element<string_type, string_adaptor> element1_;
Arabica::DOM::Element<string_type, string_adaptor> element2_;
Arabica::DOM::Element<string_type, string_adaptor> element3_;
Arabica::DOM::Element<string_type, string_adaptor> spinkle_;
Arabica::DOM::Attr<string_type, string_adaptor> attr_;
Arabica::DOM::Text<string_type, string_adaptor> text_;
Arabica::DOM::Comment<string_type, string_adaptor> comment_;
Arabica::DOM::ProcessingInstruction<string_type, string_adaptor> processingInstruction_;
Arabica::DOM::Document<string_type, string_adaptor> chapters_;
Arabica::DOM::Document<string_type, string_adaptor> numbers_;
class StringVariableResolver : public Arabica::XPath::VariableResolver<string_type, string_adaptor> {
public:
virtual Arabica::XPath::XPathValue<string_type, string_adaptor> resolveVariable(const string_type& /* namespace_uri */,
const string_type& name) const {
using namespace Arabica::XPath;
VarMap::const_iterator n = map_.find(name);
if(n == map_.end())
throw UnboundVariableException(string_adaptor::asStdString(name));
return XPathValue<string_type, string_adaptor>(new StringValue<string_type, string_adaptor>((*n).second));
} // resolveVariable
void setVariable(const string_type& name, const string_type& value) {
map_[name] = value;
} // setVariable
private:
typedef std::map<string_type, string_type> VarMap;
VarMap map_;
}; // StringVariableResolver
int main(int argc, char** argv) {
factory_ = Arabica::SimpleDOM::DOMImplementation<string_type, string_adaptor>::getDOMImplementation();
document_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
root_ = document_.createElement("root");
document_.appendChild(root_);
assert(root_);
element1_ = document_.createElement(SA::construct_from_utf8("child1"));
element2_ = document_.createElement(SA::construct_from_utf8("child2"));
element3_ = document_.createElement(SA::construct_from_utf8("child3"));
element1_.setAttribute(SA::construct_from_utf8("one"), SA::construct_from_utf8("1"));
element2_.setAttribute(SA::construct_from_utf8("one"), SA::construct_from_utf8("1"));
element2_.setAttribute(SA::construct_from_utf8("two"), SA::construct_from_utf8("1"));
element2_.setAttribute(SA::construct_from_utf8("three"), SA::construct_from_utf8("1"));
element2_.setAttribute(SA::construct_from_utf8("four"), SA::construct_from_utf8("1"));
text_ = document_.createTextNode(SA::construct_from_utf8("data"));
comment_ = document_.createComment(SA::construct_from_utf8("comment"));
processingInstruction_ = document_.createProcessingInstruction(SA::construct_from_utf8("target"), SA::construct_from_utf8("data"));
element2_.appendChild(text_);
spinkle_ = document_.createElement(SA::construct_from_utf8("spinkle"));
element2_.appendChild(spinkle_);
element2_.appendChild(comment_);
element2_.appendChild(processingInstruction_);
attr_ = element1_.getAttributeNode(SA::construct_from_utf8("one"));
root_.appendChild(element1_);
root_.appendChild(element2_);
root_.appendChild(element3_);
chapters_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
chapters_.appendChild(chapters_.createElement(SA::construct_from_utf8("document")));
chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("one")));
chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("two")));
chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("three")));
chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("four")));
chapters_.getFirstChild().appendChild(chapters_.createElement(SA::construct_from_utf8("chapter"))).appendChild(chapters_.createTextNode(SA::construct_from_utf8("five")));
numbers_ = factory_.createDocument(SA::construct_from_utf8(""), SA::construct_from_utf8(""), 0);
numbers_.appendChild(numbers_.createElement(SA::construct_from_utf8("doc")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("1")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("2")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("3")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("4")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("5")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("6")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("7")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("8")));
numbers_.getFirstChild().appendChild(numbers_.createElement(SA::construct_from_utf8("number"))).appendChild(numbers_.createTextNode(SA::construct_from_utf8("9")));
std::cout << document_ << std::endl;
std::cout << numbers_ << std::endl;
std::cout << chapters_ << std::endl;
if (false) {
using namespace Arabica::XPath;
StringVariableResolver svr;
svr.setVariable(SA::construct_from_utf8("index"), SA::construct_from_utf8("1"));
parser.setVariableResolver(svr);
XPathValue<string_type, string_adaptor> result = parser.evaluate(SA::construct_from_utf8("/root/*[@two = $index]"), document_);
assert(NODE_SET == result.type());
assert(element2_ == result.asNodeSet()[0]);
parser.resetVariableResolver();
} // test18
if (false) {
using namespace Arabica::XPath;
XPathExpression<string_type, string_adaptor> xpath = parser.compile(SA::construct_from_utf8("root/*[position() = 2]"));
XPathValue<string_type, string_adaptor> result = xpath.evaluate(document_);
assert(NODE_SET == result.type());
assert(1 == result.asNodeSet().size());
Arabica::DOM::Node<string_type, string_adaptor> n = result.asNodeSet()[0];
assert(element2_ == n);
} // test19
if (false) {
using namespace Arabica::XPath;
Arabica::DOM::DocumentFragment<string_type, string_adaptor> frag = document_.createDocumentFragment();
frag.appendChild(document_.createElement(SA::construct_from_utf8("foo")));
NodeSetVariableResolver svr;
NodeSet<string_type, string_adaptor> ns;
ns.push_back(frag);
svr.setVariable(SA::construct_from_utf8("fruit"), ns);
parser.setVariableResolver(svr);
XPathValue<string_type, string_adaptor> result = parser.evaluate_expr(SA::construct_from_utf8("$fruit/foo|/root/child3"), document_);
assert(NODE_SET == result.type());
assert(2 == result.asNodeSet().size());
assert(element3_ == result.asNodeSet()[0]);
} // testUnion11
if (false) {
using namespace Arabica::XPath;
XPathValue<string_type, string_adaptor> result = parser.evaluate_expr(SA::construct_from_utf8("local-name(/root)"), document_);
assert(STRING == result.type());
assert(SA::construct_from_utf8("root") == result.asString());
} // testLocalNameFn1
{
using namespace Arabica::XPath;
Arabica::DOM::DocumentFragment<std::string> frag = document_.createDocumentFragment();
frag.appendChild(document_.createElement("foo"));
NodeSetVariableResolver svr;
NodeSet<string_type, string_adaptor> ns;
ns.push_back(frag);
svr.setVariable("fruit", ns);
parser.setVariableResolver(svr);
XPathValue<string_type, string_adaptor> result = parser.evaluate(SA::construct_from_utf8("local-name($fruit/foo) = 'foo'"), document_);
std::cout << result.asBool() << std::endl;
}
}
|