QXmlQuery and XSLT20: Resultant Output String is empty everytime, works well on shell(xmlpattern)

StackOverflow https://stackoverflow.com/questions/7075309

  •  19-12-2020
  •  | 
  •  

문제

I am writing a class to parse Itunes Libray File using QXmlQuery and QT-XSLT.

Here's my sample code:

ItunesLibParser::ItunesLibParser()
{
    pathToLib = QString("/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml");
}

void ItunesLibParser::createXSLFile(QFile &inFile)
{
    if (inFile.exists()) {
        inFile.remove();
    }

    inFile.open(QIODevice::WriteOnly);
    QTextStream out(&inFile);

    out << QString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
    out << QString("<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">");
    out << QString("<xsl:output method=\"text\" />");


    out << QString("<xsl:template name=\"playlistNames\">");
    out << QString("<xsl:value-of select=\"child::integer[preceding-sibling::key[1]='Playlist ID']\"/>");
    out << QString("<xsl:text>&#xa;</xsl:text>");
    out << QString("<xsl:value-of select=\"child::string[preceding-sibling::key[1]='Name']\"/>");
    out << QString("<xsl:text>&#xa;</xsl:text>");
    out << QString("</xsl:template>");


    out << QString("<xsl:template match=\"/\">");
    out << QString("<xsl:for-each select=\"plist/dict/array/dict\">");
    out << QString("<xsl:call-template name=\"playlistNames\"/>");
    out << QString("</xsl:for-each>");
    out << QString("</xsl:template>");

    out << QString("</xsl:stylesheet>");

    inFile.close();

    return;

}

void ItunesLibParser::dumpPlayList()
{

    QXmlQuery query(QXmlQuery::XSLT20);
    query.setFocus(QUrl(pathToLib));

    QFile xslFile("plist.xsl");
    createXSLFile(xslFile);

    query.setQuery(QUrl("plist.xsl"));

    QStringList* outDump = new QStringList();

    query.evaluateTo(outDump);


    if(outDump != NULL) {

        QStringList::iterator iter = (*outDump).begin();
        for (; iter != (*outDump).end();
               ++iter)
            //code flow doesn't come here. It means being() == end()
            std::cout << (*iter).toLocal8Bit().constData() << std::endl;
    }

    return;
}

OutDump here doesn't contain data. While in Shell (xmlpatterns-4.7 mystlye.xsl itunes_music_library.xml ), If I run my Query I get proper output.

Is there anything, wrong I am doing while calling it programatically? I checked out plist.xsl is created properly, but my doubt is whether "/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml" this is getting loaded or not? Or there might be another reasons, I am confused. Is there any experts to throw some light onto problem, I will be glad.

도움이 되었습니까?

해결책

Intead from reading from the file, I read the file into buffer and converted that int string as passed to setquery. That solved the problem.

Here's sample code for those who could face similar problem in future.

void ITunesMlibParser::parsePlayListItemXml(int plistId)
{

    QXmlQuery xQuery(QXmlQuery::XSLT20);
    QFile inFile("/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml");

    if (!inFile.open(QIODevice::ReadOnly)) {
        return;
    }

    QByteArray bArray;
    while (!inFile.atEnd()) {
        bArray += inFile.readLine();
    }

    QBuffer xOriginalContent(&bArray);
    xOriginalContent.open(QBuffer::ReadWrite);
    xOriginalContent.reset();


    if (xQuery.setFocus(&xOriginalContent))
         std::cout << "File Loaded" << std::endl;

//.. 
//..

}

Thanks Rakesh

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top