From 2094a4fd3da85c1972f215cb5977f6157590ff79 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Fri, 28 Feb 2014 06:01:16 +0000 Subject: docs: Regenerate HTML documentation git-svn-id: https://pugixml.googlecode.com/svn/trunk@993 99668b35-9821-0410-8761-19e4c4f06640 --- docs/manual/xpath.html | 362 ++++++++++++++++++++++++++----------------------- 1 file changed, 189 insertions(+), 173 deletions(-) (limited to 'docs/manual/xpath.html') diff --git a/docs/manual/xpath.html b/docs/manual/xpath.html index bb37f64..e2290e5 100644 --- a/docs/manual/xpath.html +++ b/docs/manual/xpath.html @@ -3,16 +3,16 @@ XPath - - - + + +
-pugixml 1.2 manual | +pugixml 1.4 manual | Overview | Installation | Document: @@ -28,15 +28,15 @@
-
-
XPath types
-
Selecting nodes via XPath expression
-
Using query objects
-
Using variables
-
Error handling
-
Conformance to W3C specification
+

If the task at hand is to select a subset of document nodes that match some @@ -48,7 +48,7 @@ for these cases. pugixml implements an almost complete subset of XPath 1.0. Because of differences in document object model and some performance implications, there are minor violations of the official specifications, which can be found - in Conformance to W3C specification. The rest of this section describes the interface for XPath + in Conformance to W3C specification. The rest of this section describes the interface for XPath functionality. Please note that if you wish to learn to use XPath language, you have to look for other tutorials or manuals; for example, you can read W3Schools XPath tutorial, @@ -58,11 +58,12 @@

-

- Each XPath expression can have one of the following types: boolean, number, - string or node set. Boolean type corresponds to bool +

+ Each + XPath expression can have one of the following types: boolean, number, string + or node set. Boolean type corresponds to bool type, number type corresponds to double type, string type corresponds to either std::string or std::wstring, depending on whether wide @@ -72,11 +73,11 @@ xpath_type_number, xpath_type_string or xpath_type_node_set, accordingly.

-

- Because an XPath node can be either a node or an attribute, there is a special - type, xpath_node, which is - a discriminated union of these types. A value of this type contains two node - handles, one of xml_node +

+ Because an XPath node can be either a node or an + attribute, there is a special type, xpath_node, + which is a discriminated union of these types. A value of this type contains + two node handles, one of xml_node type, and another one of xml_attribute type; at most one of them can be non-null. The accessors to get these handles are available: @@ -101,30 +102,33 @@ handle. For null nodes, parent returns null handle.

-

- Like node and attribute handles, XPath node handles can be implicitly cast - to boolean-like object to check if it is a null node, and also can be compared +

+ Like + node and attribute handles, XPath node handles can be implicitly cast to + boolean-like object to check if it is a null node, and also can be compared for equality with each other.

-

- You can also create XPath nodes with one of the three constructors: the default - constructor, the constructor that takes node argument, and the constructor - that takes attribute and node arguments (in which case the attribute must - belong to the attribute list of the node). The constructor from xml_node is implicit, so you can usually - pass xml_node to functions - that expect xpath_node. Apart - from that you usually don't need to create your own XPath node objects, since - they are returned to you via selection functions. -

-

- XPath expressions operate not on single nodes, but instead on node sets. - A node set is a collection of nodes, which can be optionally ordered in either - a forward document order or a reverse one. Document order is defined in XPath - specification; an XPath node is before another node in document order if - it appears before it in XML representation of the corresponding document. -

-

- Node sets are represented by xpath_node_set +

+ You can also create XPath nodes with one of + the three constructors: the default constructor, the constructor that takes + node argument, and the constructor that takes attribute and node arguments + (in which case the attribute must belong to the attribute list of the node). + The constructor from xml_node + is implicit, so you can usually pass xml_node + to functions that expect xpath_node. + Apart from that you usually don't need to create your own XPath node objects, + since they are returned to you via selection functions. +

+

+ XPath expressions operate not on single nodes, + but instead on node sets. A node set is a collection of nodes, which can + be optionally ordered in either a forward document order or a reverse one. + Document order is defined in XPath specification; an XPath node is before + another node in document order if it appears before it in XML representation + of the corresponding document. +

+

+ Node sets are represented by xpath_node_set object, which has an interface that resembles one of sequential random-access containers. It has an iterator type along with usual begin/past-the-end iterator accessors: @@ -133,8 +137,9 @@ const_iterator xpath_node_set::begin() const; const_iterator xpath_node_set::end() const; -

- And it also can be iterated via indices, just like std::vector: +

+ And it also can be iterated via indices, just + like std::vector:

const xpath_node& xpath_node_set::operator[](size_t index) const;
 size_t xpath_node_set::size() const;
@@ -147,9 +152,9 @@
         set size results in undefined behavior. You can use both iterator-based and
         index-based access for iteration, however the iterator-based one can be faster.
       

-

- The order of iteration depends on the order of nodes inside the set; the - order can be queried via the following function: +

+ The order of iteration depends on the order of + nodes inside the set; the order can be queried via the following function:

enum xpath_node_set::type_t {type_unsorted, type_sorted, type_sorted_reverse};
 type_t xpath_node_set::type() const;
@@ -173,9 +178,10 @@
         will return type_sorted or
         type_sorted_reverse.
       

-

- Often the actual iteration is not needed; instead, only the first element - in document order is required. For this, a special accessor is provided: +

+ Often the actual iteration is not needed; + instead, only the first element in document order is required. For this, + a special accessor is provided:

xpath_node xpath_node_set::first() const;
 
@@ -187,10 +193,11 @@ the complexity does - if the set is sorted, the complexity is constant, otherwise it is linear in the number of elements or worse.

-

- While in the majority of cases the node set is returned by XPath functions, - sometimes there is a need to manually construct a node set. For such cases, - a constructor is provided which takes an iterator range (const_iterator +

+ While in the majority of cases the node + set is returned by XPath functions, sometimes there is a need to manually + construct a node set. For such cases, a constructor is provided which takes + an iterator range (const_iterator is a typedef for const xpath_node*), and an optional type:

xpath_node_set::xpath_node_set(const_iterator begin, const_iterator end, type_t type = type_unsorted);
@@ -205,11 +212,12 @@
 
-

- If you want to select nodes that match some XPath expression, you can do - it with the following functions: +

+ If + you want to select nodes that match some XPath expression, you can do it + with the following functions:

xpath_node xml_node::select_single_node(const char_t* query, xpath_variable_set* variables = 0) const;
 xpath_node_set xml_node::select_nodes(const char_t* query, xpath_variable_set* variables = 0) const;
@@ -228,15 +236,15 @@
 

If exception handling is not disabled, both functions throw xpath_exception if the query can not be compiled or if it returns a value with type other - than node set; see Error handling for details. + than node set; see Error handling for details.

-

- While compiling expressions is fast, the compilation time can introduce a - significant overhead if the same expression is used many times on small subtrees. - If you're doing many similar queries, consider compiling them into query - objects (see Using query objects for further reference). Once you get a compiled - query object, you can pass it to select functions instead of an expression - string: +

+ While + compiling expressions is fast, the compilation time can introduce a significant + overhead if the same expression is used many times on small subtrees. If + you're doing many similar queries, consider compiling them into query objects + (see Using query objects for further reference). Once you get a compiled query + object, you can pass it to select functions instead of an expression string:

xpath_node xml_node::select_single_node(const xpath_query& query) const;
 xpath_node_set xml_node::select_nodes(const xpath_query& query) const;
@@ -249,36 +257,36 @@
         This is an example of selecting nodes using XPath expressions (samples/xpath_select.cpp):
       

-

pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
 
-std::cout << "Tools:";
+std::cout << "Tools:\n";
 
 for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it)
 {
     pugi::xpath_node node = *it;
-    std::cout << " " << node.node().attribute("Filename").value();
+    std::cout << node.node().attribute("Filename").value() << "\n";
 }
 
 pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]");
 
-std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n";
+if (build_tool)
+    std::cout << "Build tool: " << build_tool.node().attribute("Filename").value() << "\n";
 

-

- When you call select_nodes +

+ When you call select_nodes with an expression string as an argument, a query object is created behind the scenes. A query object represents a compiled XPath expression. Query objects can be needed in the following circumstances:

-
    +
    • You can precompile expressions to query objects to save compilation time if it becomes an issue; @@ -299,23 +307,25 @@ operator and store pointers to xpath_query in the container.

      -

      - You can create a query object with the constructor that takes XPath expression - as an argument: +

      + You can create a query object with the constructor + that takes XPath expression as an argument:

      explicit xpath_query::xpath_query(const char_t* query, xpath_variable_set* variables = 0);
       
      -

      - The expression is compiled and the compiled representation is stored in the - new query object. If compilation fails, xpath_exception - is thrown if exception handling is not disabled (see Error handling for - details). After the query is created, you can query the type of the evaluation - result using the following function: +

      + The expression is compiled and the + compiled representation is stored in the new query object. If compilation + fails, xpath_exception is thrown if + exception handling is not disabled (see Error handling for details). + After the query is created, you can query the type of the evaluation result + using the following function:

      xpath_value_type xpath_query::return_type() const;
       
      -

      - You can evaluate the query using one of the following functions: +

      + You + can evaluate the query using one of the following functions:

      bool xpath_query::evaluate_boolean(const xpath_node& n) const;
       double xpath_query::evaluate_number(const xpath_node& n) const;
      @@ -330,7 +340,7 @@
               of this, evaluate_boolean,
               evaluate_number and evaluate_string always return a result,
               but evaluate_node_set results
      -        in an error if the return type is not node set (see  Error handling).
      +        in an error if the return type is not node set (see Error handling).
             

      @@ -342,9 +352,9 @@ is equivalent to calling xpath_query("query").evaluate_node_set(node).

      -

      - Note that evaluate_string - function returns the STL string; as such, it's not available in PUGIXML_NO_STL +

      + Note that evaluate_string function returns the STL + string; as such, it's not available in PUGIXML_NO_STL mode and also usually allocates memory. There is another string evaluation function:

      @@ -357,7 +367,7 @@ is not 0, the resulting buffer is always zero-terminated. You can use this function as follows:

      -
        +
        • First call the function with buffer = 0 @@ -376,21 +386,20 @@ This is an example of using query objects (samples/xpath_query.cpp):

          -

          -
          // Select nodes via compiled query
          -pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
          +
          // Select nodes via compiled query
          +pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
           
           pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc);
           std::cout << "Remote tool: ";
           tools[2].node().print(std::cout);
           
          -// Evaluate numbers via compiled query
          -pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
          +// Evaluate numbers via compiled query
          +pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
           std::cout << query_timeouts.evaluate_number(doc) << std::endl;
           
          -// Evaluate strings via compiled query for different context nodes
          -pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
          +// Evaluate strings via compiled query for different context nodes
          +pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
           pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)");
           
           for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling())
          @@ -405,7 +414,7 @@
           

        XPath queries may contain references to variables; this is useful if you @@ -438,12 +447,13 @@ that the lifetime of the set exceeds that of query object.

-

- Variable sets correspond to xpath_variable_set - type, which is essentially a variable container. +

+ Variable sets correspond to xpath_variable_set type, which is essentially + a variable container.

-

- You can add new variables with the following function: +

+ You can add new variables with the + following function:

xpath_variable* xpath_variable_set::add(const char_t* name, xpath_value_type type);
 
@@ -460,8 +470,9 @@ 0 for numbers, false for booleans, empty string for strings and empty set for node sets.

-

- You can get the existing variables with the following functions: +

+ You can get the existing variables + with the following functions:

xpath_variable* xpath_variable_set::get(const char_t* name);
 const xpath_variable* xpath_variable_set::get(const char_t* name) const;
@@ -470,13 +481,14 @@
         The functions return the variable handle, or null pointer if the variable
         with the specified name is not found.
       

-

- Additionally, there are the helper functions for setting the variable value - by name; they try to add the variable with the corresponding type, if it - does not exist, and to set the value. If the variable with the same name - but with different type is already present, they return false; - they also return false on allocation - failure. Note that these functions do not perform any type conversions. +

+ Additionally, there are the helper + functions for setting the variable value by name; they try to add the variable + with the corresponding type, if it does not exist, and to set the value. + If the variable with the same name but with different type is already present, + they return false; they also + return false on allocation failure. + Note that these functions do not perform any type conversions.

bool xpath_variable_set::set(const char_t* name, bool value);
 bool xpath_variable_set::set(const char_t* name, double value);
@@ -487,14 +499,15 @@
         The variable values are copied to the internal variable storage, so you can
         modify or destroy them after the functions return.
       

-

- If setting variables by name is not efficient enough, or if you have to inspect - variable information or get variable values, you can use variable handles. - A variable corresponds to the xpath_variable - type, and a variable handle is simply a pointer to xpath_variable. +

+ If setting variables by name is not efficient + enough, or if you have to inspect variable information or get variable values, + you can use variable handles. A variable corresponds to the xpath_variable type, and a variable handle + is simply a pointer to xpath_variable.

-

- In order to get variable information, you can use one of the following functions: +

+ In + order to get variable information, you can use one of the following functions:

const char_t* xpath_variable::name() const;
 xpath_value_type xpath_variable::type() const;
@@ -503,8 +516,9 @@
         Note that each variable has a distinct type which is specified upon variable
         creation and can not be changed later.
       

-

- In order to get variable value, you should use one of the following functions, +

+ In + order to get variable value, you should use one of the following functions, depending on the variable type:

bool xpath_variable::get_boolean() const;
@@ -517,9 +531,9 @@
         are performed; if the type mismatch occurs, a dummy value is returned (false for booleans, NaN
         for numbers, empty string for strings and empty set for node sets).
       

-

- In order to set variable value, you should use one of the following functions, - depending on the variable type: +

+ In order to set variable value, you should + use one of the following functions, depending on the variable type:

bool xpath_variable::set(bool value);
 bool xpath_variable::set(double value);
@@ -536,10 +550,9 @@
         This is an example of using variables in XPath queries (samples/xpath_variables.cpp):
       

-

-
// Select nodes via compiled query
-pugi::xpath_variable_set vars;
+
// Select nodes via compiled query
+pugi::xpath_variable_set vars;
 vars.add("remote", pugi::xpath_type_boolean);
 
 pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote = string($remote)]", &vars);
@@ -556,8 +569,8 @@
 std::cout << "Local tool: ";
 tools_local[0].node().print(std::cout);
 
-// You can pass the context directly to select_nodes/select_single_node
-pugi::xpath_node_set tools_local_imm = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote = string($remote)]", &vars);
+// You can pass the context directly to select_nodes/select_single_node
+pugi::xpath_node_set tools_local_imm = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote = string($remote)]", &vars);
 
 std::cout << "Local tool imm: ";
 tools_local_imm[0].node().print(std::cout);
@@ -567,7 +580,7 @@
 
 

There are two different mechanisms for error handling in XPath implementation; @@ -575,21 +588,23 @@ is controlled with PUGIXML_NO_EXCEPTIONS define).

-

- By default, XPath functions throw xpath_exception - object in case of errors; additionally, in the event any memory allocation - fails, an std::bad_alloc exception is thrown. Also xpath_exception is thrown if the query - is evaluated to a node set, but the return type is not node set. If the query - constructor succeeds (i.e. no exception is thrown), the query object is valid. - Otherwise you can get the error details via one of the following functions: +

+ By default, XPath functions throw xpath_exception object in case of errors; + additionally, in the event any memory allocation fails, an std::bad_alloc + exception is thrown. Also xpath_exception + is thrown if the query is evaluated to a node set, but the return type is + not node set. If the query constructor succeeds (i.e. no exception is thrown), + the query object is valid. Otherwise you can get the error details via one + of the following functions:

virtual const char* xpath_exception::what() const throw();
 const xpath_parse_result& xpath_exception::result() const;
 
-

- If exceptions are disabled, then in the event of parsing failure the query - is initialized to invalid state; you can test if the query object is valid - by using it in a boolean expression: if +

+ If + exceptions are disabled, then in the event of parsing failure the query is + initialized to invalid state; you can test if the query object is valid by + using it in a boolean expression: if (query) { ... }. Additionally, you can get parsing result via the result() accessor: @@ -602,8 +617,9 @@ a query as a node set results in an empty node set if the return type is not node set.

-

- The information about parsing result is returned via xpath_parse_result +

+ The information about parsing result is + returned via xpath_parse_result object. It contains parsing status and the offset of last successfully parsed character from the beginning of the source stream:

@@ -616,39 +632,39 @@ const char* description() const; };
-

- Parsing result is represented as the error message; it is either a null pointer, - in case there is no error, or the error message in the form of ASCII zero-terminated - string. +

+ Parsing result is represented as + the error message; it is either a null pointer, in case there is no error, + or the error message in the form of ASCII zero-terminated string.

-

- description() - member function can be used to get the error message; it never returns the - null pointer, so you can safely use description() even if query parsing succeeded. Note that +

+ description() member function can be used to get the + error message; it never returns the null pointer, so you can safely use description() - returns a char string even in - PUGIXML_WCHAR_MODE; you'll - have to call as_wide to get the wchar_t string. + even if query parsing succeeded. Note that description() returns a char + string even in PUGIXML_WCHAR_MODE; + you'll have to call as_wide to get the wchar_t string.

-

- In addition to the error message, parsing result has an offset +

+ In addition to the error message, + parsing result has an offset member, which contains the offset of last successfully parsed character. This offset is in units of pugi::char_t (bytes for character mode, wide characters for wide character mode).

-

- Parsing result object can be implicitly converted to bool - like this: if (result) { ... } +

+ Parsing result object can be implicitly + converted to bool like this: + if (result) { ... } else { ... }.

This is an example of XPath error handling (samples/xpath_error.cpp):

-

-
// Exception is thrown for incorrect query syntax
-try
+
// Exception is thrown for incorrect query syntax
+try
 {
     doc.select_nodes("//nodes[#true()]");
 }
@@ -657,8 +673,8 @@
     std::cout << "Select failed: " << e.what() << std::endl;
 }
 
-// Exception is thrown for incorrect query semantics
-try
+// Exception is thrown for incorrect query semantics
+try
 {
     doc.select_nodes("(123)/next");
 }
@@ -667,8 +683,8 @@
     std::cout << "Select failed: " << e.what() << std::endl;
 }
 
-// Exception is thrown for query with incorrect return type
-try
+// Exception is thrown for query with incorrect return type
+try
 {
     doc.select_nodes("123");
 }
@@ -682,18 +698,18 @@
 
 

Because of the differences in document object models, performance considerations and implementation complexity, pugixml does not provide a fully conformant XPath 1.0 implementation. This is the current list of incompatibilities:

-
    +
    • Consecutive text nodes sharing the same parent are not merged, i.e. in <node>text1 - <![CDATA[data]]> text2</node> node should have one text node children, + <![CDATA[data]]> text2</node> node should have one text node child, but instead has three.
    • @@ -721,7 +737,7 @@
    - @@ -729,7 +745,7 @@
    -pugixml 1.2 manual | +pugixml 1.4 manual | Overview | Installation | Document: -- cgit v1.2.3