QXmlQuery

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

Inheritance diagram of PySide2.QtXmlPatterns.QXmlQuery

概要

函数

详细描述

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.

使用 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 .

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 output the result to a device as XML.

使用 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 使用 setQuery() .

Running XSLT Stylesheets

Running an XSLT stylesheet is like running an XQuery , except that when you construct your QXmlQuery , you must pass 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() .

QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(QUrl("myInput.xml"));
query.setQuery(QUrl("myStylesheet.xsl"));
query.evaluateTo(out);
												

注意

目前, 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
												

注意

For the current release, XSLT support should be considered experimental. See section XSLT conformance 了解细节。

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 .

重入和线程安全

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.

错误处理

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

资源管理

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.

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 实例。

An instance of QCoreApplication must exist before QXmlQuery 可以使用。

事件处理

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.

class QXmlQuery

QXmlQuery(queryLanguage[, np=QXmlNamePool()])

QXmlQuery(np)

QXmlQuery(other)

param np

QXmlNamePool

param other

QXmlQuery

param queryLanguage

QueryLanguage

Constructs an invalid, empty query that cannot be used until setQuery() 被调用。

注意

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.

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 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 XSLT conformance 了解细节。

另请参阅

queryLanguage()

PySide2.QtXmlPatterns.QXmlQuery. QueryLanguage

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

常量

描述

QXmlQuery.XQuery10

XQuery 1.0.

QXmlQuery.XSLT20

XSLT 2.0 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. 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. Signifies XPath 2.0. Has no effect in the public API, it’s used internally. As With and , the type check stage for the expression assumes a sequence of nodes to be the focus.

另请参阅

setQuery()

PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( localName , arg__2 )
参数
  • localName – unicode

  • arg__2 QIODevice

PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( localName , value )
参数
PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( localName , query )
参数
PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( name , arg__2 )
参数
PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( name , value )
参数
PySide2.QtXmlPatterns.QXmlQuery. bindVariable ( name , query )
参数
PySide2.QtXmlPatterns.QXmlQuery. evaluateTo ( callback )
参数

callback QAbstractXmlReceiver

返回类型

bool

Evaluates this query and sends the result as a sequence of callbacks to the receiver callback . QXmlQuery 未拥有所有权对于 callback .

If an error occurs during the evaluation, error messages are sent to messageHandler() and false 被返回。

If this query is invalid , false is returned and the behavior is undefined. If callback is null, behavior is undefined.

PySide2.QtXmlPatterns.QXmlQuery. evaluateTo ( target )
参数

target QIODevice

返回类型

bool

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 被返回。

target is null , or is not opened in at least WriteOnly mode, the behavior is undefined. QXmlQuery 未拥有所有权对于 target .

这是重载函数。

PySide2.QtXmlPatterns.QXmlQuery. evaluateTo ( result )
参数

result QXmlResultItems

Starts the evaluation and makes it available in result 。若 result is null, the behavior is undefined. The evaluation takes place incrementally (lazy evaluation), as the caller uses next() to get the next result.

另请参阅

next()

PySide2.QtXmlPatterns.QXmlQuery. initialTemplateName ( )
返回类型

QXmlName

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 被返回。

PySide2.QtXmlPatterns.QXmlQuery. isValid ( )
返回类型

bool

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.

PySide2.QtXmlPatterns.QXmlQuery. messageHandler ( )
返回类型

QAbstractMessageHandler

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

PySide2.QtXmlPatterns.QXmlQuery. namePool ( )
返回类型

QXmlNamePool

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.

PySide2.QtXmlPatterns.QXmlQuery. networkAccessManager ( )
返回类型

QNetworkAccessManager

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

PySide2.QtXmlPatterns.QXmlQuery. queryLanguage ( )
返回类型

QueryLanguage

Returns a value indicating what this QXmlQuery is being used for. The default is XQuery10 ,这意味着 QXmlQuery is being used for running XQuery and XPath queries. XSLT20 can also be returned, which indicates the QXmlQuery is for running XSL-T spreadsheets.

PySide2.QtXmlPatterns.QXmlQuery. setFocus ( document )
参数

document QIODevice

返回类型

bool

Sets the focus to be the document read from the QIODevice and returns true. If document cannot be loaded, false is returned.

QXmlQuery 未拥有所有权对于 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.

这是重载函数。

PySide2.QtXmlPatterns.QXmlQuery. setFocus ( focus )
参数

focus – unicode

返回类型

bool

PySide2.QtXmlPatterns.QXmlQuery. setFocus ( documentURI )
参数

documentURI QUrl

返回类型

bool

PySide2.QtXmlPatterns.QXmlQuery. setFocus ( item )
参数

item QXmlItem

PySide2.QtXmlPatterns.QXmlQuery. setInitialTemplateName ( name )
参数

name – unicode

PySide2.QtXmlPatterns.QXmlQuery. setInitialTemplateName ( name )
参数

name QXmlName

PySide2.QtXmlPatterns.QXmlQuery. setMessageHandler ( messageHandler )
参数

messageHandler QAbstractMessageHandler

改变 message handler for this QXmlQuery to aMessageHandler . The query sends all compile and runtime messages to this message handler. QXmlQuery 未拥有所有权对于 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.

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

另请参阅

messageHandler()

PySide2.QtXmlPatterns.QXmlQuery. setNetworkAccessManager ( newManager )
参数

newManager QNetworkAccessManager

将网络管理器设为 newManager . QXmlQuery 未拥有所有权对于 newManager .

PySide2.QtXmlPatterns.QXmlQuery. setQuery ( sourceCode [ , documentURI=QUrl() ] )
参数
  • sourceCode QIODevice

  • documentURI QUrl

PySide2.QtXmlPatterns.QXmlQuery. setQuery ( sourceCode [ , documentURI=QUrl() ] )
参数
  • sourceCode – unicode

  • documentURI QUrl

PySide2.QtXmlPatterns.QXmlQuery. setQuery ( queryURI [ , baseURI=QUrl() ] )
参数
  • queryURI QUrl

  • baseURI QUrl

PySide2.QtXmlPatterns.QXmlQuery. setUriResolver ( resolver )
参数

resolver QAbstractUriResolver

将 URI 解析器设为 resolver . QXmlQuery 未拥有所有权对于 resolver .

另请参阅

uriResolver()

PySide2.QtXmlPatterns.QXmlQuery. uriResolver ( )
返回类型

QAbstractUriResolver

Returns the query’s URI resolver. If no URI resolver has been set, Qt XML Patterns 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.

Qt XML Patterns calls the URI resolver for all URIs it encounters, except for namespaces. Specifically, all builtin functions that deal with URIs ( fn:doc() ,和 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 resolve() 会被使用。

When Qt XML Patterns calls resolve() the absolute URI is the URI mandated by the XQuery language, and the relative URI is the URI specified by the user.

另请参阅

setUriResolver()