0
0
mirror of https://github.com/zeux/pugixml.git synced 2024-12-27 13:33:17 +08:00

XPath: Rename xml_node::select_single_node to ::select_node

select_node is shorter and mistyping nodes as node or vice versa should
not lead to any issues since return types are substantially different.

select_single_node method still works and will be deprecated with an
attribute and removed at some point.

git-svn-id: https://pugixml.googlecode.com/svn/trunk@1065 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
Arseny Kapoulkine 2014-10-19 07:33:51 +00:00
parent f663558875
commit c3eb9c92a8
5 changed files with 51 additions and 25 deletions

View File

@ -11152,13 +11152,13 @@ namespace pugi
return !_impl; return !_impl;
} }
PUGI__FN xpath_node xml_node::select_single_node(const char_t* query, xpath_variable_set* variables) const PUGI__FN xpath_node xml_node::select_node(const char_t* query, xpath_variable_set* variables) const
{ {
xpath_query q(query, variables); xpath_query q(query, variables);
return select_single_node(q); return select_node(q);
} }
PUGI__FN xpath_node xml_node::select_single_node(const xpath_query& query) const PUGI__FN xpath_node xml_node::select_node(const xpath_query& query) const
{ {
return query.evaluate_node(*this); return query.evaluate_node(*this);
} }
@ -11173,6 +11173,17 @@ namespace pugi
{ {
return query.evaluate_node_set(*this); return query.evaluate_node_set(*this);
} }
PUGI__FN xpath_node xml_node::select_single_node(const char_t* query, xpath_variable_set* variables) const
{
xpath_query q(query, variables);
return select_single_node(q);
}
PUGI__FN xpath_node xml_node::select_single_node(const xpath_query& query) const
{
return query.evaluate_node(*this);
}
} }
#endif #endif

View File

@ -585,12 +585,17 @@ namespace pugi
#ifndef PUGIXML_NO_XPATH #ifndef PUGIXML_NO_XPATH
// Select single node by evaluating XPath query. Returns first node from the resulting node set. // Select single node by evaluating XPath query. Returns first node from the resulting node set.
xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = 0) const; xpath_node select_node(const char_t* query, xpath_variable_set* variables = 0) const;
xpath_node select_single_node(const xpath_query& query) const; xpath_node select_node(const xpath_query& query) const;
// Select node set by evaluating XPath query // Select node set by evaluating XPath query
xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = 0) const; xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = 0) const;
xpath_node_set select_nodes(const xpath_query& query) const; xpath_node_set select_nodes(const xpath_query& query) const;
// (deprecated: use select_node instead) Select single node by evaluating XPath query.
xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = 0) const;
xpath_node select_single_node(const xpath_query& query) const;
#endif #endif
// Print subtree using a writer object // Print subtree using a writer object

View File

@ -19,22 +19,22 @@ TEST_XML(xpath_api_select_nodes, "<node><head/><foo/><foo/><tail/></node>")
xpath_node_set_tester(ns2, "ns2") % 4 % 5; xpath_node_set_tester(ns2, "ns2") % 4 % 5;
} }
TEST_XML(xpath_api_select_single_node, "<node><head/><foo id='1'/><foo/><tail/></node>") TEST_XML(xpath_api_select_node, "<node><head/><foo id='1'/><foo/><tail/></node>")
{ {
xpath_node n1 = doc.select_single_node(STR("node/foo")); xpath_node n1 = doc.select_node(STR("node/foo"));
xpath_query q(STR("node/foo")); xpath_query q(STR("node/foo"));
xpath_node n2 = doc.select_single_node(q); xpath_node n2 = doc.select_node(q);
CHECK(n1.node().attribute(STR("id")).as_int() == 1); CHECK(n1.node().attribute(STR("id")).as_int() == 1);
CHECK(n2.node().attribute(STR("id")).as_int() == 1); CHECK(n2.node().attribute(STR("id")).as_int() == 1);
xpath_node n3 = doc.select_single_node(STR("node/bar")); xpath_node n3 = doc.select_node(STR("node/bar"));
CHECK(!n3); CHECK(!n3);
xpath_node n4 = doc.select_single_node(STR("node/head/following-sibling::foo")); xpath_node n4 = doc.select_node(STR("node/head/following-sibling::foo"));
xpath_node n5 = doc.select_single_node(STR("node/tail/preceding-sibling::foo")); xpath_node n5 = doc.select_node(STR("node/tail/preceding-sibling::foo"));
CHECK(n4.node().attribute(STR("id")).as_int() == 1); CHECK(n4.node().attribute(STR("id")).as_int() == 1);
CHECK(n5.node().attribute(STR("id")).as_int() == 1); CHECK(n5.node().attribute(STR("id")).as_int() == 1);
@ -42,20 +42,20 @@ TEST_XML(xpath_api_select_single_node, "<node><head/><foo id='1'/><foo/><tail/><
TEST_XML(xpath_api_node_bool_ops, "<node attr='value'/>") TEST_XML(xpath_api_node_bool_ops, "<node attr='value'/>")
{ {
generic_bool_ops_test(doc.select_single_node(STR("node"))); generic_bool_ops_test(doc.select_node(STR("node")));
generic_bool_ops_test(doc.select_single_node(STR("node/@attr"))); generic_bool_ops_test(doc.select_node(STR("node/@attr")));
} }
TEST_XML(xpath_api_node_eq_ops, "<node attr='value'/>") TEST_XML(xpath_api_node_eq_ops, "<node attr='value'/>")
{ {
generic_eq_ops_test(doc.select_single_node(STR("node")), doc.select_single_node(STR("node/@attr"))); generic_eq_ops_test(doc.select_node(STR("node")), doc.select_node(STR("node/@attr")));
} }
TEST_XML(xpath_api_node_accessors, "<node attr='value'/>") TEST_XML(xpath_api_node_accessors, "<node attr='value'/>")
{ {
xpath_node null; xpath_node null;
xpath_node node = doc.select_single_node(STR("node")); xpath_node node = doc.select_node(STR("node"));
xpath_node attr = doc.select_single_node(STR("node/@attr")); xpath_node attr = doc.select_node(STR("node/@attr"));
CHECK(!null.node()); CHECK(!null.node());
CHECK(!null.attribute()); CHECK(!null.attribute());
@ -411,4 +411,14 @@ TEST_XML(xpath_api_node_set_assign_out_of_memory_preserve, "<node><a/><b/></node
} }
#endif #endif
TEST_XML(xpath_api_deprecated_select_single_node, "<node><head/><foo id='1'/><foo/><tail/></node>")
{
xpath_node n1 = doc.select_single_node(STR("node/foo"));
xpath_query q(STR("node/foo"));
xpath_node n2 = doc.select_single_node(q);
CHECK(n1.node().attribute(STR("id")).as_int() == 1);
CHECK(n2.node().attribute(STR("id")).as_int() == 1);
}
#endif #endif

View File

@ -281,7 +281,7 @@ TEST_XML(xpath_variables_select, "<node attr='1'/><node attr='2'/>")
xpath_node_set ns = doc.select_nodes(STR("node[@attr=$one+1]"), &set); xpath_node_set ns = doc.select_nodes(STR("node[@attr=$one+1]"), &set);
CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); CHECK(ns.size() == 1 && ns[0].node() == doc.last_child());
xpath_node n = doc.select_single_node(STR("node[@attr=$one+1]"), &set); xpath_node n = doc.select_node(STR("node[@attr=$one+1]"), &set);
CHECK(n == ns[0]); CHECK(n == ns[0]);
} }

View File

@ -4,7 +4,7 @@
TEST_XML(xpath_xalan_axes_1, "<far-north><north-north-west1/><north-north-west2/><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-west/><near-south><south><far-south/></south></near-south><near-south-east/></center><near-east/><east/><far-east/></near-north></north><north-north-east1/><north-north-east2/></far-north>") TEST_XML(xpath_xalan_axes_1, "<far-north><north-north-west1/><north-north-west2/><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-west/><near-south><south><far-south/></south></near-south><near-south-east/></center><near-east/><east/><far-east/></near-north></north><north-north-east1/><north-north-east2/></far-north>")
{ {
xml_node center = doc.select_single_node(STR("//center")).node(); xml_node center = doc.select_node(STR("//center")).node();
CHECK_XPATH_NODESET(center, STR("self::*[near-south]")) % 10; CHECK_XPATH_NODESET(center, STR("self::*[near-south]")) % 10;
CHECK_XPATH_NODESET(center, STR("self::*[@center-attr-2]")) % 10; CHECK_XPATH_NODESET(center, STR("self::*[@center-attr-2]")) % 10;
@ -35,7 +35,7 @@ TEST_XML(xpath_xalan_axes_1, "<far-north><north-north-west1/><north-north-west2/
TEST_XML_FLAGS(xpath_xalan_axes_2, "<far-north> Level-1<north-north-west1/><north-north-west2/><!-- Comment-2 --> Level-2<?a-pi pi-2?><north><!-- Comment-3 --> Level-3<?a-pi pi-3?><near-north><far-west/><west/><near-west/><!-- Comment-4 --> Level-4<?a-pi pi-4?><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-west/><!--Comment-5--> Level-5<?a-pi pi-5?><near-south><!--Comment-6--> Level-6<?a-pi pi-6?><south attr1='First' attr2='Last'> <far-south/></south></near-south><near-south-east/></center><near-east/><east/><far-east/></near-north></north><north-north-east1/><north-north-east2/></far-north>", parse_default | parse_comments | parse_pi) TEST_XML_FLAGS(xpath_xalan_axes_2, "<far-north> Level-1<north-north-west1/><north-north-west2/><!-- Comment-2 --> Level-2<?a-pi pi-2?><north><!-- Comment-3 --> Level-3<?a-pi pi-3?><near-north><far-west/><west/><near-west/><!-- Comment-4 --> Level-4<?a-pi pi-4?><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-west/><!--Comment-5--> Level-5<?a-pi pi-5?><near-south><!--Comment-6--> Level-6<?a-pi pi-6?><south attr1='First' attr2='Last'> <far-south/></south></near-south><near-south-east/></center><near-east/><east/><far-east/></near-north></north><north-north-east1/><north-north-east2/></far-north>", parse_default | parse_comments | parse_pi)
{ {
xml_node center = doc.select_single_node(STR("//center")).node(); xml_node center = doc.select_node(STR("//center")).node();
CHECK_XPATH_NODESET(center, STR("@*")) % 21 % 22 % 23; CHECK_XPATH_NODESET(center, STR("@*")) % 21 % 22 % 23;
CHECK_XPATH_NODESET(center, STR("@*/child::*")); CHECK_XPATH_NODESET(center, STR("@*/child::*"));
@ -65,7 +65,7 @@ TEST_XML_FLAGS(xpath_xalan_axes_2, "<far-north> Level-1<north-north-west1/><nort
TEST_XML(xpath_xalan_axes_3, "<far-north><north><near-north><far-west/><west/><near-west/><center><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>") TEST_XML(xpath_xalan_axes_3, "<far-north><north><near-north><far-west/><west/><near-west/><center><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>")
{ {
xml_node center = doc.select_single_node(STR("//center")).node(); xml_node center = doc.select_node(STR("//center")).node();
CHECK_XPATH_NODESET(center, STR("ancestor-or-self::*")) % 8 % 4 % 3 % 2; CHECK_XPATH_NODESET(center, STR("ancestor-or-self::*")) % 8 % 4 % 3 % 2;
CHECK_XPATH_NODESET(center, STR("ancestor::*[3]")) % 2; CHECK_XPATH_NODESET(center, STR("ancestor::*[3]")) % 2;
@ -99,7 +99,7 @@ TEST_XML(xpath_xalan_axes_3, "<far-north><north><near-north><far-west/><west/><n
TEST_XML(xpath_xalan_axes_4, "<far-north><north><near-north><far-west/><west/><near-west/><center><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>") TEST_XML(xpath_xalan_axes_4, "<far-north><north><near-north><far-west/><west/><near-west/><center><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>")
{ {
xml_node north = doc.select_single_node(STR("//north")).node(); xml_node north = doc.select_node(STR("//north")).node();
CHECK_XPATH_STRING(north, STR("name(/descendant-or-self::north)"), STR("north")); CHECK_XPATH_STRING(north, STR("name(/descendant-or-self::north)"), STR("north"));
CHECK_XPATH_STRING(north, STR("name(/descendant::near-north)"), STR("near-north")); CHECK_XPATH_STRING(north, STR("name(/descendant::near-north)"), STR("near-north"));
@ -166,7 +166,7 @@ TEST_XML(xpath_xalan_axes_6, "<doc><T>Test for source tree depth</T><a><T>A</T><
TEST_XML(xpath_xalan_axes_7, "<far-north><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>") TEST_XML(xpath_xalan_axes_7, "<far-north><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south><south><far-south/></south></near-south></center><near-east/><east/><far-east/></near-north></north></far-north>")
{ {
xml_node center = doc.select_single_node(STR("//center")).node(); xml_node center = doc.select_node(STR("//center")).node();
CHECK_XPATH_NODESET(center, STR("attribute::*[2]")) % 10; CHECK_XPATH_NODESET(center, STR("attribute::*[2]")) % 10;
CHECK_XPATH_NODESET(center, STR("@*")) % 9 % 10 % 11; CHECK_XPATH_NODESET(center, STR("@*")) % 9 % 10 % 11;
@ -177,7 +177,7 @@ TEST_XML(xpath_xalan_axes_7, "<far-north><north><near-north><far-west/><west/><n
TEST_XML(xpath_xalan_axes_8, "<far-north><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-east/><near-south><south><far-south/></south></near-south><near-south-west/></center><near-east/><east/><far-east/></near-north></north></far-north>") TEST_XML(xpath_xalan_axes_8, "<far-north><north><near-north><far-west/><west/><near-west/><center center-attr-1='c1' center-attr-2='c2' center-attr-3='c3'><near-south-east/><near-south><south><far-south/></south></near-south><near-south-west/></center><near-east/><east/><far-east/></near-north></north></far-north>")
{ {
xml_node near_north = doc.select_single_node(STR("//near-north")).node(); xml_node near_north = doc.select_node(STR("//near-north")).node();
CHECK_XPATH_NODESET(near_north, STR("center//child::*")) % 12 % 13 % 14 % 15 % 16; CHECK_XPATH_NODESET(near_north, STR("center//child::*")) % 12 % 13 % 14 % 15 % 16;
CHECK_XPATH_NODESET(near_north, STR("center//descendant::*")) % 12 % 13 % 14 % 15 % 16; CHECK_XPATH_NODESET(near_north, STR("center//descendant::*")) % 12 % 13 % 14 % 15 % 16;
@ -188,7 +188,7 @@ TEST_XML(xpath_xalan_axes_8, "<far-north><north><near-north><far-west/><west/><n
TEST_XML(xpath_xalan_axes_9, "<doc><foo att1='c'><foo att1='b'><foo att1='a'><baz/></foo></foo></foo><bar/></doc>") TEST_XML(xpath_xalan_axes_9, "<doc><foo att1='c'><foo att1='b'><foo att1='a'><baz/></foo></foo></foo><bar/></doc>")
{ {
xml_node baz = doc.select_single_node(STR("//baz")).node(); xml_node baz = doc.select_node(STR("//baz")).node();
CHECK_XPATH_NODESET(baz, STR("ancestor-or-self::*[@att1][1]/@att1")) % 8; CHECK_XPATH_NODESET(baz, STR("ancestor-or-self::*[@att1][1]/@att1")) % 8;
CHECK_XPATH_NODESET(baz, STR("(ancestor-or-self::*)[@att1][1]/@att1")) % 4; CHECK_XPATH_NODESET(baz, STR("(ancestor-or-self::*)[@att1][1]/@att1")) % 4;
@ -243,7 +243,7 @@ TEST_XML_FLAGS(xpath_xalan_axes_12, "<far-north><north>north-text1<near-north><f
TEST_XML(xpath_xalan_axes_13, "<doc att1='e'><foo att1='d'><foo att1='c'><foo att1='b'><baz att1='a'/></foo></foo></foo></doc>") TEST_XML(xpath_xalan_axes_13, "<doc att1='e'><foo att1='d'><foo att1='c'><foo att1='b'><baz att1='a'/></foo></foo></foo></doc>")
{ {
xml_node d = doc.child(STR("doc")); xml_node d = doc.child(STR("doc"));
xml_node baz = doc.select_single_node(STR("//baz")).node(); xml_node baz = doc.select_node(STR("//baz")).node();
CHECK_XPATH_NUMBER(d, STR("count(descendant-or-self::*/@att1)"), 5); CHECK_XPATH_NUMBER(d, STR("count(descendant-or-self::*/@att1)"), 5);
CHECK_XPATH_NODESET(d, STR("descendant-or-self::*/@att1[last()]")) % 3 % 5 % 7 % 9 % 11; CHECK_XPATH_NODESET(d, STR("descendant-or-self::*/@att1[last()]")) % 3 % 5 % 7 % 9 % 11;