Skip to content

PythonQt.Qt.QAbstractXmlNodeModel

QAbstractXmlNodeModel

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

The QAbstractXmlNodeModel specifies the interface that a node model must implement for that node model be accessible to the query engine for processing XQuery queries. A node model represents data as a structure that can be queried as if the data were XML.

The node model represented by a subclass of QAbstractXmlNodeModel is meant to be accessed by the Qt XML Patterns query engine. If the API seems a little strange in a few places, it is because the member functions are called by the query engine as it evaluates an XQuery. They aren't meant to be used programatically.

Usage

QAbstractXmlNodeModel bridges the gap between the arbitrary structure of the non-XML data to be queried and the well-defined structure of XML data understood by QXmlQuery.

Consider a chemistry application that reads the file chemistryData, which contains non-XML data that represents a chemical structure composed of molecules and atoms. The application will query this chemistry data with an XQuery it reads from file queryFile. We write a custom subclass of QAbstractXmlNodeModel (ChemistryNodeModel) that reads chemistryData and builds a data structure, perhaps composed of objects of our own classes molecule and atom. Clearly, this data structure is not XML. Our custom subclass will know how to traverse this non-XML structure and present it through the XPath Data Model interface.

QFile queryFile(argv[1]);
QFile chemistryData(argv[2]);
QString moleculeName = argv[3];

QXmlQuery query;
query.setQuery(&queryFile, QUrl::fromLocalFile(queryFile.fileName()));

ChemistryNodeModel myNodeModel(query.namePool(), chemistryData);
QXmlNodeModelIndex startNode = myNodeModel.nodeFor(moleculeName);
query.bindVariable("queryRoot", startNode);

QFile out;
out.open(stdout, QIODevice::WriteOnly);

QXmlSerializer serializer(query, &out);
query.evaluateTo(&serializer);

The application first creates an instance of QXmlQuery and calls setQuery() to read queryFile containing the XQuery we want to run. Then it creates an instance of our custom node model class, ChemistryNodeModel, which is a subclass of QAbstractXmlNodeModel. Its constructor is called with the name pool obtained from our QXmlQuery, and with the chemistryFile containing the structure of molecules and atoms to be queried. The name pool is required because our custom node model has the member function name(), which returns the name of any node in the model. The query and the custom node model must use the same name pool for constructing these names. The constructor would then read chemistryFile and build the custom node model structure.

To connect the query to the custom node model, we must bind a variable name used in the query to a node in the model. The variable can then be used in the query as a starting node. First, an index for the desired starting node is retrieved by calling QAbstractXmlNodeModel::createIndex(). Then the index is bound to a variable name, in this case queryRoot, by passing the name and the index to QXmlQuery::bindVariable(). The query can then use a variable reference $queryRoot to refer to the starting node. Note that if the query uses multiple variable references, a call to QXmlQuery::bindVariable() is required to bind each different variable name to a node in the model.

The query is executed when the application calls one of the QXmlQuery evaluation functions. The application uses QXmlQuery::evaluateTo(QAbstractXmlReceiver *), because it then uses a serializer to out the query result as XML to stdout. We could have used QXmlQuery::evaluateTo(QXmlResultItems *) to get a list of result items, or QXmlQuery::evaluateTo(QStringList *) if the query evaluated to a sequence of xs:string values.

During query execution, the engine iterates over the node model using nextFromSimpleAxis() to get the index of the next node to be visited. The engine can get the name of a node by calling name() with the node's index. stringValue(), baseUri(), documentUri() and kind() are also called as needed with a node index.

The example demonstrates the standard pattern for using a subclass of QAbstractXmlNodeModel in combination with QXmlQuery to perform an XQuery.

  1. Instantiate QXmlQuery and give it the XQuery to be run;
  2. Instantiate a subclass of QAbstractXmlNodeModel or QSimpleXmlNodeModel;
  3. Retrieve a QXmlNodeModelIndex for the node in the model where the QXmlQuery should start the query;
  4. Use QXmlQuery::bindVariable() to bind the QXmlNodeModelIndex to $variable name;
  5. Call one of the QXmlQuery evaluation functions to run the query.

Subclassing

Because the XPath Data Model interface presented by QAbstractXmlNodeModel allows QXmlQuery to operate on non-XML data as if it were XML, implementing subclasses of QAbstractXmlNodeModel can involve a significant amount of work. The QSimpleXmlNodeModel class is provided to simplify the implementation for many common use cases.

Thread Safety

Because the node model can be accessed concurrently by threads in the Qt XML Patterns module, subclasses of QAbstractXmlNodeModel must be written to be thread-safe. Classes that simplify implementing thread-safety include QReadLocker and QWriteLocker.

See the example File System Example for a demonstration.

Source: https://doc.qt.io/archives/qt-5.15/qabstractxmlnodemodel.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
FirstChild attribute Any (shared captured text)
InheritNamespaces attribute Any (shared captured text)
NextSibling attribute Any (shared captured text)
NodeCopySetting attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
Parent attribute Any (shared captured text)
PreserveNamespaces attribute Any (shared captured text)
PreviousSibling attribute Any (shared captured text)
SimpleAxis attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
attributes function (self, element: Any) -> tuple X.attributes(element) -> tuple (shared captured text)
baseUri function (self, ni: Any) -> Any X.baseUri(ni) -> PythonQt.QtCore.QUrl (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
compareOrder function (self, ni1: Any, ni2: Any) -> Any X.compareOrder(ni1, ni2) (shared captured text)
createIndex function (self, data: Any, additionalData: Any) -> Any X.createIndex(data, additionalData) -> PythonQt.QtXmlPatterns.QXmlNodeModelIndex (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
documentUri function (self, ni: Any) -> Any X.documentUri(ni) -> PythonQt.QtCore.QUrl (shared captured text)
elementById function (self, NCName: Any) -> Any X.elementById(NCName) -> PythonQt.QtXmlPatterns.QXmlNodeModelIndex (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
isDeepEqual function (self, ni1: Any, ni2: Any) -> bool X.isDeepEqual(ni1, ni2) -> bool (shared captured text)
kind function (self, ni: Any) -> Any Returns a value indicating the kind of node identified by ni. (shared captured text)
name function (self, ni: Any) -> Any X.name(ni) -> PythonQt.QtXmlPatterns.QXmlName (shared captured text)
namespaceBindings function (self, n: Any) -> tuple X.namespaceBindings(n) -> tuple (shared captured text)
namespaceForPrefix function (self, ni: Any, prefix: Any) -> int X.namespaceForPrefix(ni, prefix) -> int (shared captured text)
nextFromSimpleAxis function (self, axis: Any, origin: Any) -> Any X.nextFromSimpleAxis(axis, origin) -> PythonQt.QtXmlPatterns.QXmlNodeModelIndex (shared captured text)
nodesByIdref function (self, NCName: Any) -> tuple X.nodesByIdref(NCName) -> tuple (shared captured text)
root function (self, n: Any) -> Any X.root(n) -> PythonQt.QtXmlPatterns.QXmlNodeModelIndex (shared captured text)
sourceLocation function (self, index: Any) -> Any X.sourceLocation(index) -> PythonQt.QtXmlPatterns.QSourceLocation (shared captured text)
stringValue function (self, n: Any) -> str X.stringValue(n) -> str (shared captured text)
typedValue function (self, n: Any) -> Any X.typedValue(n) -> object (shared captured text)