|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QAbstractXmlNodeModel Class Reference
[QtXmlPatterns module]

The QAbstractXmlNodeModel class provides an abstract interface for representing arbitrary data as XML. More...

     #include <QAbstractXmlNodeModel>

Inherits QSharedData.

Inherited by QPatternist::AccelTree, QPatternist::QObjectNodeModel, and QSimpleXmlNodeModel.

Note: All the functions in this class are reentrant.

This class was introduced in Qt 4.4.

- List of all members, including inherited members

### Public Types

- typedef Ptr
- enum SimpleAxis { Parent, FirstChild, PreviousSibling, NextSibling }

### Public Functions

- QAbstractXmlNodeModel ()
- virtual ~QAbstractXmlNodeModel ()
- virtual QUrl baseUri ( const QXmlNodeModelIndex & n ) const = 0
- virtual QXmlNodeModelIndex::DocumentOrder compareOrder ( const QXmlNodeModelIndex & ni1, const QXmlNodeModelIndex & ni2 ) const = 0
- virtual QUrl documentUri ( const QXmlNodeModelIndex & n ) const = 0
- virtual QXmlNodeModelIndex elementById ( const QXmlName & id ) const = 0
- virtual QXmlNodeModelIndex::NodeKind kind ( const QXmlNodeModelIndex & ni ) const = 0
- virtual QXmlName name ( const QXmlNodeModelIndex & ni ) const = 0
- virtual QVector<QXmlName> namespaceBindings ( const QXmlNodeModelIndex & n ) const = 0
- virtual QVector<QXmlNodeModelIndex> nodesByIdref ( const QXmlName & idref ) const = 0
- virtual QXmlNodeModelIndex root ( const QXmlNodeModelIndex & n ) const = 0
- virtual QString stringValue ( const QXmlNodeModelIndex & n ) const = 0
- virtual QVariant typedValue ( const QXmlNodeModelIndex & node ) const = 0

### Protected Functions

- virtual QVector<QXmlNodeModelIndex> attributes ( const QXmlNodeModelIndex & element ) const = 0
- QXmlNodeModelIndex createIndex ( qint64 data ) const
- QXmlNodeModelIndex createIndex ( void * pointer, qint64 additionalData = 0 ) const
- QXmlNodeModelIndex createIndex ( qint64 data, qint64 additionalData ) const
- virtual QXmlNodeModelIndex nextFromSimpleAxis ( SimpleAxis axis, const QXmlNodeModelIndex & origin ) const = 0

## Detailed Description

The QAbstractXmlNodeModel class provides an abstract interface for representing arbitrary data as XML.

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

### Usage

Subclassing QAbstractXmlNodeModel is central to using QXmlQuery to perform XQueries. QAbstractXmlNodeModel bridges the gap between the XML world understood by QXmlQuery and the structure of the non-XML data to be queried.

Consider a chemistry application that reads a file containing data representing a structure of atoms and molecules. The application queries this data with an XQuery. We subclass QAbstractXmlNodeModel as ChemistryNodeModel to implement the node model interface. The subclass can read the file containing the chemistry data and build a structure composed of instances of class molecule, which are, in turn, composed of instances of class atom.

       QXmlQuery query;
       QFile queryFile(argv[1]);
       QFile chemistryFile(argv[2]);
       QString moleculeName = argv[3];

       ChemistryNodeModel myNodeModel(query.namePool(),chemistryFile);
       query.setQuery(&queryFile, QUrl::fromLocalFile(queryFile.fileName()));

       qint64 moleculeHash = myNodeModel.hash(moleculeName);
       QXmlNodeModelIndex moleculeIndex = myNodeModel.createIndex(moleculeHash);
       query.bindVariable(QLatin1String("queryRoot"), moleculeIndex);

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

       QXmlSerializerPointer serializer(new QXmlSerializer(query, &out));
       query.evaluateToReceiver(serializer);

The application creates an instance of ChemistryNodeModel, myNodeModel, which QXmlQuery can then use as a delegate for the query engine to use for traversing the molecules and atoms as if they were XML nodes. The ChemistryNodeModel constructor is called with the name pool from the query, which has been loaded from queryFile, and with chemistryFile, which contains the chemistry data to be searched with XQuery. The constructor reads chemistryFile and builds the node model from the data.

The call to QXmlQuery::bindVariable() might be a bit confusing. The query must start searching the data structure at some node. An index is computed for the desired starting node, and the index is bound to the variable name queryRoot, which appears as $queryRoot in the text of the query. Note that a query can use multiple $variables. A call to QXmlQuery::bindVariable() would be required for each one.

The query is executed when the application calls one of QXmlQuery's three evaluation functions. QXmlQuery::evaluateToReceiver() is used in this case, because the application serializes the query result items and outputs them as XML. QXmlQuery::evaluateToResult() could have been be used to get the sequence of result items, and QXmlQuery::evaluateToStringList() could have been be used if the query was known to evaluate 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 any node it visits by calling name() with the node's index. stringValue(), baseUri(), documentUri() and kind() are also called as needed with the index.

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

1. Write an XQuery that identifies a starting node with declare variable $variableName external;
2. Instantiate QXmlQuery and set it to the XQuery;
3. Instantiate a subclass of QAbstractXmlNodeModel of QSimpleXmlNodeModel;
4. Create a QXmlNodeModelIndex for the node where the QXmlQuery should start processing the node model;
5. Use QXmlQuery::bindVariable() to bind the QXmlNodeModelIndex to variableName;
6. Run the query with one of the evaluation functions in QXmlQuery.

### Subclassing

Because the interface presented by QAbstractXmlNodeModel allows the QXmlQuery engine to operate on a non-XML data structure as if it were XML, subclassing QAbstractXmlNodeModel is of central importance, and implementing the subclass can require significant work. QSimpleXmlNodeModel is provided to simplify the subclassing task for many common cases.

See the example Query the File System for a demonstration.

## Member Type Documentation

### typedef QAbstractXmlNodeModel::Ptr

A smart pointer to a QAbstractXmlNodeModel instance.

### enum QAbstractXmlNodeModel::SimpleAxis

Four axes that only contain one node each.

Constant | Value | Description
--- | --- | ---
`QAbstractXmlNodeModel::Parent` | `0` | The parent of the context node
`QAbstractXmlNodeModel::FirstChild` | `1` | The first child of the context node
`QAbstractXmlNodeModel::PreviousSibling` | `2` | The previous sibling of the context node
`QAbstractXmlNodeModel::NextSibling` | `3` | The next sibling of the context node

## Member Function Documentation

### QAbstractXmlNodeModel::QAbstractXmlNodeModel ()

Default constructor.

### QAbstractXmlNodeModel::~QAbstractXmlNodeModel () `[virtual]`

Destructor.

### QVector<QXmlNodeModelIndex> QAbstractXmlNodeModel::attributes ( const QXmlNodeModelIndex & element ) const `[pure virtual protected]`

Returns the attributes that element element has.

The caller guarantees that element is always an element.

### QUrl QAbstractXmlNodeModel::baseUri ( const QXmlNodeModelIndex & n ) const `[pure virtual]`

Returns the base URI for n.

The caller guarantees that n is not `null` and that it belongs to this QAbstractXmlNodeModel instance.

The base URI of nodes can be extracted using the `fn:base-uri()` function, and is typically used for resolving relative URIs appearing directly, or as child of the node. 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 the specification completely specifies. Here's a summary:

- For document nodes, the base URI equals the document URI.
- For elements, the base URI is the URI appearing in its `xml:base` attribute, if any, recursively resolved to the parent's base URI.
- Namespaces nodes have no base URI.
- The base URI for processing instructions, comments, attributes, and text nodes is the base URI of its parent element.

The implementation guarantees to return a valid QUrl instance, or a default constructed QUrl instance. If a node has no base URI, such as in the case of that a comment has no parent, a default constructed QUrl is returned.

See also XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.2 base-uri Accessor.

### QXmlNodeModelIndex::DocumentOrder QAbstractXmlNodeModel::compareOrder ( const QXmlNodeModelIndex & ni1, const QXmlNodeModelIndex & ni2 ) const `[pure virtual]`

Returns the relative document order between ni1 and ni2.

This is used for the `is` operator, and to sort nodes in document order.

The caller guarantees that ni1 and ni2 are not `null` and that they belong to this QAbstractXmlNodeModel instance.

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.

### QXmlNodeModelIndex QAbstractXmlNodeModel::createIndex ( qint64 data ) const `[protected]`

Creates a node index with data as its internal data.

What data is, is not constrained.

### QXmlNodeModelIndex QAbstractXmlNodeModel::createIndex ( void * pointer, qint64 additionalData = 0 ) const `[protected]`

This is an overloaded member function, provided for convenience.

Creates a node index with pointer and additionalData as its internal data.

What pointer and additionalData is, is not constrained.

### QXmlNodeModelIndex QAbstractXmlNodeModel::createIndex ( qint64 data, qint64 additionalData ) const `[protected]`

This is an overloaded member function, provided for convenience.

Creates a QXmlNodeModelIndex that houses data and additionalData.

### QUrl QAbstractXmlNodeModel::documentUri ( const QXmlNodeModelIndex & n ) const `[pure virtual]`

Returns the document URI of n.

The document URI identifies the resource which is the document. For instance, if the document would be a regular file, it would perhaps be the `file:/` or `http://` URL of the location. The document URI is used for resolving URIs and to simply know where the document is.

If the data model maps to a URI in a natural way -- use that. Otherwise return the company or product's URI. It 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 QAbstractXmlNodeModel instance.

This function maps to the `dm:document-uri` accessor, which the specification completely specifies. Here's a summary:

- If n is a document node, return an absolute, valid QUrl containing the document URI, or a default constructed QUrl. The latter signals that no document URI is available for the document node. - For all other kinds of nodes, return a default constructed QUrl.

See also XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.4 document-uri Accessor, QUrl::isValid(), and QUrl::isRelative().

### QXmlNodeModelIndex QAbstractXmlNodeModel::elementById ( const QXmlName & id ) const `[pure virtual]`

Returns the element that has id id. XQuery's `id()` function ends up calling this.

Returns the element node whose typed value is of type `ID` and equals id, or the element that has an attribute whose typed value is of type `ID` and equals id. If there is no such element, a default constructed QXmlNodeModelIndex instance is returned. The implementor guarantees the returned node, if not `null`, is an element.

In effect it's not sufficient for an attribute or element to merely be called `id` in order to be of type `ID`. However, the reserved name `xml:id` is always recognized as so.

In id the namespace URI and the prefix is undefined, while the local name is the ID that should be looked up.

See also XQuery 1.0 and XPath 2.0 Functions and Operators, 15.5.2 fn:id.

### QXmlNodeModelIndex::NodeKind QAbstractXmlNodeModel::kind ( const QXmlNodeModelIndex & ni ) const `[pure virtual]`

Determines what node kind ni is. Simply, whether ni is an element or comment for instance.

The caller guarantees that ni is not `null` and that it belongs to this QAbstractXmlNodeModel instance.

This function maps to the `dm:node-kind()` accessor.

See also XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.10 node-kind Accessor.

### QXmlName QAbstractXmlNodeModel::name ( const QXmlNodeModelIndex & ni ) const `[pure virtual]`

Returns the name of ni.

The caller guarantees that ni is not `null` and that it belong to this QAbstractXmlNodeModel instance.

If a node does not have a name, such as a comment code, a null QXmlName is returned. QXmlName instances must be created with the same QXmlQuery instance that is used for evaluating queries using this QAbstractXmlNodeModel instance.

This function maps to the `dm:node-name()` accessor.

As specified, if ni is a processing instruction, a QXmlName is returned where the local name is the target name and the namespace URI and prefix is empty.

See also XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.11 node-name Accessor and QXmlName.

### QVector<QXmlName> QAbstractXmlNodeModel::namespaceBindings ( const QXmlNodeModelIndex & n ) const `[pure virtual]`

Returns the in-scope namespaces of n.

The caller guarantees that n is not `null` and that it belong to this QAbstractXmlNodeModel instance.

This corresponds to the `dm:namespace-nodes` accessor.

Note: This is not only the namespace declarations that appear on this element, but takes also into account namespace bindings of the ancestors.

The caller guarantees that n is an Element and belongs to this QAbstractXmlNodeModel instance.

### QXmlNodeModelIndex QAbstractXmlNodeModel::nextFromSimpleAxis ( SimpleAxis axis, const QXmlNodeModelIndex & origin ) const `[pure virtual protected]`

When Patternist calls iterate(), QSimpleXmlNodeModel create iterators that calls nextFromSimpleAxis() and "emulates" real XPath axes using QSimpleXmlNodeModel::SimpleAxis. Therefore, the implementation of this function should return the node, if any, that appear on axis axis, from origin.

If no such node is available, a default constructed QXmlNodeModelIndex is returned.

QSimpleXmlNodeModel removes 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 does a switch over 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
       }
     }

### QVector<QXmlNodeModelIndex> QAbstractXmlNodeModel::nodesByIdref ( const QXmlName & idref ) const `[pure virtual]`

Returns the elements and/or attributes that an `IDREF` value equal to idref. XQuery's `idref()` function ends up calling this.

The implementor guarantees the returned nodes are elements or attributes.

In effect it's not sufficient for an attribute or element to merely be called `idref` in order to be of type `IDREF`. Elements must be typed as `xs:IDREF` or `xs:IDREFS`, or in the case of attributes only, as `IDREF` or `IDREFS` in the schema.

In id the namespace URI and the prefix is undefined, while the local name is the ID that should be looked up.

See also XQuery 1.0 and XPath 2.0 Functions and Operators, 15.5.3 fn:idref.

### QXmlNodeModelIndex QAbstractXmlNodeModel::root ( const QXmlNodeModelIndex & n ) const `[pure virtual]`

Returns the root node of the tree that n is part of. This is typically a document node. This function is used among other things for `fn:root()` and the root expression, such as seen in the expression `/html`.

The caller guarantees that n is not `null` and that it belong to this QAbstractXmlNodeModel instance.

If n is a direct child of the QXmlNodeModelIndex returned from this function, parent() would return the same QXmlNodeModelIndex.

### QString QAbstractXmlNodeModel::stringValue ( const QXmlNodeModelIndex & n ) const `[pure virtual]`

Returns the string value for node n.

The caller guarantees that n is not `null` and that it belong to this QAbstractXmlNodeModel instance.

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.

See also XQuery 1.0 and XPath 2.0 Data Model (XDM), 5.13 string-value Accessor.

### QVariant QAbstractXmlNodeModel::typedValue ( const QXmlNodeModelIndex & node ) const `[pure virtual]`

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 QVariant has a value which is supported in XQuery. It cannot be an arbitrary QVariant value. The implementor also guarantees that stringValue() returns a lexical representation of typedValue()(this is guaranteed by QSimpleXmlNodeModel::stringValue()).

If the return QVariant is a default constructed variant, it signals that node has no typed value.