From 186e491d1e7f7bddc04d5169084b224a648aa457 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 31 Oct 2010 07:45:27 +0000 Subject: docs: Regenerated HTML documentation git-svn-id: http://pugixml.googlecode.com/svn/trunk@790 99668b35-9821-0410-8761-19e4c4f06640 --- docs/manual/xpath.html | 368 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 308 insertions(+), 60 deletions(-) (limited to 'docs/manual/xpath.html') diff --git a/docs/manual/xpath.html b/docs/manual/xpath.html index 731a969..5a97a79 100644 --- a/docs/manual/xpath.html +++ b/docs/manual/xpath.html @@ -4,14 +4,15 @@ XPath - - + + -
pugixml 0.9 manual | + +pugixml 1.0 manual | Overview | Installation | Document: @@ -33,6 +34,7 @@
XPath types
Selecting nodes via XPath expression
Using query objects
+
Using variables
Error handling
Conformance to W3C specification
@@ -54,18 +56,6 @@ at tizag.com, and the XPath 1.0 specification.

-
- - - - - -
[Note]Note

- As of version 0.9, you need both STL and exception support to use XPath; - XPath is disabled if either PUGIXML_NO_STL - or PUGIXML_NO_EXCEPTIONS - is defined. -

XPath types @@ -76,7 +66,7 @@ type, number type corresponds to double type, string type corresponds to either std::string or std::wstring, depending on whether wide - character interface is enabled, and node set corresponds to xpath_node_set type. There is an enumeration, + character interface is enabled, and node set corresponds to xpath_node_set type. There is an enumeration, xpath_value_type, which can take the values xpath_type_boolean, xpath_type_number, xpath_type_string or xpath_type_node_set, @@ -117,12 +107,14 @@ for equality with each other.

- You can also create XPath nodes with one of tree constructors: the default + 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). However, usually you don't need - to create your own XPath node objects, since they are returned to you via - selection functions. + 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. @@ -153,7 +145,7 @@ the iterators are random-access, all of the above operations are constant time, and accessing the element at index that is greater or equal than the set size results in undefined behavior. You can use both iterator-based and - index-based access for iteration, however the iterator-based can be faster. + 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 @@ -195,6 +187,21 @@ 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 + 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);
+
+

+ The constructor copies the specified range and sets the specified type. The + objects in the range are not checked in any way; you'll have to ensure that + the range contains no duplicates, and that the objects are sorted according + to the type parameter. Otherwise + XPath operations with this set may produce unexpected results. +

@@ -204,8 +211,8 @@ 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) const;
-xpath_node_set xml_node::select_nodes(const char_t* query) const;
+
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;
 

select_nodes function compiles @@ -219,7 +226,7 @@ returns null XPath node.

- Both functions throw xpath_exception + 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.

@@ -235,7 +242,7 @@ xpath_node_set xml_node::select_nodes(const xpath_query& query) const;

- Both functions throw xpath_exception + If exception handling is not disabled, both functions throw xpath_exception if the query returns a value with type other than node set.

@@ -268,8 +275,8 @@

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

  • @@ -296,33 +303,34 @@ 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);
    +
    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 (see Error handling for details). After the query is created, - you can query the type of the evaluation result using the following function: + 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:

    -
    bool xpath_query::evaluate_boolean(const xml_node& n) const;
    -double xpath_query::evaluate_number(const xml_node& n) const;
    -string_t xpath_query::evaluate_string(const xml_node& n) const;
    -xpath_node_set xpath_query::evaluate_node_set(const xml_node& n) const;
    +
    bool xpath_query::evaluate_boolean(const xpath_node& n) const;
    +double xpath_query::evaluate_number(const xpath_node& n) const;
    +string_t xpath_query::evaluate_string(const xpath_node& n) const;
    +xpath_node_set xpath_query::evaluate_node_set(const xpath_node& n) const;
     

    All functions take the context node as an argument, compute the expression - and return the result, converted to the requested type. By XPath specification, - value of any type can be converted to boolean, number or string value, but - no type other than node set can be converted to node set. Because of this, - evaluate_boolean, evaluate_number and evaluate_string - always return a result, but evaluate_node_set - throws an xpath_exception - if the return type is not node set. + and return the result, converted to the requested type. According to XPath + specification, value of any type can be converted to boolean, number or string + value, but no type other than node set can be converted to node set. Because + 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).

    @@ -334,6 +342,36 @@ 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 + mode and also usually allocates memory. There is another string evaluation + function: +

    +
    size_t xpath_query::evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const;
    +
    +

    + This function evaluates the string, and then writes the result to buffer (but at most capacity + characters); then it returns the full size of the result in characters, including + the terminating zero. If capacity + is not 0, the resulting buffer is always zero-terminated. You can use this + function as follows: +

    +
      +
    • + First call the function with buffer + = 0 + and capacity = + 0; then allocate the returned amount + of characters, and call the function again, passing the allocated storage + and the amount of characters; +
    • +
    • + First call the function with small buffer and buffer capacity; then, + if the result is larger than the capacity, the output has been trimmed, + so allocate a larger buffer and call the function again. +
    • +

    This is an example of using query objects (samples/xpath_query.cpp):

    @@ -367,22 +405,237 @@
+

+ XPath queries may contain references to variables; this is useful if you + want to use queries that depend on some dynamic parameter without manually + preparing the complete query string, or if you want to reuse the same query + object for similar queries. +

+

+ Variable references have the form $name; in order to use them, you have to provide + a variable set, which includes all variables present in the query with correct + types. This set is passed to xpath_query + constructor or to select_nodes/select_single_node functions: +

+
explicit xpath_query::xpath_query(const char_t* query, xpath_variable_set* variables = 0);
+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;
+
+

+ If you're using query objects, you can change the variable values before + evaluate/select + calls to change the query behavior. +

+
+ + + + + +
[Note]Note

+ The variable set pointer is stored in the query object; you have to ensure + 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. +

+

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

+
xpath_variable* xpath_variable_set::add(const char_t* name, xpath_value_type type);
+
+

+ The function tries to add a new variable with the specified name and type; + if the variable with such name does not exist in the set, the function adds + a new variable and returns the variable handle; if there is already a variable + with the specified name, the function returns the variable handle if variable + has the specified type. Otherwise the function returns null pointer; it also + returns null pointer on allocation failure. +

+

+ New variables are assigned the default value which depends on the type: + 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: +

+
xpath_variable* xpath_variable_set::get(const char_t* name);
+const xpath_variable* xpath_variable_set::get(const char_t* name) const;
+
+

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

+
bool xpath_variable_set::set(const char_t* name, bool value);
+bool xpath_variable_set::set(const char_t* name, double value);
+bool xpath_variable_set::set(const char_t* name, const char_t* value);
+bool xpath_variable_set::set(const char_t* name, const xpath_node_set& value);
+
+

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

+

+ 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;
+
+

+ 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, + depending on the variable type: +

+
bool xpath_variable::get_boolean() const;
+double xpath_variable::get_number() const;
+const char_t* xpath_variable::get_string() const;
+const xpath_node_set& xpath_variable::get_node_set() const;
+
+

+ These functions return the value of the variable. Note that no type conversions + 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: +

+
bool xpath_variable::set(bool value);
+bool xpath_variable::set(double value);
+bool xpath_variable::set(const char_t* value);
+bool xpath_variable::set(const xpath_node_set& value);
+
+

+ These function modify the variable value. Note that no type conversions are + performed; if the type mismatch occurs, the functions return false; they also return false + on allocation failure. The variable values are copied to the internal variable + storage, so you can modify or destroy them after the functions return. +

+

+ This is an example of using variables in XPath queries (samples/xpath_variables.cpp): +

+

+ +

+
// 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);
+
+vars.set("remote", true);
+pugi::xpath_node_set tools_remote = query_remote_tools.evaluate_node_set(doc);
+
+vars.set("remote", false);
+pugi::xpath_node_set tools_local = query_remote_tools.evaluate_node_set(doc);
+
+std::cout << "Remote tool: ";
+tools_remote[2].node().print(std::cout);
+
+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);
+
+std::cout << "Local tool imm: ";
+tools_local_imm[0].node().print(std::cout);
+
+

+

+
+
+ -

- As of version 0.9, all XPath errors result in thrown exceptions. The errors - can arise during expression compilation or node set evaluation. In both cases, - an xpath_exception object - is thrown. This is an exception object that implements std::exception - interface, and thus has a single function what(): +

+ There are two different mechanisms for error handling in XPath implementation; + the mechanism used depends on whether exception support is disabled (this + 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:

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 + (query) { ... + }. Additionally, you can get parsing + result via the result() accessor: +

+
const xpath_parse_result& xpath_query::result() const;
 

- This function returns the error message. Currently it is impossible to get - the exact place where query compilation failed. This functionality, along - with optional error handling without exceptions, will be available in version - 1.0. + Without exceptions, evaluating invalid query results in false, + empty string, NaN or an empty node set, depending on the type; evaluating + 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 + object. It contains parsing status and the offset of last successfully parsed + character from the beginning of the source stream: +

+
struct xpath_parse_result
+{
+    const char* error;
+    ptrdiff_t offset;
+
+    operator bool() const;
+    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. +

+

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

+

+ 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) { ... } + else { ... }.

This is an example of XPath error handling (samples/xpath_error.cpp): @@ -440,7 +693,7 @@ but instead has three.

  • - Since document can't have a document type declaration, id() + Since the document type declaration is not used for parsing, id() function always returns an empty node set.
  • @@ -459,13 +712,7 @@ value, depending on the library configuration; this means that some string functions are not fully Unicode-aware. This affects substring(), string-length() and translate() functions.
  • -
  • - Variable references are not supported. -
  • -

    - Some of these incompatibilities will be fixed in version 1.0. -

    @@ -477,7 +724,8 @@

    -
    pugixml 0.9 manual | + +pugixml 1.0 manual | Overview | Installation | Document: -- cgit v1.2.3