[Solved] QXmlQuery returns null item



  • Hi, I'm using a code probed to work with others xquery files, but in this case my code is not working.

    Here is xml file i'm using:
    @<comandos>
    <comando servicio="plafones" on=true id="1">2cp0021\n\r</comando>
    <comando servicio="plafones" on=true id="2">2cp0023\n\r</comando>
    <comando servicio="plafones" on=true id="3">2cp0025\n\r</comando>
    <comando servicio="plafones" on=false id="1">2cp0022\n\r</comando>
    <comando servicio="plafones" on=false id="2">2cp0024\n\r</comando>
    <comando servicio="plafones" on=false id="3">2cp0026\n\r</comando>
    </comandos>@

    Note: true and false are between double quotes in real file, I had to delete in here because on parameter got erased in the post.

    Here is xquery file:
    @declare variable $inputDocument external;
    declare variable $servicio external;
    declare variable $on external;
    declare variable $id external;

    doc($inputDocument)//comandos/comando[@servicio=xs:string($servicio) and @on=xs:boolean($on) and @id=xs:integer($id)]/string()@

    xmlpatterns console command returns the correct string as result:
    @$ xmlpatterns -param inputDocument=comando.xml -param servicio=plafones -param on=true -param id=1 comando.xq
    2cp0021\n\r@

    And here is the piece of code that uses all this stuff:
    @QString ConfigSistema::getComando(QString servicio, bool on, int id)
    {
    QString comando = "null";
    QFile instanceFile(getXMLPrincipal());
    instanceFile.open(QIODevice::ReadOnly);

    QFile xq(":/xquery/xml/xquery/comando.xq");
    xq.open(QIODevice::ReadOnly);

    QXmlQuery query;
    query.bindVariable("inputDocument", &instanceFile);
    query.bindVariable("servicio", QXmlItem(QVariant(servicio)));
    query.bindVariable("on", QXmlItem(QVariant(on)));
    query.bindVariable("id", QXmlItem(QVariant(id)));

    if(xq.isOpen()){
    query.setQuery(&xq, QUrl::fromLocalFile(xq.fileName()));

    QXmlResultItems res;
    query.evaluateTo(&res&#41;;
    QXmlItem item(res.next());
    while (!item.isNull()) {
      if (item.isAtomicValue()) {
        QVariant v = item.toAtomicValue();
        if(v.type() == QVariant::String){
          comando = v.toString();
          qDebug() << "getComando()" << comando;
        }
      }
      item = res.next();
    }
    

    } else
    qDebug() << "not opened" << xq.fileName();

    xq.close();
    instanceFile.close();

    return comando;
    }@

    After running this method, item is always null when it is supossed to be atomic value containing the expected string.



  • I found the problem, I was giving an incorrect file name for the xml to be processed.

    I changed this line:
    @QFile instanceFile(getXMLPrincipal());@

    I put another fuction as parameter to get the path to the correct file and I got it working.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.