Important: Please read the Qt Code of Conduct -

QXmlQuery and XPath expression

  • Hello,

    How can I use QXmlQuery to execute an XPath expression?

    Here is some test code that compiles but does not work;

    #include <QDomDocument>
    #include <QXmlQuery>
    #include <QFile>
    #include <QDebug>

    int main(int argc, char *argv[])
    // Test code
    QFile file("Books.xml");;

    QXmlQuery query(QXmlQuery::XPath20);
    bool validQuery = query.isValid();
    qDebug() << "QXmlQuery::isValid: " << validQuery;
    QStringList results;
    int count = results.count();
    qDebug() << "QStringList::Count: " << count;
    return 0;


    Books.xml looks like this;
    <?xml version="1.0" encoding="utf-8"?>

    According to QXmlQuery the query is valid but the result is always 0. Have tried various combinations of the query but nothing seems to work.

    The XPath expression works when testing it here:

    Appreciate some help.

Log in to reply