marco
marco

Reputation: 1752

XPath: element with attribute invalidates query

I'm fetching the <script> element of an XML file via a XPath query and all is ok until there's no attribute in the elements before the <script> one.

In few words, this works:

<?xml version="1.0" encoding="UTF-8"?>
<html>
    <head>
        <script type="text/javascript" id="report-data">
            var templateMetaData =
...

This doesn't (html has an attribute xmlns):

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" id="report-data">
            var templateMetaData =
...

This is my query:

//script[attribute::id = "report-data"]

and I tried also this one:

/html/head/script[attribute::id = "report-data"]

I cannot find any document that could help me finding a solution.

I'm using QXmlQuery to execute the query and the code is the following (just a test app):

....
QXmlQuery query(QXmlQuery::XPath20);
query.setFocus(&file);     // the XML file
query.setQuery(queryStr);  // the query

QString values;
if( query.isValid())
{
    QXmlResultItems result;
    query.evaluateTo( &result );
    QXmlItem XmlItem( result.next());
    while( !XmlItem.isNull())
    {
        if( XmlItem.isAtomicValue())
        {
            values.append( XmlItem.toAtomicValue().toString());
            values.append("\n");
        }
        else if( XmlItem.isNode())
        {
            QXmlNodeModelIndex Index = XmlItem.toNodeModelIndex();
            values.append( Index.stringValue());
            values.append("\n");
        }
        XmlItem = result.next();
    }
}
else
{
    QMessageBox::information( parent, "error", QString("invalid query!"));
}
....

Any help is welcome.

Upvotes: 1

Views: 43

Answers (0)

Related Questions