How to use QXmlQuery? (Qt XQuery / XPath)

I use the following code to load into an XML file (actually 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>waWdnZFHevdBeZTUnZ2dnUVZ8uOdnZ2d@giganews.com</segment>
        </segments>
    </file>
</nzb>

However, the next time you enter, the results are not returned. Here's how to format input 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="test@test.test" 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">waWdnZFHevdBeZTUnZ2dnUVZ8uOdnZ2d@giganews.com</segment>
            <segment bytes="661967" number="1">waWdfhrgfnZFHevdBeZTUnZ2dnUVZ8uOdnZ2d@giganews.com</segment>
        </segments>
    </file>
</nzb>

Please, can someone tell me what I'm doing wrong?

+5
source share
2 answers

I discovered this because I needed to provide a default namespace, which took several hours ...

Request now:

declare default element namespace "http://www.newzbin.com/DTD/2003/nzb";
declare variable $path external;
doc($path)/nzb/file/segments/segment/string()
+7
source

Perhaps use a namespace query template in the query?

doc($path)//*:file/*:segments/*:segment/string()
0
source

All Articles