2

I'm attempting to use QXmlQuery to execute an XQuery expression against a document with a declared default namespace.

For discussion:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://namespace.com/ns1">
    <node1 attr1="hi"/>
</root>

Now, I have the following to open and query against the document:

QFile temp("my.xml");
temp.open(QIODevice::ReadOnly | QIODevice::Text);

QXmlQuery query;
query.setFocus(&temp);

QXmlResultItems items;
query.setQuery("/root");

query.evaluateTo(&items);

In running this, 'items' never has data in it, since the document is namespaced. Of course, if I remove the default namespace declaration, 'items' does have the correct data, but I don't have that luxury.

I've tried changing the query to: "/prefix:root", and Qt barks a warning like:

No namespace binding exists for the prefix prefix in prefix:root

So namespace binding does exist! But where? I see QXmlNamePool, but it has no mutator methods. I can create a QXmlName with the pool from the query ala:

QXmlName name(query.namePool(), "prefix", "http://namespace.com/ns1");

But it doesn't change anything. I'm at a loss, other toolkits I have used have simple methods to bind prefixes to namespace URIs.

1 Answer 1

4

I believe if you would change your query to

...
QXmlResultItems items;
query.setQuery("declare default element namespace \"http://namespace.com/ns1\"; /root");
...

it should return the data.

hope this helps, regards

Sign up to request clarification or add additional context in comments.

5 Comments

That works! That's absolutely horrible API on Qt's part, but it works. Thanks!
@Nick Veys: This is an XQuery expression, not XPath.
@Alejandro: Is there a better class to be using then? It claims XPath support, as it's a subset of XQuery.
@Nick Veys: I would use XQuery/XPath 2.0 over any XPath 1.0 engine
I can't disagree, however requirements tend to trump preference. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.