QAbstractXmlNodeModel¶
The
QAbstractXmlNodeModelclass is an abstract base class for modeling non-XML data to look like XML forQXmlQuery. More…

Synopsis¶
Functions¶
def
createIndex(data)def
createIndex(data, additionalData)def
createIndex(pointer[, additionalData=0])def
sourceLocation(index)
Virtual functions¶
def
attributes(element)def
baseUri(ni)def
compareOrder(ni1, ni2)def
documentUri(ni)def
elementById(NCName)def
isDeepEqual(ni1, ni2)def
kind(ni)def
name(ni)def
namespaceBindings(n)def
namespaceForPrefix(ni, prefix)def
nextFromSimpleAxis(axis, origin)def
nodesByIdref(NCName)def
root(n)def
sendNamespaces(n, receiver)def
stringValue(n)def
typedValue(n)
Detailed Description¶
The
QAbstractXmlNodeModelspecifies 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
QAbstractXmlNodeModelis 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¶
QAbstractXmlNodeModelbridges the gap between the arbitrary structure of the non-XML data to be queried and the well-defined structure of XML data understood byQXmlQuery.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 filequeryFile. We write a custom subclass ofQAbstractXmlNodeModel(ChemistryNodeModel) that readschemistryDataand builds a data structure, perhaps composed of objects of our own classesmoleculeandatom. 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
QXmlQueryand callssetQuery()to readqueryFilecontaining the XQuery we want to run. Then it creates an instance of our custom node model class,ChemistryNodeModel, which is a subclass ofQAbstractXmlNodeModel. Its constructor is called with thename poolobtained from ourQXmlQuery, and with thechemistryFilecontaining the structure of molecules and atoms to be queried. Thename poolis required because our custom node model has the member functionname(), which returns thenameof any node in the model. Thequeryand the custom node model must use the same name pool for constructing thesenames. The constructor would then readchemistryFileand build the custom node model structure.To connect the
queryto 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, anindexfor the desired starting node is retrieved by callingcreateIndex(). Then the index is bound to a variable name, in this casequeryRoot, by passing the name and the index tobindVariable(). The query can then use a variable reference$queryRootto refer to the starting node. Note that if thequeryuses multiple variable references, a call tobindVariable()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
QXmlQueryevaluation functions. The application usesevaluateTo(QAbstractXmlReceiver*), because it then uses aserializerto out the query result as XML tostdout. We could have usedevaluateTo(QXmlResultItems*) to get a list of result items, orevaluateTo(QStringList*) if the query evaluated to a sequence ofxs:stringvalues.During query execution, the engine iterates over the node model using
nextFromSimpleAxis()to get theindexof the next node to be visited. The engine can get the name of a node by callingname()with the node’sindex.stringValue(),baseUri(),documentUri()andkind()are also called as needed with a nodeindex.The example demonstrates the standard pattern for using a subclass of
QAbstractXmlNodeModelin combination withQXmlQueryto perform an XQuery .
Instantiate a subclass of
QAbstractXmlNodeModelorQSimpleXmlNodeModel;Retrieve a
QXmlNodeModelIndexfor the node in the model where theQXmlQueryshould start the query;Use
bindVariable()to bind theQXmlNodeModelIndexto$variable name;Call one of the
QXmlQueryevaluation functions to run the query.
Subclassing¶
Because the XPath Data Model interface presented by
QAbstractXmlNodeModelallowsQXmlQueryto operate on non-XML data as if it were XML, implementing subclasses ofQAbstractXmlNodeModelcan involve a significant amount of work. TheQSimpleXmlNodeModelclass 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
QAbstractXmlNodeModelmust be written to bethread-safe. Classes that simplify implementing thread-safety includeQReadLockerandQWriteLocker.See the example File System Example for a demonstration.
- class PySide2.QtXmlPatterns.QAbstractXmlNodeModel¶
Default constructor.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.SimpleAxis¶
Four axes that each contain one node only.
Constant
Description
QAbstractXmlNodeModel.Parent
The parent of the context node
QAbstractXmlNodeModel.FirstChild
The first child of the context node
QAbstractXmlNodeModel.PreviousSibling
The previous child of the context node
QAbstractXmlNodeModel.NextSibling
The next child of the context node
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.NodeCopySetting¶
Controls how nodes are copied with copyNodeTo.
Constant
Description
QAbstractXmlNodeModel.InheritNamespaces
Copies the node with the
copy-namespacessetting beinginherit. If not set,no-inheritis assumed.QAbstractXmlNodeModel.PreserveNamespaces
Copies the node with the
copy-namespacessettings beingpreserve. If not set,no-preserveis assumed.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.attributes(element)¶
- Parameters:
element –
PySide2.QtXmlPatterns.QXmlNodeModelIndex- Return type:
Returns the attributes of
element. The caller guarantees thatelementis an element in this node model.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.baseUri(ni)¶
- Parameters:
- Return type:
Returns the base URI for the node whose index is
n. The caller guarantees thatnis notnulland that it belongs to a node in this node model.The base URI of a node can be extracted using the
fn:base-uri()function. The base URI is typically used for resolving relative URIs that appear in the node or its children. It is conformant to just return the document URI, although that might not properly reflect the underlying data.This function maps to the
dm:base-uriaccessor, which returns a base URI according to the following:For document nodes, the base URI and the document URI are the same.
For elements, the base URI is the URI appearing in the element’s
xml:baseattribute, if present, or it is resolved to the parent element’s base URI.Namespace nodes have no base URI.
The base URI for a processing instruction, comment, attribute, or text node is the base URI of the node’s parent element.
The implementation guarantees to return a valid
QUrl, or a default constructedQUrl. If a node has no base URI, as in the case where a comment has no parent, a default constructedQUrlis returned.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.compareOrder(ni1, ni2)¶
- Parameters:
- Return type:
This function returns the relative document order for the nodes indexed by
ni1andni2. It is used for theIsoperator and for sorting nodes in document order.The caller guarantees that
ni1andni2are notnulland that both identify nodes in this node model.If
ni1is identical toni2,Isis returned. Ifni1precedesni2in document order,Precedesis returned. Ifni1followsni2in document order,Followsis returned.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.createIndex(data)¶
- Parameters:
data – int
- Return type:
Creates a node index with
dataas its internal data.datais not constrained.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.createIndex(data, additionalData)
- Parameters:
data – int
additionalData – int
- Return type:
This is an overloaded function.
Creates a
QXmlNodeModelIndexcontainingdataandadditionalData.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.createIndex(pointer[, additionalData=0])
- Parameters:
pointer –
voidadditionalData – int
- Return type:
Creates a node index with
pointerandadditionalDataas its internal data.What
pointerandadditionalDatais, is not constrained.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.documentUri(ni)¶
- Parameters:
- Return type:
Returns the document URI of
n. The document URI identifies the resource which is the document. For example, the document could be a regular file, e.g.,file:/, or it could be thehttp://URL of the location of a file. The document URI is used for resolving URIs and to simply know where the document is.If the node model maps to a URI in a natural way, return that URI. Otherwise, return the company or product URI. The document URI can be any URI as long as its valid and absolute.
The caller guarantees that
nis notnulland that it belongs to thisQAbstractXmlNodeModel.This function maps to the
dm:document-uriaccessor, which returns a document URI according to the following:If
nis a document node, return an absoluteQUrlcontaining the document URI, or a default constructedQUrl. The latter signals that no document URI is available for the document node.For all other nodes, return a default constructed
QUrl.
See also
XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.4 document-uri Accessor
isValid()isRelative()
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.elementById(NCName)¶
- Parameters:
NCName –
PySide2.QtXmlPatterns.QXmlName- Return type:
Returns the index of the element identified as
id. XQuery ‘sid()function calls this function.The node index returned will be the element node whose value is of type
IDand equalsid, or it will be the element node that has an attribute whose typed value is of typeIDand equalsid. If there is no such element, a default constructedQXmlNodeModelIndexinstance is returned. The implementor guarantees that if the returned node index is not null, it identifies an element.It is not sufficient for an attribute or element to merely be called
id. Its value type must also beID. However, the reserved namexml:idis sufficient.In
id, thenamespace URIand theprefixare undefined, and thelocal nameis the ID that should be looked up.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.isDeepEqual(ni1, ni2)¶
- Parameters:
- Return type:
bool
Determines whether
ni1is deep equal toni2.is defined as evaluating the expression
fn:deep-equal($n1, $n2)where$n1isni1and$n1isni2. This function is associative, meaning the same value is returned regardless of if is invoked withni1as first argument or second. It is guaranteed thatni1andni2are nodes, as opposed to the definition offn:deep-equal().Returns true if
ni1is deep-equal toni2, otherwise falseSee also
1 fn:deep-equal
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.kind(ni)¶
- Parameters:
- Return type:
Returns a value indicating the kind of node identified by
ni. The caller guarantees thatniis not null and that it identifies a node in this node model. This function maps to thedm:node-kind()accessor.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.name(ni)¶
- Parameters:
- Return type:
Returns the name of
ni. The caller guarantees thatniis notnulland that it belongs to thisQAbstractXmlNodeModel.If a node does not have a name, e.g., comment nodes, a null
QXmlNameis returned. QXmlNames must be created with the instance ofQXmlQuerythat is being used for evaluating queries using thisQAbstractXmlNodeModel.This function maps to the
dm:node-name()accessor.If
niis a processing instruction, aQXmlNameis returned with the local name as the target name and the namespace URI and prefix both empty.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.namespaceBindings(n)¶
- Parameters:
- Return type:
Returns the in-scope namespaces of
n. The caller guarantees thatnis notnulland that it belongs to thisQAbstractXmlNodeModel.This function corresponds to the
dm:namespace-nodesaccessor.The returned vector of namespace declarations includes namespaces of the ancestors of
n.The caller guarantees that
nis an Element that belongs to thisQAbstractXmlNodeModel.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.namespaceForPrefix(ni, prefix)¶
- Parameters:
prefix –
PrefixCode
- Return type:
NamespaceCode
Returns the namespace URI on
nithat corresponds toprefix.If
prefixis StandardPrefixes::empty, the namespace URI for the default namespace is returned.The default implementation use
namespaceBindings(), in a straight forward manner.If no namespace exists for
prefix, NamespaceResolver::NoBinding is returned.The caller guarantees to only call this function for element nodes.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.nextFromSimpleAxis(axis, origin)¶
- Parameters:
axis –
SimpleAxis
- Return type:
When Qt XML Patterns evaluate path expressions, it emulate them through a combination of calls with
SimpleAxisvalues. Therefore, the implementation of this function must return the node, if any, that appears on theaxisemanating from theorigin.If no such node is available, a default constructed
QXmlNodeModelIndexis returned.QSimpleXmlNodeModeleliminates the need to handle redundant corner cases by guaranteeing that it will never ask for:Children or siblings for attributes.
Children for comments, processing instructions, and text nodes.
Siblings or parents for document nodes.
A typical implementation performs a
switchon the value ofaxis:QXmlNodeModelIndex MyTreeModel::nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const { // Convert the QXmlNodeModelIndex to a value that is specific to what we represent. const MyValue value = toMyValue(ni); switch(axis) { case Parent: return toNodeIndex(value.parent()); case FirstChild: case PreviousSibling: case NextSibling: // and so on } }
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.nodesByIdref(NCName)¶
- Parameters:
NCName –
PySide2.QtXmlPatterns.QXmlName- Return type:
Returns the elements and/or attributes that have an
IDREFvalue equal toidref. XQuery ‘sidref()function calls this function.The implementor guarantees that the nodes identified by the returned indexes are elements or attributes.
It is not sufficient for an attribute or element to merely be called
idref. It must also be of typeIDREF. Elements must be typed asxs:IDREForxs:IDREFS, or, in the case of attributes, asIDREForIDREFSin the schema.In
idref, thenamespace URIand theprefixare undefined, and thelocal nameis the ID that should be looked up.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.root(n)¶
- Parameters:
- Return type:
Returns the root node of the tree that contains the node whose index is
n. The caller guarantees thatnis notnulland that it identifies a node in this node model.If
nidentifies a node that is a direct child of the root, parent() would return the sameQXmlNodeModelIndexreturned by this function.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.sendNamespaces(n, receiver)¶
- Parameters:
Sends the namespaces declared on
ntoreceiver.As a consequence, no namespaces are sent unless this node is an element and has namespaces declared.
The caller guarantees that
nis notnulland that it belongs to thisQAbstractXmlNodeModelinstance.Note that it is not the namespaces that are in scope on
n, but only the namespaces that are specifically declared onn.receiveris the receiver that this node is supposed to send its namespaces to. This is guaranteed by the caller to be a valid pointer.nis the index of the node whose namespaces are to be sent.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.sourceLocation(index)¶
- Parameters:
- Return type:
Returns the source location for the object with the given
indexor a default constructedQSourceLocationin case no location information is available.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.stringValue(n)¶
- Parameters:
- Return type:
str
Returns the string value for node
n.The caller guarantees that
nis notnulland that it belong to thisQAbstractXmlNodeModelinstance.This function maps to the
dm:string-value()accessor, which the specification completely specifies. Here’s a summary:For processing instructions, the string value is the data section(excluding any whitespace appearing between the name and the data).
For text nodes, the string value equals the text node.
For comments, the content of the comment
For elements, the concatenation of all text nodes that are descendants. Note, this is not only the children, but the childrens’ childrens’ text nodes, and so forth.
For document nodes, the concatenation of all text nodes in the document.
- PySide2.QtXmlPatterns.QAbstractXmlNodeModel.typedValue(n)¶
- Parameters:
- Return type:
object
Returns the typed value for node
node.The typed value is an atomic value, which an element or attribute contains.
The caller guarantees that
nodeis either an element or an attribute. The implementor guarantees that the returnedQVarianthas a value which is supported in XQuery . It cannot be an arbitraryQVariantvalue. The implementor also guarantees thatstringValue()returns a lexical representation of (this is guaranteed bystringValue()).If the return
QVariantis a default constructed variant, it signals thatnodehas no typed value.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.