The PySide.QtXmlPatterns.QAbstractXmlNodeModel class is an abstract base class for modeling non-XML data to look like XML for PySide.QtXmlPatterns.QXmlQuery .
The PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QAbstractXmlNodeModel is meant to be accessed by the QtXmlPatterns 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.
PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QXmlQuery and calls PySide.QtXmlPatterns.QXmlQuery.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 PySide.QtXmlPatterns.QAbstractXmlNodeModel . Its constructor is called with the name pool obtained from our PySide.QtXmlPatterns.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 PySide.QtXmlPatterns.QAbstractXmlNodeModel.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 PySide.QtXmlPatterns.QXmlQuery evaluation functions. The application uses QXmlQuery::evaluateTo( PySide.QtXmlPatterns.QAbstractXmlReceiver *), because it then uses a serializer to out the query result as XML to stdout . We could have used QXmlQuery::evaluateTo( PySide.QtXmlPatterns.QXmlResultItems *) to get a list of result items, or QXmlQuery::evaluateTo( PySide.QtCore.QStringList *) if the query evaluated to a sequence of xs:string values.
During query execution, the engine iterates over the node model using PySide.QtXmlPatterns.QAbstractXmlNodeModel.nextFromSimpleAxis() to get the index of the next node to be visited. The engine can get the name of a node by calling PySide.QtXmlPatterns.QAbstractXmlNodeModel.name() with the node’s index . PySide.QtXmlPatterns.QAbstractXmlNodeModel.stringValue() , PySide.QtXmlPatterns.QAbstractXmlNodeModel.baseUri() , PySide.QtXmlPatterns.QAbstractXmlNodeModel.documentUri() and PySide.QtXmlPatterns.QAbstractXmlNodeModel.kind() are also called as needed with a node index .
The example demonstrates the standard pattern for using a subclass of PySide.QtXmlPatterns.QAbstractXmlNodeModel in combination with PySide.QtXmlPatterns.QXmlQuery to perform an XQuery .
Because the XPath Data Model interface presented by PySide.QtXmlPatterns.QAbstractXmlNodeModel allows PySide.QtXmlPatterns.QXmlQuery to operate on non-XML data as if it were XML, implementing subclasses of PySide.QtXmlPatterns.QAbstractXmlNodeModel can involve a significant amount of work. The QSimpleXmlNodeModel class is provided to simplify the implementation for many common use cases.
Because the node model can be accessed concurrently by threads in the QtXmlPatterns module, subclasses of PySide.QtXmlPatterns.QAbstractXmlNodeModel must be written to be thread-safe . Classes that simplify implementing thread-safety include PySide.QtCore.QReadLocker and PySide.QtCore.QWriteLocker .
See the example File System Example for a demonstration.
Default constructor.
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 |
Controls how nodes are copied with copyNodeTo.
Constant | Description |
---|---|
QAbstractXmlNodeModel.InheritNamespaces | Copies the node with the copy-namespaces setting being inherit. If not set, no-inherit is assumed. |
QAbstractXmlNodeModel.PreserveNamespaces | Copies the node with the copy-namespaces settings being preserve. If not set, no-preserve is assumed. |
Parameters: | element – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: |
Returns the attributes of element . The caller guarantees that element is an element in this node model.
Parameters: | ni – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtCore.QUrl |
Returns the base URI for the node whose index is n . The caller guarantees that n is not null and 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-uri accessor, which returns a base URI according to the following:
The implementation guarantees to return a valid PySide.QtCore.QUrl , or a default constructed PySide.QtCore.QUrl . If a node has no base URI, as in the case where a comment has no parent, a default constructed PySide.QtCore.QUrl is returned.
See also
Parameters: | |
---|---|
Return type: |
This function returns the relative document order for the nodes indexed by ni1 and ni2 . It is used for the Is operator and for sorting nodes in document order.
The caller guarantees that ni1 and ni2 are not null and that both identify nodes in this node model.
If ni1 is identical to ni2 , QXmlNodeModelIndex.Is is returned. If ni1 precedes ni2 in document order, QXmlNodeModelIndex.Precedes is returned. If ni1 follows ni2 in document order, QXmlNodeModelIndex.Follows is returned.
See also
Parameters: |
|
---|---|
Return type: |
Creates a node index with pointer and additionalData as its internal data.
What pointer and additionalData is, is not constrained.
Parameters: |
|
---|---|
Return type: |
This is an overloaded function.
Creates a PySide.QtXmlPatterns.QXmlNodeModelIndex containing data and additionalData .
Parameters: | data – PySide.QtCore.qint64 |
---|---|
Return type: | PySide.QtXmlPatterns.QXmlNodeModelIndex |
Creates a node index with data as its internal data. data is not constrained.
Parameters: | ni – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtCore.QUrl |
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 the http:// 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 n is not null and that it belongs to this PySide.QtXmlPatterns.QAbstractXmlNodeModel .
This function maps to the dm:document-uri accessor, which returns a document URI according to the following:
See also
QUrl.isValid() QUrl.isRelative()
Parameters: | NCName – PySide.QtXmlPatterns.QXmlName |
---|---|
Return type: | PySide.QtXmlPatterns.QXmlNodeModelIndex |
Returns the index of the element identified as id . XQuery ‘s id() function calls this function.
The node index returned will be the element node whose value is of type ID and equals id , or it will be the element node that has an attribute whose typed value is of type ID and equals id . If there is no such element, a default constructed PySide.QtXmlPatterns.QXmlNodeModelIndex instance 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 be ID . However, the reserved name xml:id is sufficient.
In id , the namespace URI and the prefix are undefined, and the local name is the ID that should be looked up.
See also
Parameters: | |
---|---|
Return type: | PySide.QtCore.bool |
Determines whether ni1 is deep equal to ni2 .
PySide.QtXmlPatterns.QAbstractXmlNodeModel.isDeepEqual() is defined as evaluating the expression fn:deep-equal($n1, $n2) where $n1 is ni1 and $n1 is ni2 . This function is associative, meaning the same value is returned regardless of if PySide.QtXmlPatterns.QAbstractXmlNodeModel.isDeepEqual() is invoked with ni1 as first argument or second. It is guaranteed that ni1 and ni2 are nodes, as opposed to the definition of fn:deep-equal() .
Returns true if ni1 is deep-equal to ni2 , otherwise false
See also
Parameters: | n – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtCore.bool |
Parameters: | ni – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtXmlPatterns.QXmlNodeModelIndex.NodeKind |
Returns a value indicating the kind of node identified by ni . The caller guarantees that ni is not null and that it identifies a node in this node model. This function maps to the dm:node-kind() accessor.
See also
Parameters: | ni – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtXmlPatterns.QXmlName |
Returns the name of ni . The caller guarantees that ni is not null and that it belongs to this PySide.QtXmlPatterns.QAbstractXmlNodeModel .
If a node does not have a name, e.g., comment nodes, a null PySide.QtXmlPatterns.QXmlName is returned. QXmlNames must be created with the instance of PySide.QtXmlPatterns.QXmlQuery that is being used for evaluating queries using this PySide.QtXmlPatterns.QAbstractXmlNodeModel .
This function maps to the dm:node-name() accessor.
If ni is a processing instruction, a PySide.QtXmlPatterns.QXmlName is returned with the local name as the target name and the namespace URI and prefix both empty.
See also
Parameters: | n – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: |
Returns the in-scope namespaces of n . The caller guarantees that n is not null and that it belongs to this PySide.QtXmlPatterns.QAbstractXmlNodeModel .
This function corresponds to the dm:namespace-nodes accessor.
The returned vector of namespace declarations includes namespaces of the ancestors of n .
The caller guarantees that n is an Element that belongs to this PySide.QtXmlPatterns.QAbstractXmlNodeModel .
Parameters: |
|
---|---|
Return type: | PySide.QtCore.short |
Parameters: | |
---|---|
Return type: |
When QtXmlPatterns evaluate path expressions, it emulate them through a combination of calls with QSimpleXmlNodeModel.SimpleAxis values. Therefore, the implementation of this function must return the node, if any, that appears on the axis emanating from the origin .
If no such node is available, a default constructed PySide.QtXmlPatterns.QXmlNodeModelIndex is returned.
QSimpleXmlNodeModel eliminates the need to handle redundant corner cases by guaranteeing that it will never ask for:
A typical implementation performs a switch on the value of axis :
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
}
}
Parameters: | NCName – PySide.QtXmlPatterns.QXmlName |
---|---|
Return type: |
Returns the elements and/or attributes that have an IDREF value equal to idref . XQuery ‘s idref() 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 type IDREF . Elements must be typed as xs:IDREF or xs:IDREFS , or, in the case of attributes, as IDREF or IDREFS in the schema.
In idref , the namespace URI and the prefix are undefined, and the local name is the ID that should be looked up.
See also
Parameters: | n – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtXmlPatterns.QXmlNodeModelIndex |
Returns the root node of the tree that contains the node whose index is n . The caller guarantees that n is not null and that it identifies a node in this node model.
If n identifies a node that is a direct child of the root, parent() would return the same PySide.QtXmlPatterns.QXmlNodeModelIndex returned by this function.
Parameters: |
---|
Parameters: | index – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | PySide.QtXmlPatterns.QSourceLocation |
Returns the source location for the object with the given index or a default constructed PySide.QtXmlPatterns.QSourceLocation in case no location information is available.
Parameters: | n – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
Return type: | unicode |
Returns the string value for node n .
The caller guarantees that n is not null and that it belong to this PySide.QtXmlPatterns.QAbstractXmlNodeModel instance.
This function maps to the dm:string-value() accessor, which the specification completely specifies. Here’s a summary:
See also
Parameters: | n – PySide.QtXmlPatterns.QXmlNodeModelIndex |
---|---|
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 node is either an element or an attribute. The implementor guarantees that the returned PySide.QtCore.QVariant has a value which is supported in XQuery . It cannot be an arbitrary PySide.QtCore.QVariant value. The implementor also guarantees that PySide.QtXmlPatterns.QAbstractXmlNodeModel.stringValue() returns a lexical representation of PySide.QtXmlPatterns.QAbstractXmlNodeModel.typedValue() (this is guaranteed by QSimpleXmlNodeModel.stringValue() ).
If the return PySide.QtCore.QVariant is a default constructed variant, it signals that node has no typed value.