1

Qt's QXmlQuery::setQuery has a polymorphism, just like:

void QXmlQuery::setQuery(const QString &sourceCode, const QUrl &documentURI = QUrl())

However, when I pass a HTML source code to the parameter sourceCode, and try to evaluate, I can only get an error:

Error XPST0003 in file:///, at line 1, column 2: syntax error, unexpected unknown keyword, expecting QName or NCName

Here is an example:

QString srcHTML = "<html>......</html>";    // An HTML forked from any website

QXmlQuery query;
query.setQuery(srcHTML, QUrl("/html/body/"));

QString r;
query.evaluateTo(&r);

qDebug() << r;

Then an error message shows:

Error XPST0003 in file:///html/body/, at line 1, column 2: syntax error, unexpected unknown keyword, expecting QName or NCName ""

That's strange, even though I've feeded QXmlQuery::setQuery() a valid HTML source!

Akura Ryu
  • 31
  • 4
  • Maybe, it is about the second parameter, see https://doc.qt.io/qt-5/qxmlquery.html#details . Check if QUrl is valid. – sirop Apr 07 '19 at 09:44
  • @sirop The example in my code comes from this: https://stackoverflow.com/a/18677398/10865463 – Akura Ryu Apr 07 '19 at 10:34
  • Then could you explain:`query.setQuery("doc('index.html')/html/body/p[1]");`? – sirop Apr 07 '19 at 10:59

1 Answers1

1

Use query.setFocus(srcHTML); query.setQuery("/html/body");. Note that the input string to setFocus needs to be well-formed XML which HTML often not is.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110