Giovanni
Giovanni

Reputation: 21

Parent item with xpath in qml/qt

How do I get the parent for the current item in an xpath query in qml/qt ? fn:parent() isn't implemented, ../ yes but doesn't work for me. Seems like a focus problem.

xml example:

<groups>
  <group id="A">
    <item>bla</item>
    <item>blah</item>
  </group>
  <group id="B">
    <item>bla</item>
    <item>blah</item>
  </group>
  <group id="C">
    <item>bla</item>
    <item>blah</item>
  </group>
</groups>

The following XmlRole returns an empty string:

XmlListModel {
  query: "/groups/group/item"
  XmlRole { name: "group_id"; query: "../@id/string()" }
}

Upvotes: 2

Views: 1289

Answers (4)

Allen B
Allen B

Reputation: 25

It does appear that you are missing the source tag.... the location of the xml file. Although the other discussion regarding the query syntax is still relevant.

Upvotes: 0

VirusVzla
VirusVzla

Reputation: 11

Where is the source tag that tell the model from where take the query? You need to declare the source, your ID property is outside of node. And have to declare the namespace used(for example http://www.w3.org/2005/Atom). So i think that your code have to be like:

XmlListModel {
  query: "/groups/group/"
  namespaceDeclarations: "declare namespace group=\"http://www.w3.org/2005/Atom/\";"
  source: "url of your xml file"
  XmlRole { name: "group_id"; query: "../@id/string()" }
}

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

../@id/string()

This is invalid syntax in XPath 1.0.

Are you sure you are using an XPath 2.0 engine?

In XPath 1.0 use:

string(../@id)

Or, if an expression that selects a node-set is required, then simply use:

../@id

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Try this XPath:

../@id

The string(object?) function converts an object to a string as follows:

  • A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.

Reference: http://www.w3.org/TR/xpath/#section-String-Functions

Upvotes: 0

Related Questions