如何正确使用QXmlQuery? (Qt XQuery/XPath)
我使用以下代码加载 XML 文件(实际上是 NZB):
QXmlQuery query;
query.bindVariable("path", QVariant(path));
query.setQuery("doc($path)/nzb/file/segments/segment/string()");
if(!query.isValid())
throw QString("Invalid query.");
QStringList segments;
if(!query.evaluateTo(&segments))
throw QString("Unable to evaluate...");
QString string;
foreach(string, segments)
qDebug() << "String: " << string;
使用以下输入,它会按预期工作:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
<nzb>
<file>
<groups>
<group>alt.binaries.cd.image</group>
</groups>
<segments>
<segment>[email protected]</segment>
</segments>
</file>
</nzb>
但是,使用以下输入不会返回任何结果。这就是输入的格式,带有属性:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
<file poster="[email protected]" date="1225385180" subject="ubuntu-8.10-desktop-i386 - ubuntu-8.10-desktop-i386.par2 (1/1)">
<groups>
<group>alt.binaries.cd.image</group>
</groups>
<segments>
<segment bytes="66196" number="1">[email protected]</segment>
<segment bytes="661967" number="1">[email protected]</segment>
</segments>
</file>
</nzb>
请有人告诉我我做错了什么?
I'm using the following code to load in an XML file (actually an NZB):
QXmlQuery query;
query.bindVariable("path", QVariant(path));
query.setQuery("doc($path)/nzb/file/segments/segment/string()");
if(!query.isValid())
throw QString("Invalid query.");
QStringList segments;
if(!query.evaluateTo(&segments))
throw QString("Unable to evaluate...");
QString string;
foreach(string, segments)
qDebug() << "String: " << string;
With the following input, it works as expected:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
<nzb>
<file>
<groups>
<group>alt.binaries.cd.image</group>
</groups>
<segments>
<segment>[email protected]</segment>
</segments>
</file>
</nzb>
However, with the following input no results are returned. This is how the input should be formatted, with attributes:
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
<file poster="[email protected]" date="1225385180" subject="ubuntu-8.10-desktop-i386 - ubuntu-8.10-desktop-i386.par2 (1/1)">
<groups>
<group>alt.binaries.cd.image</group>
</groups>
<segments>
<segment bytes="66196" number="1">[email protected]</segment>
<segment bytes="661967" number="1">[email protected]</segment>
</segments>
</file>
</nzb>
Please can someone tell me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现这是因为我需要提供默认名称空间,这花了几个小时才弄清楚......
现在的查询是:
I discovered it's because I needed to supply a default namespace, that took hours to figure out...
The query is now:
也许在查询中使用名称空间通配符?
Maybe use the namespace wildcard in the query?