Qt 4.8
Public Types | Public Functions | Properties | Friends | Related Functions | List of all members
QXmlQuery Class Reference

The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML. More...

#include <qxmlquery.h>

Public Types

enum  QueryLanguage {
  XQuery10 = 1, XSLT20 = 2, XmlSchema11IdentityConstraintSelector = 1024, XmlSchema11IdentityConstraintField = 2048,
  XPath20 = 4096
}
 Specifies whether you want QXmlQuery to interpret the input to setQuery() as an XQuery or as an XSLT stylesheet. More...
 

Public Functions

void bindVariable (const QXmlName &name, const QXmlItem &value)
 Binds the variable name to the value so that $name can be used from within the query to refer to the value. More...
 
void bindVariable (const QString &localName, const QXmlItem &value)
 This function constructs a QXmlName from localName using the query's QXmlNamePool {namespace}. More...
 
void bindVariable (const QXmlName &name, QIODevice *)
 Binds the variable name to the device so that $name can be used from within the query to refer to the device. More...
 
void bindVariable (const QString &localName, QIODevice *)
 If localName is a valid QXmlName::isNCName() {NCName}, this function is equivalent to the following snippet. More...
 
void bindVariable (const QXmlName &name, const QXmlQuery &query)
 Binds the result of the query query, to a variable by name name. More...
 
void bindVariable (const QString &localName, const QXmlQuery &query)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Has the same behavior and effects as the function being overloaded, but takes the variable name localName as a QString. More...
 
void evaluateTo (QXmlResultItems *result) const
 Starts the evaluation and makes it available in result. More...
 
bool evaluateTo (QAbstractXmlReceiver *callback) const
 Evaluates this query and sends the result as a sequence of callbacks to the QAbstractXmlReceiver {receiver} callback. More...
 
bool evaluateTo (QStringList *target) const
 Attempts to evaluate the query and returns the results in the target QStringList {string list}. More...
 
bool evaluateTo (QIODevice *target) const
 Evaluates the query or stylesheet, and writes the output to target. More...
 
bool evaluateTo (QString *output) const
 Evaluates the query, and serializes the output as XML to output. More...
 
QXmlName initialTemplateName () const
 Returns the name of the XSL-T stylesheet template that the processor will call first when running an XSL-T stylesheet. More...
 
bool isValid () const
 Returns true if this query is valid. More...
 
QAbstractMessageHandlermessageHandler () const
 Returns the message handler that handles compile and runtime messages for this QXmlQuery. More...
 
QXmlNamePool namePool () const
 Returns the name pool used by this QXmlQuery for constructing QXmlName {names}. More...
 
QNetworkAccessManagernetworkAccessManager () const
 Returns the network manager, or 0 if it has not been set. More...
 
QXmlQueryoperator= (const QXmlQuery &other)
 Assigns other to this QXmlQuery instance. More...
 
QueryLanguage queryLanguage () const
 Returns a value indicating what this QXmlQuery is being used for. More...
 
 QXmlQuery ()
 Constructs an invalid, empty query that cannot be used until setQuery() is called. More...
 
 QXmlQuery (const QXmlQuery &other)
 Constructs a QXmlQuery that is a copy of other. More...
 
 QXmlQuery (const QXmlNamePool &np)
 Constructs a query that will use np as its name pool. More...
 
 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. More...
 
void setFocus (const QXmlItem &item)
 Sets the focus to item. More...
 
bool setFocus (const QUrl &documentURI)
 Sets the focus to be the document located at documentURI and returns true. More...
 
bool setFocus (QIODevice *document)
 Sets the focus to be the document read from the QIODevice and returns true. More...
 
bool setFocus (const QString &focus)
 This function behaves identically to calling the setFocus() overload with a QIODevice whose content is focus encoded as UTF-8. More...
 
void setInitialTemplateName (const QXmlName &name)
 Sets the name of the initial template. More...
 
void setInitialTemplateName (const QString &name)
 Sets the name of the initial template to localName, which must be a valid local name. More...
 
void setMessageHandler (QAbstractMessageHandler *messageHandler)
 Changes the QAbstractMessageHandler {message handler} for this QXmlQuery to aMessageHandler. More...
 
void setNetworkAccessManager (QNetworkAccessManager *newManager)
 Sets the network manager to newManager. More...
 
void setQuery (const QString &sourceCode, const QUrl &documentURI=QUrl())
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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. More...
 
void setQuery (QIODevice *sourceCode, const QUrl &documentURI=QUrl())
 Sets this QXmlQuery to an XQuery read from the sourceCode device. More...
 
void setQuery (const QUrl &queryURI, const QUrl &baseURI=QUrl())
 Sets this QXmlQuery to the XQuery read from the queryURI. More...
 
void setUriResolver (const QAbstractUriResolver *resolver)
 Sets the URI resolver to resolver. More...
 
const QAbstractUriResolveruriResolver () const
 Returns the query's URI resolver. More...
 
 ~QXmlQuery ()
 Destroys this QXmlQuery. More...
 

Properties

QXmlQueryPrivated
 

Friends

class QPatternist::VariableLoader
 
class QPatternist::XsdSchemaParser
 
class QPatternist::XsdValidatingInstanceReader
 
class QPatternistSDK::TestCase
 
class QXmlName
 
class QXmlSerializer
 
template<typename TInputType >
bool setFocusHelper (QXmlQuery *const queryInstance, const TInputType &focusValue)
 

Related Functions

(Note that these are not member functions.)

typedef QFlags< QXmlQuery::QueryLanguageQueryLanguages
 

Detailed Description

The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML.

Note
This class or function is reentrant.
Since
4.4

The QXmlQuery class compiles and executes queries written in the http://www.w3.org/TR/xquery/ {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 QAbstractXmlNodeModel {XML data model} as its delegate to the underlying query engine for traversing the data. The built-in data model is specified in http://www.w3.org/TR/xpath-datamodel/ {XQuery 1.0 and XPath 2.0 Data Model}.

QXmlQuery query;
query.setQuery("doc('index.html')/html/body/p[1]");
QXmlSerializer serializer(query, myOutputDevice);
query.evaluateTo(&serializer);

The example uses QXmlQuery to match the first paragraph of an XML document and then QXmlSerializer {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, see the documentation for QAbstractXmlNodeModel.

Running XQueries

To run a query set up with QXmlQuery, call one of the evaluation functions.

Running XPath Expressions

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 XSLT stylesheets

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().

query.setFocus(QUrl("myInput.xml"));
query.setQuery(QUrl("myStylesheet.xsl"));
query.evaluateTo(out);
Note
Currently, setFocus() must be called before setQuery() when using XSLT.

Another way to run an XSLT stylesheet is to use the xmlpatterns command line utility.

xmlpatterns myStylesheet.xsl myInput.xml
Note
For the current release, XSLT support should be considered experimental. See section message handler XSLT conformance for details.

Stylesheet parameters are bound using bindVariable().

Binding A Query To A Starting Node

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. See the example in the documentation for QAbstractXmlNodeModel.

Reentrancy and Thread Safety

QXmlQuery is reentrant 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. Behind the scenes, QXmlQuery will reuse resources such as opened files and compiled queries to the extent possible. But it is not safe to use the same instance of QXmlQuery in multiple threads.

Error Handling

Errors can occur during query evaluation. Examples include type errors and file loading errors. When an error occurs:

Resource Management

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.

Event Handling

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.

Definition at line 79 of file qxmlquery.h.

Enumerations

◆ QueryLanguage

Specifies whether you want QXmlQuery to interpret the input to setQuery() as an XQuery or as an XSLT stylesheet.

Since
4.5
  • XQuery10 XQuery 1.0.
  • XSLT20 XSLT 2.0
  • XmlSchema11IdentityConstraintSelector The selector, the restricted XPath pattern found in W3C XML Schema 1.1 for uniqueness contraints. Apart from restricting the syntax, the type check stage for the expression assumes a sequence of nodes to be the focus.
  • XmlSchema11IdentityConstraintField The field, the restricted XPath pattern found in W3C XML Schema 1.1 for uniqueness contraints. Apart from restricting the syntax, the type check stage for the expression assumes a sequence of nodes to be the focus.
  • XPath20 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.
See also
setQuery()
Enumerator
XQuery10 
XSLT20 
XmlSchema11IdentityConstraintSelector 
XmlSchema11IdentityConstraintField 
XPath20 

Definition at line 82 of file qxmlquery.h.

Constructors and Destructors

◆ QXmlQuery() [1/4]

QXmlQuery::QXmlQuery ( )

Constructs an invalid, empty query that cannot be used until setQuery() is called.

Note
This constructor must not be used if you intend to use this QXmlQuery to process XSL-T stylesheets. The other constructor must be used in that case.

Definition at line 261 of file qxmlquery.cpp.

261  : d(new QXmlQueryPrivate())
262 {
263 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ QXmlQuery() [2/4]

QXmlQuery::QXmlQuery ( const QXmlQuery other)

Constructs a QXmlQuery that is a copy of other.

The new instance will share resources with the existing query to the extent possible.

Definition at line 270 of file qxmlquery.cpp.

270  : d(new QXmlQueryPrivate(*other.d))
271 {
272  /* First we have invoked QXmlQueryPrivate's synthesized copy constructor.
273  * Keep this section in sync with QXmlQuery::operator=(). */
274  d->detach();
275 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ QXmlQuery() [3/4]

QXmlQuery::QXmlQuery ( const QXmlNamePool np)

Constructs a query that will use np as its name pool.

The query cannot be evaluated until setQuery() has been called.

Definition at line 281 of file qxmlquery.cpp.

281  : d(new QXmlQueryPrivate(np))
282 {
283 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ QXmlQuery() [4/4]

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.

Note
If your QXmlQuery will process XSL-T stylesheets, this constructor must be used. The default constructor can only create instances of QXmlQuery for running XQueries.
The XSL-T support in this release is considered experimental. See the 2.0 {XSLT conformance} for details.
Since
4.5
See also
queryLanguage()

Definition at line 301 of file qxmlquery.cpp.

302  : d(new QXmlQueryPrivate(np))
303 {
305 }
QueryLanguage queryLanguage() const
Returns a value indicating what this QXmlQuery is being used for.
Definition: qxmlquery.cpp:1093
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QXmlQuery::QueryLanguage queryLanguage
Definition: qxmlquery_p.h:292

◆ ~QXmlQuery()

QXmlQuery::~QXmlQuery ( )

Destroys this QXmlQuery.

Definition at line 310 of file qxmlquery.cpp.

311 {
312  delete d;
313 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

Functions

◆ bindVariable() [1/6]

void QXmlQuery::bindVariable ( const QXmlName name,
const QXmlItem value 
)

Binds the variable name to the value so that $name can be used from within the query to refer to the value.

name must not be null. {name}.isNull() must return false. If name has already been bound by a previous bindVariable() call, its previous binding will be overridden.

If {value} is null so that {value}.isNull() returns true, 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 such that QXmlItem's 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.

See also
QVariant::isValid(), {QtXDM}{How QVariant maps to XQuery's Data Model}, QXmlItem::isNull()

Definition at line 528 of file qxmlquery.cpp.

Referenced by bindVariable(), QDeclarativeXmlQueryEngine::doQueryJob(), and QDeclarativeXmlQueryEngine::doSubQueryJob().

529 {
530  if(name.isNull())
531  {
532  qWarning("The variable name cannot be null.");
533  return;
534  }
535 
537  const QVariant variant(QVariant::fromValue(value));
538 
539  /* If the type of the variable changed(as opposed to only the value),
540  * we will have to recompile. */
541  if(vl->invalidationRequired(name, variant) || value.isNull())
542  d->recompileRequired();
543 
544  vl->addBinding(name, variant);
545 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QPatternist::VariableLoader::Ptr variableLoader()
Definition: qxmlquery_p.h:125
void recompileRequired()
Definition: qxmlquery_p.h:120
Q_CORE_EXPORT void qWarning(const char *,...)
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
const char * variant
bool isNull() const
Returns true if this QXmlName is not initialized with a valid combination of {namespace URI}...
Definition: qxmlname.cpp:224
bool isNull() const
Returns true if this QXmlItem is neither a node nor an atomic value.

◆ bindVariable() [2/6]

void QXmlQuery::bindVariable ( const QString localName,
const QXmlItem value 
)

This function constructs a QXmlName from localName using the query's QXmlNamePool {namespace}.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

The function then behaves as the overloaded function. It is equivalent to the following snippet.

QXmlNamePool namePool(query.namePool());
query.bindVariable(QXmlName(namePool, localName), value);

Definition at line 559 of file qxmlquery.cpp.

560 {
561  bindVariable(QXmlName(d->namePool, localName), value);
562 }
void bindVariable(const QXmlName &name, const QXmlItem &value)
Binds the variable name to the value so that $name can be used from within the query to refer to the ...
Definition: qxmlquery.cpp:528
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
friend class QXmlName
Definition: qxmlquery.h:141

◆ bindVariable() [3/6]

void QXmlQuery::bindVariable ( const QXmlName name,
QIODevice device 
)

Binds the variable name to the device so that $name can be used from within the query to refer to the device.

The QIODevice device is exposed to the query as a URI of type xs:anyURI, which can be passed to the fn:doc() function to be read. E.g., this function can be used to pass an XML document in memory to fn:doc.

QByteArray myDocument;
QBuffer buffer(&myDocument); // This is a QIODevice.
buffer.open(QIODevice::ReadOnly);
QXmlQuery query;
query.bindVariable("myDocument", &buffer);
query.setQuery("doc($myDocument)");

The caller must ensure that device has been opened with at least QIODevice::ReadOnly prior to this binding. Otherwise, behavior is undefined.

If the query will access an XML document contained in a QString, use a QBuffer as shown in the following snippet. Suppose myQString contains <document>content</document>

QBuffer device;
device.setData(myQString.toUtf8());
QXmlQuery query;
query.bindVariable("inputDocument", &device);
query.setQuery("doc($inputDocument)/query[theDocument]");

name must not be null. {name}.isNull() must return false. If name has already been bound, its previous binding will be overridden. The URI that name evaluates to is arbitrary and may change.

If the type of the variable binding changes (e.g., if a previous binding by the same name was a QVariant, or if there was no previous binding), isValid() will return false, and recompilation of the query text is required. To recompile the query, call setQuery(). For this reason, bindVariable() should be called before setQuery(), if possible.

Note
device must not be deleted while this QXmlQuery exists.

Definition at line 598 of file qxmlquery.cpp.

599 {
600  if(device && !device->isReadable())
601  {
602  qWarning("A null, or readable QIODevice must be passed.");
603  return;
604  }
605 
606  if(name.isNull())
607  {
608  qWarning("The variable name cannot be null.");
609  return;
610  }
611 
613 
614  if(device)
615  {
616  const QVariant variant(QVariant::fromValue(device));
617 
618  if(vl->invalidationRequired(name, variant))
619  d->recompileRequired();
620 
621  vl->addBinding(name, variant);
622 
623  /* We need to tell the resource loader to discard its document, because
624  * the underlying QIODevice has changed, but the variable name is the
625  * same which means that the URI is the same, and hence the resource
626  * loader will return the document for the old QIODevice.
627  */
628  d->resourceLoader()->clear(QUrl(QLatin1String("tag:trolltech.com,2007:QtXmlPatterns:QIODeviceVariable:") + d->namePool.d->stringForLocalName(name.localName())));
629  }
630  else
631  {
632  vl->removeBinding(name);
633  d->recompileRequired();
634  }
635 }
const QString & stringForLocalName(const QXmlName::LocalNameCode code) const
Definition: qnamepool_p.h:168
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
QPatternist::VariableLoader::Ptr variableLoader()
Definition: qxmlquery_p.h:125
void recompileRequired()
Definition: qxmlquery_p.h:120
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
Q_CORE_EXPORT void qWarning(const char *,...)
QExplicitlySharedDataPointer< QPatternist::NamePool > d
Definition: qxmlnamepool.h:88
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QString localName(const QXmlNamePool &query) const
Returns the local name.
Definition: qxmlname.cpp:387
QPatternist::AccelTreeResourceLoader::Ptr resourceLoader()
Definition: qxmlquery_p.h:206
const char * variant
bool isNull() const
Returns true if this QXmlName is not initialized with a valid combination of {namespace URI}...
Definition: qxmlname.cpp:224

◆ bindVariable() [4/6]

void QXmlQuery::bindVariable ( const QString localName,
QIODevice device 
)

If localName is a valid QXmlName::isNCName() {NCName}, this function is equivalent to the following snippet.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

QXmlNamePool namePool(query.namePool());
query.bindVariable(QXmlName(namePool, localName), device);

A QXmlName is constructed from localName, and is passed to the appropriate overload along with device.

See also
QXmlName::isNCName()

Definition at line 653 of file qxmlquery.cpp.

654 {
655  bindVariable(QXmlName(d->namePool, localName), device);
656 }
void bindVariable(const QXmlName &name, const QXmlItem &value)
Binds the variable name to the value so that $name can be used from within the query to refer to the ...
Definition: qxmlquery.cpp:528
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
friend class QXmlName
Definition: qxmlquery.h:141

◆ bindVariable() [5/6]

void QXmlQuery::bindVariable ( const QXmlName name,
const QXmlQuery query 
)

Binds the result of the query query, to a variable by name name.

Evaluation of query will be commenced when this function is called.

If query is invalid, behavior is undefined. query will be copied.

Since
4.5
See also
isValid()

Definition at line 1197 of file qxmlquery.cpp.

1198 {
1199  Q_ASSERT_X(query.isValid(), Q_FUNC_INFO, "The query being bound must be valid.");
1200 
1202  const QVariant variant(QVariant::fromValue(query));
1203 
1204  if(vl->invalidationRequired(name, variant))
1205  d->recompileRequired();
1206 
1207  vl->addBinding(name, variant);
1208 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isValid() const
Returns true if this query is valid.
Definition: qxmlquery.cpp:885
QPatternist::VariableLoader::Ptr variableLoader()
Definition: qxmlquery_p.h:125
void recompileRequired()
Definition: qxmlquery_p.h:120
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
const char * variant
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ bindVariable() [6/6]

void QXmlQuery::bindVariable ( const QString localName,
const QXmlQuery query 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Has the same behavior and effects as the function being overloaded, but takes the variable name localName as a QString.

query is used as in the overloaded function.

Since
4.5

Definition at line 1219 of file qxmlquery.cpp.

1220 {
1221  return bindVariable(QXmlName(d->namePool, localName), query);
1222 }
void bindVariable(const QXmlName &name, const QXmlItem &value)
Binds the variable name to the value so that $name can be used from within the query to refer to the ...
Definition: qxmlquery.cpp:528
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
friend class QXmlName
Definition: qxmlquery.h:141

◆ evaluateTo() [1/5]

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.

See also
QXmlResultItems::next()

Definition at line 815 of file qxmlquery.cpp.

Referenced by QDeclarativeXmlQueryEngine::doQueryJob(), QDeclarativeXmlQueryEngine::doSubQueryJob(), evaluateTo(), QDeclarativeXmlQueryEngine::getValuesOfKeyRoles(), and QPatternist::XsdValidatingInstanceReader::selectNodeSets().

816 {
817  if(!result)
818  {
819  qWarning("A null pointer cannot be passed.");
820  return;
821  }
822 
823  if(isValid())
824  {
825  try
826  {
827  /*
828  * We don't have the d->expression() calls and
829  * d->dynamicContext() calls in the same order as seen in
830  * QXmlQuery::evaluateTo(), and the reason to why
831  * that isn't a problem, is that we call isValid().
832  */
834  result->d_ptr->setDynamicContext(dynContext);
835  result->d_ptr->iterator = d->expression()->evaluateSequence(dynContext);
836  }
837  catch(const QPatternist::Exception)
838  {
840  result->d_ptr->hasError = true;
841  }
842  }
843  else
844  {
846  result->d_ptr->hasError = true;
847  }
848 }
bool isValid() const
Returns true if this query is valid.
Definition: qxmlquery.cpp:885
static const EmptyIterator< Item >::Ptr emptyIterator
void setDynamicContext(const QPatternist::DynamicContext::Ptr &context)
QScopedPointer< QXmlResultItemsPrivate > d_ptr
Q_CORE_EXPORT void qWarning(const char *,...)
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPatternist::Item::Iterator::Ptr iterator
QPatternist::Expression::Ptr expression(QIODevice *const queryDevice=0)
Definition: qxmlquery_p.h:235
QPatternist::DynamicContext::Ptr dynamicContext(QAbstractXmlReceiver *const callback=0)
Definition: qxmlquery_p.h:175

◆ evaluateTo() [2/5]

bool QXmlQuery::evaluateTo ( QAbstractXmlReceiver callback) const

Evaluates this query and sends the result as a sequence of callbacks to the QAbstractXmlReceiver {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 isValid() {is invalid}, false is returned and the behavior is undefined. If callback is null, behavior is undefined.

See also
QAbstractXmlReceiver, isValid()

Definition at line 672 of file qxmlquery.cpp.

673 {
674  if(!callback)
675  {
676  qWarning("A non-null callback must be passed.");
677  return false;
678  }
679 
680  if(isValid())
681  {
682  try
683  {
684  /*
685  * This order is significant. expression() might cause
686  * query recompilation, and as part of that it recreates
687  * the static context. However, if we create the dynamic
688  * context before the query recompilation has been
689  * triggered, it will use the old static context, and
690  * hence old source locations.
691  */
693  const QPatternist::DynamicContext::Ptr dynContext(d->dynamicContext(callback));
694  callback->startOfSequence();
695  expr->evaluateToSequenceReceiver(dynContext);
696  callback->endOfSequence();
697  return true;
698  }
699  catch(const QPatternist::Exception)
700  {
701  return false;
702  }
703  }
704  else
705  return false;
706 }
bool isValid() const
Returns true if this query is valid.
Definition: qxmlquery.cpp:885
virtual void endOfSequence()=0
This callback is called once only, right after the XQuery Sequence ends.
Q_CORE_EXPORT void qWarning(const char *,...)
virtual void startOfSequence()=0
This callback is called once only, right before the XQuery Sequence begins.
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPatternist::Expression::Ptr expression(QIODevice *const queryDevice=0)
Definition: qxmlquery_p.h:235
QPatternist::DynamicContext::Ptr dynamicContext(QAbstractXmlReceiver *const callback=0)
Definition: qxmlquery_p.h:175

◆ evaluateTo() [3/5]

bool QXmlQuery::evaluateTo ( QStringList target) const

Attempts to evaluate the query and returns the results in the target QStringList {string list}.

If the query isValid() {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.

Definition at line 723 of file qxmlquery.cpp.

724 {
725  if(!target)
726  {
727  qWarning("A non-null callback must be passed.");
728  return false;
729  }
730 
731  if(isValid())
732  {
733  try
734  {
735  /*
736  * This order is significant. expression() might cause
737  * query recompilation, and as part of that it recreates
738  * the static context. However, if we create the dynamic
739  * context before the query recompilation has been
740  * triggered, it will use the old static context, and
741  * hence old source locations.
742  */
744  if(!expr)
745  return false;
746 
748 
749  if(!QPatternist::BuiltinTypes::xsString->xdtTypeMatches(expr->staticType()->itemType()))
750  return false;
751 
752  const QPatternist::Item::Iterator::Ptr it(expr->evaluateSequence(dynContext));
753  QPatternist::Item next(it->next());
754 
755  while(!next.isNull())
756  {
757  target->append(next.stringValue());
758  next = it->next();
759  }
760 
761  return true;
762  }
763  catch(const QPatternist::Exception)
764  {
765  return false;
766  }
767  }
768  else
769  return false;
770 }
#define it(className, varName)
bool isValid() const
Returns true if this query is valid.
Definition: qxmlquery.cpp:885
static const AtomicType::Ptr xsString
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
Q_CORE_EXPORT void qWarning(const char *,...)
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
QPatternist::Expression::Ptr expression(QIODevice *const queryDevice=0)
Definition: qxmlquery_p.h:235
QPatternist::DynamicContext::Ptr dynamicContext(QAbstractXmlReceiver *const callback=0)
Definition: qxmlquery_p.h:175

◆ evaluateTo() [4/5]

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. In a future release, it is expected that this function will be changed to respect serialization options set in the stylesheet.

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.

Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 789 of file qxmlquery.cpp.

790 {
791  if(!target)
792  {
793  qWarning("The pointer to the device cannot be null.");
794  return false;
795  }
796 
797  if(!target->isWritable())
798  {
799  qWarning("The device must be writable.");
800  return false;
801  }
802 
803  QXmlSerializer serializer(*this, target);
804  return evaluateTo(&serializer);
805 }
void evaluateTo(QXmlResultItems *result) const
Starts the evaluation and makes it available in result.
Definition: qxmlquery.cpp:815
bool isWritable() const
Returns true if data can be written to the device; otherwise returns false.
Definition: qiodevice.cpp:558
The QXmlSerializer class is an implementation of QAbstractXmlReceiver for transforming XQuery output ...
Q_CORE_EXPORT void qWarning(const char *,...)

◆ evaluateTo() [5/5]

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 behavior is undefined. QXmlQuery does not take ownership of output.

Internally, the class QXmlFormatter is used for this.

Since
4.5

Definition at line 863 of file qxmlquery.cpp.

864 {
865  Q_ASSERT_X(output, Q_FUNC_INFO,
866  "The input cannot be null");
867 
868  QBuffer outputDevice;
869  outputDevice.open(QIODevice::ReadWrite);
870 
871  QXmlFormatter formatter(*this, &outputDevice);
872  const bool success = evaluateTo(&formatter);
873 
874  outputDevice.close();
875  *output = QString::fromUtf8(outputDevice.data().constData());
876 
877  return success;
878 }
void evaluateTo(QXmlResultItems *result) const
Starts the evaluation and makes it available in result.
Definition: qxmlquery.cpp:815
const QByteArray & data() const
Returns the data contained in the buffer.
Definition: qbuffer.cpp:301
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
void close()
Reimplemented Function
Definition: qbuffer.cpp:359
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
The QXmlFormatter class is an implementation of QXmlSerializer for transforming XQuery output into fo...
Definition: qxmlformatter.h:58
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ initialTemplateName()

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 that case, a default constructed QXmlName is returned.

Since
4.5

Definition at line 1159 of file qxmlquery.cpp.

1160 {
1161  return d->initialTemplateName;
1162 }
QXmlName initialTemplateName
Definition: qxmlquery_p.h:285
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ isValid()

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.

Definition at line 885 of file qxmlquery.cpp.

Referenced by bindVariable(), QDeclarativeXmlQueryEngine::doSubQueryJob(), evaluateTo(), and QPatternist::XsdSchemaParser::readXPathAttribute().

886 {
887  return d->isValid();
888 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ messageHandler()

QAbstractMessageHandler * QXmlQuery::messageHandler ( ) const

Returns the message handler that handles compile and runtime messages for this QXmlQuery.

Definition at line 380 of file qxmlquery.cpp.

381 {
382  return d->messageHandler;
383 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPointer< QAbstractMessageHandler > messageHandler
Definition: qxmlquery_p.h:278

◆ namePool()

QXmlNamePool QXmlQuery::namePool ( ) const

Returns the name pool used by this QXmlQuery for constructing QXmlName {names}.

There is no setter for the name pool, because mixing name pools causes errors due to name confusion.

Definition at line 933 of file qxmlquery.cpp.

934 {
935  return d->namePool;
936 }
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ networkAccessManager()

QNetworkAccessManager * QXmlQuery::networkAccessManager ( ) const

Returns the network manager, or 0 if it has not been set.

See also
setNetworkAccessManager()
Since
4.5

Definition at line 1182 of file qxmlquery.cpp.

1183 {
1184  return d->m_networkAccessDelegator->m_genericManager;
1185 }
QPatternist::NetworkAccessDelegator::Ptr m_networkAccessDelegator
Definition: qxmlquery_p.h:319
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ operator=()

QXmlQuery & QXmlQuery::operator= ( const QXmlQuery other)

Assigns other to this QXmlQuery instance.

Definition at line 318 of file qxmlquery.cpp.

319 {
320  /* Keep this section in sync with QXmlQuery::QXmlQuery(const QXmlQuery &).
321  */
322  if(d != other.d)
323  {
324  *d = *other.d;
325  d->detach();
326  }
327 
328  return *this;
329 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ queryLanguage()

QXmlQuery::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.

Since
4.5

Definition at line 1093 of file qxmlquery.cpp.

Referenced by QXmlQuery().

1094 {
1095  return d->queryLanguage;
1096 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QXmlQuery::QueryLanguage queryLanguage
Definition: qxmlquery_p.h:292

◆ setFocus() [1/4]

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.

Definition at line 955 of file qxmlquery.cpp.

956 {
957  d->contextItem = item;
958 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QXmlItem contextItem
Definition: qxmlquery_p.h:284

◆ setFocus() [2/4]

bool QXmlQuery::setFocus ( const QUrl documentURI)

Sets the focus to be the document located at documentURI and returns true.

Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

If documentURI cannot 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.

Definition at line 1025 of file qxmlquery.cpp.

1026 {
1027  Q_ASSERT_X(documentURI.isValid() && !documentURI.isEmpty(),
1028  Q_FUNC_INFO,
1029  "The URI passed must be valid.");
1030 
1031  return setFocusHelper(this, QVariant(documentURI));
1032 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
friend bool setFocusHelper(QXmlQuery *const queryInstance, const TInputType &focusValue)
Definition: qxmlquery.cpp:967
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ setFocus() [3/4]

bool QXmlQuery::setFocus ( QIODevice document)

Sets the focus to be the document read from the QIODevice and returns true.

If document cannot 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.

Since
4.5 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1048 of file qxmlquery.cpp.

1049 {
1050  if(!document)
1051  {
1052  qWarning("A null QIODevice pointer cannot be passed.");
1053  return false;
1054  }
1055 
1056  if(!document->isReadable())
1057  {
1058  qWarning("The device must be readable.");
1059  return false;
1060  }
1061 
1062  return setFocusHelper(this, document);
1063 }
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
Q_CORE_EXPORT void qWarning(const char *,...)
friend bool setFocusHelper(QXmlQuery *const queryInstance, const TInputType &focusValue)
Definition: qxmlquery.cpp:967

◆ setFocus() [4/4]

bool QXmlQuery::setFocus ( const QString focus)

This function behaves identically to calling the setFocus() overload with a QIODevice whose content is focus encoded as UTF-8.

That is, focus is treated as if it contained an XML document.

Returns the same result as the overload.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
4.6

Definition at line 1075 of file qxmlquery.cpp.

1076 {
1077  QBuffer device;
1078  device.setData(focus.toUtf8());
1079  device.open(QIODevice::ReadOnly);
1080 
1081  return setFocusHelper(this, &device);
1082 }
void setData(const QByteArray &data)
Sets the contents of the internal buffer to be data.
Definition: qbuffer.cpp:315
bool open(OpenMode openMode)
Reimplemented Function
Definition: qbuffer.cpp:338
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
friend bool setFocusHelper(QXmlQuery *const queryInstance, const TInputType &focusValue)
Definition: qxmlquery.cpp:967

◆ setInitialTemplateName() [1/2]

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 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 the stylesheet has no template named name, the processor will use the standard order of template invocation.

Since
4.5
See also
initialTemplateName()

Definition at line 1114 of file qxmlquery.cpp.

Referenced by setInitialTemplateName().

1115 {
1117 }
QXmlName initialTemplateName
Definition: qxmlquery_p.h:285
const char * name
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ setInitialTemplateName() [2/2]

void QXmlQuery::setInitialTemplateName ( const QString localName)

Sets the name of the initial template to localName, which must be a valid local name.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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.

Since
4.5
See also
initialTemplateName()

Definition at line 1142 of file qxmlquery.cpp.

1143 {
1144  Q_ASSERT_X(QXmlName::isNCName(localName),
1145  Q_FUNC_INFO,
1146  "The name passed must be a valid NCName.");
1147  setInitialTemplateName(QXmlName(d->namePool, localName));
1148 }
QXmlNamePool namePool
Definition: qxmlquery_p.h:277
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
static bool isNCName(const QString &candidate)
Returns true if candidate is an NCName.
Definition: qxmlname.cpp:445
friend class QXmlName
Definition: qxmlquery.h:141
void setInitialTemplateName(const QXmlName &name)
Sets the name of the initial template.
Definition: qxmlquery.cpp:1114
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ setMessageHandler()

void QXmlQuery::setMessageHandler ( QAbstractMessageHandler aMessageHandler)

Changes the QAbstractMessageHandler {message handler} for this QXmlQuery to aMessageHandler.

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. It writes compile and runtime messages to stderr. The default message handler includes color codes if stderr can render colors.

Note that changing the message handler after the query has been compiled has no effect, i.e. 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:

message() argument Semantics
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.

Definition at line 371 of file qxmlquery.cpp.

372 {
373  d->messageHandler = aMessageHandler;
374 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPointer< QAbstractMessageHandler > messageHandler
Definition: qxmlquery_p.h:278

◆ setNetworkAccessManager()

void QXmlQuery::setNetworkAccessManager ( QNetworkAccessManager newManager)

Sets the network manager to newManager.

QXmlQuery does not take ownership of newManager.

See also
networkAccessManager()
Since
4.5

Definition at line 1171 of file qxmlquery.cpp.

1172 {
1173  d->m_networkAccessDelegator->m_genericManager = newManager;
1174 }
QPatternist::NetworkAccessDelegator::Ptr m_networkAccessDelegator
Definition: qxmlquery_p.h:319
QXmlQueryPrivate * d
Definition: qxmlquery.h:149

◆ setQuery() [1/3]

void QXmlQuery::setQuery ( const QString sourceCode,
const QUrl documentURI = QUrl() 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. 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.

Definition at line 441 of file qxmlquery.cpp.

Referenced by QDeclarativeXmlQueryEngine::doQueryJob(), QDeclarativeXmlQueryEngine::doSubQueryJob(), QDeclarativeXmlQueryEngine::getValuesOfKeyRoles(), QPatternist::XsdSchemaParser::readXPathAttribute(), and setQuery().

442 {
443  Q_ASSERT_X(documentURI.isEmpty() || documentURI.isValid(), Q_FUNC_INFO,
444  "The document URI must be valid.");
445 
446  QByteArray query(sourceCode.toUtf8());
447  QBuffer buffer(&query);
448  buffer.open(QIODevice::ReadOnly);
449 
450  setQuery(&buffer, documentURI);
451 }
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
void setQuery(const QString &sourceCode, const QUrl &documentURI=QUrl())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qxmlquery.cpp:441
The QBuffer class provides a QIODevice interface for a QByteArray.
Definition: qbuffer.h:57
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ setQuery() [2/3]

void QXmlQuery::setQuery ( QIODevice sourceCode,
const QUrl documentURI = QUrl() 
)

Sets this QXmlQuery to an XQuery read from the sourceCode device.

The device must have been opened with at least QIODevice::ReadOnly.

documentURI represents the query obtained from the sourceCode device. It is the base URI of the static context, as defined in the http://www.w3.org/TR/xquery/ {XQuery language}. It is used internally to resolve relative URIs that appear in the query, and for message reporting. 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, it is resolved itself against the QCoreApplication::applicationFilePath() {application file path} before it is used. If documentURI is neither a valid URI nor empty, the result 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 http://www.w3.org/TR/xquery/ {XQuery language}.

If sourceCode is null or not readable, or if documentURI is not a valid URI, behavior is undefined.

See also
isValid()

Definition at line 416 of file qxmlquery.cpp.

417 {
418  if(!sourceCode)
419  {
420  qWarning("A null QIODevice pointer cannot be passed.");
421  return;
422  }
423 
424  if(!sourceCode->isReadable())
425  {
426  qWarning("The device must be readable.");
427  return;
428  }
429 
431  d->expression(sourceCode);
432 }
bool isReadable() const
Returns true if data can be read from the device; otherwise returns false.
Definition: qiodevice.cpp:544
Q_CORE_EXPORT void qWarning(const char *,...)
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPatternist::Expression::Ptr expression(QIODevice *const queryDevice=0)
Definition: qxmlquery_p.h:235
static QUrl normalizeQueryURI(const QUrl &uri)

◆ setQuery() [3/3]

void QXmlQuery::setQuery ( const QUrl queryURI,
const QUrl baseURI = QUrl() 
)

Sets this QXmlQuery to the XQuery read from the queryURI.

Use isValid() after calling this function. If an error occurred reading queryURI, e.g., the query does not exist, cannot 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 http://www.w3.org/TR/xquery/ {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 QCoreApplication::applicationFilePath() {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.

Definition at line 473 of file qxmlquery.cpp.

474 {
475  Q_ASSERT_X(queryURI.isValid(), Q_FUNC_INFO, "The passed URI must be valid.");
476 
477  const QUrl canonicalURI(QPatternist::XPathHelper::normalizeQueryURI(queryURI));
478  Q_ASSERT(canonicalURI.isValid());
479  Q_ASSERT(!canonicalURI.isRelative());
480  Q_ASSERT(baseURI.isValid() || baseURI.isEmpty());
481 
482  d->queryURI = QPatternist::XPathHelper::normalizeQueryURI(baseURI.isEmpty() ? queryURI : baseURI);
483 
485 
486  try
487  {
489  d->staticContext()));
490  }
491  catch(const QPatternist::Exception)
492  {
493  /* We do nothing, result will be 0. */
494  }
495 
496  if(result)
497  {
498  setQuery(result.data(), d->queryURI);
499  result->close();
500  }
501  else
502  d->recompileRequired();
503 }
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
A smart pointer very similar to std::auto_ptr.
Definition: qautoptr_p.h:73
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
void setQuery(const QString &sourceCode, const QUrl &documentURI=QUrl())
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qxmlquery.cpp:441
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QPatternist::NetworkAccessDelegator::Ptr m_networkAccessDelegator
Definition: qxmlquery_p.h:319
void recompileRequired()
Definition: qxmlquery_p.h:120
void reset(T *other=0)
Definition: qautoptr_p.h:154
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
static QNetworkReply * load(const QUrl &uri, QNetworkAccessManager *const networkManager, const ReportContext::Ptr &context, ErrorHandling handling=FailOnError)
Helper function that do NetworkAccessDelegator::get(), but does it blocked.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
static QUrl normalizeQueryURI(const QUrl &uri)
QPatternist::GenericStaticContext::Ptr staticContext()
Definition: qxmlquery_p.h:133
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ setUriResolver()

void QXmlQuery::setUriResolver ( const QAbstractUriResolver resolver)

Sets the URI resolver to resolver.

QXmlQuery does not take ownership of resolver.

See also
uriResolver()

Definition at line 896 of file qxmlquery.cpp.

897 {
898  d->uriResolver = resolver;
899 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
const QAbstractUriResolver * uriResolver
Definition: qxmlquery_p.h:283

◆ uriResolver()

const QAbstractUriResolver * QXmlQuery::uriResolver ( ) const

Returns the query's URI resolver.

If no URI resolver has been set, QtXmlPatterns 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 physical ones, or it can translate obsolete or invalid URIs to valid ones.

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

See also
setUriResolver()

Definition at line 924 of file qxmlquery.cpp.

925 {
926  return d->uriResolver;
927 }
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
const QAbstractUriResolver * uriResolver
Definition: qxmlquery_p.h:283

Friends and Related Functions

◆ QPatternist::VariableLoader

Definition at line 146 of file qxmlquery.h.

◆ QPatternist::XsdSchemaParser

Definition at line 144 of file qxmlquery.h.

◆ QPatternist::XsdValidatingInstanceReader

Definition at line 145 of file qxmlquery.h.

◆ QPatternistSDK::TestCase

friend class QPatternistSDK::TestCase
friend

Definition at line 143 of file qxmlquery.h.

◆ QueryLanguages

Warning
This function is not part of the public interface.

Definition at line 245 of file qquerytransformparser.cpp.

◆ QXmlName

friend class QXmlName
friend

Definition at line 141 of file qxmlquery.h.

Referenced by bindVariable(), and setInitialTemplateName().

◆ QXmlSerializer

friend class QXmlSerializer
friend

Definition at line 142 of file qxmlquery.h.

◆ setFocusHelper

template<typename TInputType >
bool setFocusHelper ( QXmlQuery *const  queryInstance,
const TInputType &  focusValue 
)
friend

This function should be a private member function of QXmlQuery, but we don't dare that due to our weird compilers.

Warning
This function is not part of the public interface.

Definition at line 967 of file qxmlquery.cpp.

Referenced by setFocus().

969 {
970  /* We call resourceLoader(), so we have ensured that we have a resourceLoader
971  * that we will share in our copy. */
972  queryInstance->d->resourceLoader();
973 
974  QXmlQuery focusQuery(*queryInstance);
975 
976  /* Now we use the same, so we own the loaded document. */
977  focusQuery.d->m_resourceLoader = queryInstance->d->m_resourceLoader;
978 
979  /* The copy constructor doesn't allow us to copy an existing QXmlQuery and
980  * changing the language at the same time so we need to use private API. */
981  focusQuery.d->queryLanguage = QXmlQuery::XQuery10;
982 
983  Q_ASSERT(focusQuery.queryLanguage() == QXmlQuery::XQuery10);
984  focusQuery.bindVariable(QChar::fromLatin1('u'), focusValue);
985  focusQuery.setQuery(QLatin1String("doc($u)"));
986  Q_ASSERT(focusQuery.isValid());
987 
988  QXmlResultItems focusResult;
989 
990  queryInstance->d->m_resourceLoader = focusQuery.d->m_resourceLoader;
991 
992  focusQuery.evaluateTo(&focusResult);
993  const QXmlItem focusItem(focusResult.next());
994 
995  if(focusItem.isNull() || focusResult.hasError())
996  {
997  /* The previous focus must be cleared in error situations.
998  * Otherwise the query may be left in an inconsistent state. */
999  queryInstance->setFocus(QXmlItem());
1000  return false;
1001  }
1002  else
1003  {
1004  queryInstance->setFocus(focusItem);
1005  return true;
1006  }
1007 }
The QXmlResultItems class iterates through the results of evaluating an XQuery in QXmlQuery...
The QXmlItem class contains either an XML node or an atomic value.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static QChar fromLatin1(char c)
Converts the Latin-1 character c to its equivalent QChar.
Definition: qchar.h:378
QXmlQueryPrivate * d
Definition: qxmlquery.h:149
QPatternist::DeviceResourceLoader::Ptr m_resourceLoader
Definition: qxmlquery_p.h:306
QPatternist::AccelTreeResourceLoader::Ptr resourceLoader()
Definition: qxmlquery_p.h:206
void setFocus(const QXmlItem &item)
Sets the focus to item.
Definition: qxmlquery.cpp:955
The QXmlQuery class performs XQueries on XML data, or on non-XML data modeled to look like XML...
Definition: qxmlquery.h:79

Properties

◆ d

QXmlQueryPrivate* QXmlQuery::d
private

The documentation for this class was generated from the following files: