Question

Using Qt 4.8.4 on Windows platform and trying to use QXmlQuery without any luck. Obviously researched this topic without finding a solution.

Here is the xml file

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <a>abc</a>
  <a>def</a>
  <a>123</a>
</root>

The code looks like this;

QFile temp("C:/Temp/data.xml");
bool opened = temp.open(QIODevice::ReadOnly | QIODevice::Text);

QXmlQuery q;
q.bindVariable("file", &temp);
q.setQuery("declare variable $file external;doc($file)//root");
bool valid = q.isValid();

QStringList items;
q.evaluateTo(&items);
int len = items.size();

File is opened successfully, QXmlQuery is valid but QStringList contains 0 items. Why does not the query return a result?

Was it helpful?

Solution

Changing the XQuery from

q.setQuery("declare variable $file external;doc($file)//root");

to

q.setQuery("declare variable $file external;doc($file)//root/string()"); 

solved the problem.

The docs did actually state "The query must evaluate to a sequence of xs:string values. If the query does not evaluate to a sequence of strings, the values can often be converted by adding a call to string() at the end of the XQuery."

http://qt-project.org/doc/qt-4.8/qxmlquery.html#evaluateTo-3

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top