开发者

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

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 Itu开发者_Python百科nesLibParser::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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜