CopperSpice API
1.9.2
|
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML. More...
Public Types | |
enum | QueryLanguage |
Public Methods | |
QXmlQuery () | |
QXmlQuery (const QXmlNamePool &np) | |
QXmlQuery (const QXmlQuery &other) | |
QXmlQuery (QueryLanguage queryLanguage, const QXmlNamePool &np=QXmlNamePool ()) | |
~QXmlQuery () | |
void | bindVariable (const QString &localName, const QXmlItem &value) |
void | bindVariable (const QString &localName, const QXmlQuery &query) |
void | bindVariable (const QString &localName, QIODevice *device) |
void | bindVariable (const QXmlName &name, const QXmlItem &value) |
void | bindVariable (const QXmlName &name, const QXmlQuery &query) |
void | bindVariable (const QXmlName &name, QIODevice *device) |
bool | evaluateTo (QAbstractXmlReceiver *callback) const |
bool | evaluateTo (QIODevice *target) const |
bool | evaluateTo (QString *output) const |
bool | evaluateTo (QStringList *target) const |
void | evaluateTo (QXmlResultItems *result) const |
QXmlName | initialTemplateName () const |
bool | isValid () const |
QAbstractMessageHandler * | messageHandler () const |
QXmlNamePool | namePool () const |
QNetworkAccessManager * | networkAccessManager () const |
QXmlQuery & | operator= (const QXmlQuery &other) |
QueryLanguage | queryLanguage () const |
bool | setFocus (const QString &focusText) |
bool | setFocus (const QUrl &documentURI) |
void | setFocus (const QXmlItem &item) |
bool | setFocus (QIODevice *document) |
void | setInitialTemplateName (const QString &name) |
void | setInitialTemplateName (const QXmlName &name) |
void | setMessageHandler (QAbstractMessageHandler *msgHandler) |
void | setNetworkAccessManager (QNetworkAccessManager *newManager) |
void | setQuery (const QString &sourceCode, const QUrl &documentURI=QUrl ()) |
void | setQuery (const QUrl &queryURI, const QUrl &baseURI=QUrl ()) |
void | setQuery (QIODevice *sourceCode, const QUrl &documentURI=QUrl ()) |
void | setUriResolver (const QAbstractUriResolver *resolver) |
const QAbstractUriResolver * | uriResolver () const |
Friends | |
class | QXmlName |
class | QXmlSerializer |
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML. The QXmlQuery class compiles and executes queries written in the XQuery language. QXmlQuery is typically used to query XML data, but it can also query non-XML data that has been modeled to look like XML.
Using QXmlQuery to query XML data, as in the snippet below, is simple because it can use the built-in XML data model as its delegate to the underlying query engine for traversing the data. The built-in data model is specified in XQuery 1.0 and XPath 2.0 Data Model.
The example uses QXmlQuery to match the first paragraph of an XML document and then output the result to a device as XML.
Using QXmlQuery to query non-XML data requires writing a subclass of QAbstractXmlNodeModel to use as a replacement for the built in XML data model. The custom data model will be able to traverse the non-XML data as required by the QAbstractXmlNodeModel interface. An instance of this custom data model then becomes the delegate used by the query engine to traverse the non-XML data. For an example of how to use QXmlQuery to query non-XML data, refer the documentation for QAbstractXmlNodeModel.
To run a query set up with QXmlQuery, call one of the evaluation functions.
The XPath language is a subset of the XQuery language, so running an XPath expression is the same as running an XQuery query. Pass the XPath expression to QXmlQuery using setQuery().
Running an XSLT stylesheet is like running an XQuery, except that when you construct your QXmlQuery, you must pass QXmlQuery::XSLT20 to tell QXmlQuery to interpret whatever it gets from setQuery() as an XSLT stylesheet instead of as an XQuery. You must also set the input document by calling setFocus().
The method setFocus() must be called before setQuery() when using XSLT. Another way to run an XSLT stylesheet is to use the xmlpatterns
command line utility.
Stylesheet parameters are bound using bindVariable().
When a query is run on XML data, as in the snippet above, the doc() function returns the node in the built-in data model where the query evaluation will begin. But when a query is run on a custom node model containing non-XML data, one of the bindVariable() functions must be called to bind a variable name to a starting node in the custom model. A $variable reference is used in the XQuery text to access the starting node in the custom model. It is not necessary to declare the variable name external in the query. Refer to the example in the documentation for QAbstractXmlNodeModel.
QXmlQuery is conditionally thread safe but not thread-safe. It is safe to use the QXmlQuery copy constructor to create a copy of a query and run the same query multiple times. QXmlQuery will reuse resources such as opened files and compiled queries to the extent possible. It is not safe to use the same instance of QXmlQuery in multiple threads.
Errors can occur during query evaluation. Examples include type errors and file loading errors. When an error occurs the following will happen.
true
, or evaluateTo() will return false
When a query runs, it parses documents, allocating internal data structures to hold them, and it may load other resources over the network. It reuses these allocated resources when possible, to avoid having to reload and reparse them.
When setQuery() is called, the query text is compiled into an internal data structure and optimized. The optimized form can then be reused for multiple evaluations of the query. Since the compile-and-optimize process can be expensive, repeating it for the same query should be avoided by using a separate instance of QXmlQuery for each query text.
Once a document has been parsed, its internal representation is maintained in the QXmlQuery instance and shared among multiple QXmlQuery instances.
An instance of QCoreApplication must exist before QXmlQuery can be used.
When QXmlQuery accesses resources (e.g., calling fn:doc()
to load a file, or accessing a device via a bound variable), the event loop is used, which means events will be processed. To avoid processing events when QXmlQuery accesses resources, create your QXmlQuery instance in a separate thread.
Specifies whether you want QXmlQuery to interpret the input to setQuery() as an XQuery or as an XSLT stylesheet.
Constant | Value | Description |
---|---|---|
QXmlQuery::XQuery10 | 1 | XQuery 1.0. |
QXmlQuery::XSLT20 | 2 | XSLT 2.0 The selector, the restricted XPath pattern found in W3C XML Schema 1.1 for uniqueness constraints. Apart from restricting the syntax, the type check stage for the expression assumes a sequence of nodes to be the focus. The field, the restricted XPath pattern found in W3C XML Schema 1.1 for uniqueness constraints. Apart from restricting the syntax, the type check stage for the expression assumes a sequence of nodes to be the focus. Signifies XPath 2.0. Has no effect in the public API, it's used internally. As With XmlSchema11IdentityConstraintSelector and XmlSchema11IdentityConstraintField, the type check stage for the expression assumes a sequence of nodes to be the focus. |
QXmlQuery::QXmlQuery | ( | ) |
Constructs an invalid, empty query that can not be used until setQuery() is called.
QXmlQuery::QXmlQuery | ( | const QXmlQuery & | other | ) |
Copy constructs a new QXmlQuery from other. The new object will share resources with the existing query to the extent possible.
QXmlQuery::QXmlQuery | ( | const QXmlNamePool & | np | ) |
Constructs a query which uses np as its name pool. The query can not be evaluated until setQuery() has been called.
QXmlQuery::QXmlQuery | ( | QueryLanguage | queryLanguage, |
const QXmlNamePool & | np = QXmlNamePool() |
||
) |
Constructs a query that will be used to run XQueries or XSL-T stylesheets, depending on the value of queryLanguage. It will use np as its name pool.
QXmlQuery::~QXmlQuery | ( | ) |
Destroys this QXmlQuery.
This method constructs a QXmlName from localName using the query's namespace. It is equivalent to the following code.
void QXmlQuery::bindVariable | ( | const QString & | localName, |
const QXmlQuery & | query | ||
) |
This method has the same behavior and effect as the overload which uses a QXmlName for the localName.
If localName is a valid NCName this method is equivalent to the following code. A QXmlName is constructed from localName and is passed to the appropriate overload along with device.
Binds the variable name to the value so $name can be used from within the query to refer to the value. The name must not be null. If name has already been bound by a previous bindVariable() call, the previous binding will be overridden. If value is null and name already has a binding, the effect is to remove the existing binding for name.
To bind a value of type QString or QUrl wrap the value in a QVariant so the QXmlItem QVariant constructor is called.
All strings processed by the query must be valid XQuery strings, which means they must contain only XML 1.0 characters. However, this requirement is not checked. If the query processes an invalid string, the behavior is undefined.
void QXmlQuery::bindVariable | ( | const QXmlName & | name, |
const QXmlQuery & | query | ||
) |
Binds the result of the given query to a variable of the given name. Evaluation of the query will begin when this method is called. If the query is invalid the behavior is undefined. The query will be copied.
Binds the variable name to the given device so that $name can be used from within the query. The given name must not be null. If name has already been bound then the previous binding will be overridden. The URI name evaluates to is arbitrary and may change.
If the type of the variable binding changes then isValid() will return false and recompilation of the query is required. To recompile the query call setQuery(). This is why bindVariable() should be called before setQuery().
The device is visible in the query using a URI of type xs:anyURI, which can be read by calling fn:doc(). The caller must ensure the device has been opened with at least QIODevice::ReadOnly prior to this binding. Otherwise, behavior is undefined.
The following example shows how to pass an XML document in memory to fn:doc.
If the query will access an XML document contained in a QString then use a QBuffer as shown in the following example. Suppose myQString contains <document>content</document>
bool QXmlQuery::evaluateTo | ( | QAbstractXmlReceiver * | callback | ) | const |
Evaluates this query and sends the result as a sequence of callbacks to the receiver callback. QXmlQuery does not take ownership of callback. If an error occurs during the evaluation, error messages are sent to messageHandler() and false is returned. If this query is invalid, false is returned and the behavior is undefined. If callback is null the behavior is undefined.
bool QXmlQuery::evaluateTo | ( | QIODevice * | target | ) | const |
Evaluates the query or stylesheet, and writes the output to target. QXmlSerializer is used to write the output to target. If an error occurs during the evaluation, error messages are sent to messageHandler() and false
is returned. If target is null, or is not opened in at least QIODevice::WriteOnly mode, the behavior is undefined. QXmlQuery does not take ownership of target.
bool QXmlQuery::evaluateTo | ( | QString * | output | ) | const |
Evaluates the query and serializes the output as XML to output. If an error occurs during the evaluation error messages are sent to messageHandler(). The content of output is undefined and false is returned, otherwise true is returned. If output is null the behavior is undefined. QXmlQuery does not take ownership of output.
bool QXmlQuery::evaluateTo | ( | QStringList * | target | ) | const |
Attempts to evaluate the query and returns the results in the target string list. If the query is valid and the evaluation succeeds, true is returned. Otherwise, false is returned and the contents of target are undefined. The query must evaluate to a sequence of xs:string
values. If the query does not evaluate to a sequence of strings, the values can often be converted by adding a call to string()
at the end of the XQuery. If target is null the behavior is undefined.
void QXmlQuery::evaluateTo | ( | QXmlResultItems * | result | ) | const |
Starts the evaluation and makes it available in result. If result is null, the behavior is undefined. The evaluation takes place incrementally (lazy evaluation), as the caller uses QXmlResultItems::next() to get the next result.
QXmlName QXmlQuery::initialTemplateName | ( | ) | const |
Returns the name of the XSL-T stylesheet template that the processor will call first when running an XSL-T stylesheet. This function only applies when using QXmlQuery to process XSL-T stylesheets. By default, no initial template is set. In this case a default constructed QXmlName is returned.
bool QXmlQuery::isValid | ( | ) | const |
Returns true if this query is valid. Examples of invalid queries are ones that contain syntax errors or that have not had setQuery() called for them yet.
QAbstractMessageHandler * QXmlQuery::messageHandler | ( | ) | const |
Returns the message handler that handles compile and runtime messages for this QXmlQuery.
QXmlNamePool QXmlQuery::namePool | ( | ) | const |
Returns the name pool used by this QXmlQuery for constructing names. There is no setter for the name pool, because mixing name pools causes errors due to name confusion.
QNetworkAccessManager * QXmlQuery::networkAccessManager | ( | ) | const |
Returns the network manager or a nullptr if it has not been set.
QXmlQuery & QXmlQuery::operator= | ( | const QXmlQuery & | other | ) |
Copy assigns from other and returns a reference to this object.
QueryLanguage QXmlQuery::queryLanguage | ( | ) | const |
Returns a value indicating what this QXmlQuery is being used for. The default is QXmlQuery::XQuery10, which means the QXmlQuery is being used for running XQuery and XPath queries. QXmlQuery::XSLT20 can also be returned, which indicates the QXmlQuery is for running XSL-T spreadsheets.
bool QXmlQuery::setFocus | ( | const QString & | focusText | ) |
Sets the focus to the XML document contained in focusText. If document can not be loaded, false is returned.
bool QXmlQuery::setFocus | ( | const QUrl & | documentURI | ) |
Sets the focus to be the document located at documentURI and returns true. If documentURI can not be loaded, false is returned. It is undefined at what time the document may be loaded. When loading the document, the message handler and URI resolver set on this QXmlQuery are used. If documentURI is empty or is not a valid URI, the behavior of this function is undefined.
void QXmlQuery::setFocus | ( | const QXmlItem & | item | ) |
Sets the focus to item. The focus is the set of items that the context item expression and path expressions navigate from. For example, in the expression p/span, the element that p evaluates to is the focus for the following expression, span. The focus can be accessed using the context item expression, i.e., dot (".").
By default, the focus is not set and is undefined. It will therefore result in a dynamic error, XPDY0002
, if the focus is attempted to be accessed. The focus must be set before the query is set with setQuery(). There is no behavior defined for setting an item which is null.
bool QXmlQuery::setFocus | ( | QIODevice * | document | ) |
Sets the focus to be the document read from the QIODevice and returns true. If document can not be loaded, false is returned.
QXmlQuery does not take ownership of document. The user guarantees that a document is available from the document device and that the document is not empty. The device must be opened in at least read-only mode. document must stay in scope as long as the current query is active.
void QXmlQuery::setInitialTemplateName | ( | const QString & | name | ) |
Sets the name of the initial template to name, which must be a valid local name. The initial template is the one the processor calls first, instead of attempting to match a template to the context node (if any). If an initial template is not set, the standard order of template invocation will be used.
This function only applies when using QXmlQuery to process XSL-T stylesheets. The name becomes part of the compiled stylesheet. Therefore, this function must be called before calling setQuery().
If localName is not a valid local name, the effect is undefined. If the stylesheet has no template named localName, the processor will use the standard order of template invocation.
void QXmlQuery::setInitialTemplateName | ( | const QXmlName & | name | ) |
Sets the name of the initial template. The initial template is the one the processor calls first, instead of attempting to match a template to the context node (if any). If an initial template is not set, the standard order of template invocation will be used.
This method only applies when using QXmlQuery to process XSL-T stylesheets. The name becomes part of the compiled stylesheet. Therefore, this function must be called before calling setQuery().
If the stylesheet has no template named name, the processor will use the standard order of template invocation.
void QXmlQuery::setMessageHandler | ( | QAbstractMessageHandler * | msgHandler | ) |
Changes the message handler for this QXmlQuery to msgHandler. The query sends all compile and runtime messages to this message handler. QXmlQuery does not take ownership of aMessageHandler.
Normally the default message handler is sufficient since it writes compile and runtime messages to stderr. The default message handler includes color codes if stderr can render colors. Changing the message handler after the query has been compiled has no effect. The query uses the same message handler at runtime that it uses at compile time.
When QXmlQuery calls QAbstractMessageHandler::message() the arguments are as follows:
Arguments passed to message() | Description |
---|---|
QtMsgType type | Only QtWarningMsg and QtFatalMsg are used. The former identifies a compile or runtime warning, while the latter identifies a dynamic or static error. |
const QString & description | An XHTML document which is the actual message. It is translated into the current language. |
const QUrl & identifier | Identifies the error with a URI, where the fragment is the error code, and the rest of the URI is the error namespace. |
const QSourceLocation & sourceLocation | Identifies where the error occurred. |
void QXmlQuery::setNetworkAccessManager | ( | QNetworkAccessManager * | newManager | ) |
Sets the network manager to newManager. QXmlQuery does not take ownership of newManager.
The behavior and requirements of this function are the same as for setQuery(QIODevice*, const QUrl&), after the XQuery has been read from the IO device into a string. Because sourceCode is already a Unicode string, detection of its encoding is unnecessary.
Sets this QXmlQuery to the XQuery read from the queryURI. Use isValid() after calling this method. If an error occurred reading queryURI, e.g., the query does not exist, can not be read, or is invalid, isValid() will return false.
The supported URI schemes are the same as those in the XQuery function fn:doc
, except that queryURI can be the object of a variable binding.
baseURI is the Base URI of the static context, as defined in the XQuery language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. If baseURI is empty, queryURI is used. Otherwise, baseURI is used, and it is resolved against the application file path if it is relative.
If queryURI is empty or invalid, or if baseURI is invalid, the behavior of this function is undefined.
Sets this QXmlQuery to an XQuery read from the sourceCode device. The device must have been opened with at least QIODevice::ReadOnly.
The documentURI represents the query obtained from the sourceCode device. It is the base URI of the static context, as defined in the XQuery language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. The documentURI can be empty. If it is empty, the application file path is used. If it is not empty, it may be either relative or absolute. If it is relative then it is resolved based on the application file path before it is used. If documentURI is neither a valid URI nor empty, the result is undefined.
If sourceCode is null, not readable, or if documentURI is not a valid URI, the behavior is undefined.
If the query contains a static error (e.g. syntax error), an error message is sent to the messageHandler(), and isValid() will return false. Variables must be bound before setQuery() is called.
The encoding of the XQuery in sourceCode is detected internally using the rules for setting and detecting encoding of XQuery files, which are explained in the XQuery language.
void QXmlQuery::setUriResolver | ( | const QAbstractUriResolver * | resolver | ) |
Sets the URI resolver to resolver. QXmlQuery does not take ownership of resolver.
const QAbstractUriResolver * QXmlQuery::uriResolver | ( | ) | const |
Returns the query's URI resolver. If no URI resolver has been set, CsXmlPatterns will use the URIs in queries as they are. The URI resolver provides a level of abstraction, or polymorphic URIs. A resolver can rewrite logical URIs to physica l ones, or it can translate obsolete or invalid URIs to valid ones. CsXmlPatterns calls the URI resolver for all URIs it encounters, except for namespaces. Specifically, all builtin functions that deal with URIs (fn:doc()
, and fn:doc-available()
).
In the case of fn:doc()
, the absolute URI is the base URI in the static context (which most likely is the location of the query). Rather than use the URI the user specified, the return value of QAbstractUriResolver::resolve() will be used.
When CsXmlPatterns calls QAbstractUriResolver::resolve() the absolute URI is the URI mandated by the XQuery language, and the relative URI is the URI specified by the user.