使用QXmlQuery(Qt XQuery)时,如何绑定名称空间前缀? [英] How does one bind namespace prefixes when using QXmlQuery (Qt XQuery)?
问题描述
我正在尝试使用QXmlQuery对具有声明的默认名称空间的文档执行XQuery表达式.
I'm attempting to use QXmlQuery to execute an XQuery expression against a document with a declared default namespace.
以供讨论:
<?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);
在运行此命令时,"items"中永远都没有数据,因为文档已命名.当然,如果删除默认的名称空间声明,则"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.
我尝试将查询更改为:"/prefix:root",并且Qt发出警告,如:
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
因此,名称空间绑定确实存在!但是哪里?我看到了QXmlNamePool,但是它没有增变方法.我可以使用查询ala的池创建一个QXmlName:
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");
但是它并没有改变任何东西.我不知所措,我使用的其他工具箱都有简单的方法将前缀绑定到名称空间URI.
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.
推荐答案
我相信您是否可以将查询更改为
I believe if you would change your query to
...
QXmlResultItems items;
query.setQuery("declare default element namespace \"http://namespace.com/ns1\"; /root");
...
它应该返回数据.
希望这会有所帮助
这篇关于使用QXmlQuery(Qt XQuery)时,如何绑定名称空间前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!