我正在使用xslt语法解析itunes库XML文件。
以下是示例代码
void ITunesMlibParser::createXslForPlistItems(QString &out, int playlistID)
{
std::stringstream ss;
ss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
ss << "<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:fn=\"fn\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">";
ss << "<xsl:output method=\"text\" />";
ss << "<xsl:template match=\"/\">";
// ss << "<xsl:variable name=\"myplaylist\" select=\"'6711'\"/>";
ss << "<xsl:variable name=\"myplaylist\" select=\"'"<< playlistID <<"'\">" ;
ss << "<xsl:variable name=\"playlist_tracks\" select=\"/plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=$myplaylist]/array/dict/integer[preceding-sibling::key[1]='Track ID']\" />";
ss << "<xsl:variable name=\"tracks\" select=\"/plist/dict/dict/dict[integer[preceding-sibling::key[1]='Track ID']=$playlist_tracks]\" />";
ss << "<xsl:for-each select=\"$tracks\">";
ss << "<xsl:value-of select=\"integer[preceding-sibling::key[1]='Track ID']\"/>";
ss << "<xsl:text>,</xsl:text>";
ss << "<xsl:value-of select=\"string[preceding-sibling::key[1]='Name']\"/>";
ss << "<xsl:text>,</xsl:text>";
ss << "<xsl:value-of select=\"integer[preceding-sibling::key[1]='Total Time']\"/>";
ss << "<xsl:text>,</xsl:text>";
ss << "<xsl:value-of select=\"string[preceding-sibling::key[1]='Kind']\"/>";
ss << "<xsl:text>,</xsl:text>";
ss << "<xsl:value-of select=\"string[preceding-sibling::key[1]='Location']\"/>";
ss << "<xsl:text>
</xsl:text>";
ss << "</xsl:for-each>";
ss << "</xsl:template>";
ss << "</xsl:stylesheet>";
ss << "";
out = QString::fromStdString(ss.str());
return;
}
请考虑以下两行:
// ss << "<xsl:variable name=\"myplaylist\" select=\"'6711'\"/>";
ss << "<xsl:variable name=\"myplaylist\" select=\"'"<< playlistID <<"'\">" ;
如果我硬编码播放列表的整数值= 6711,则解析器有效。但是如果我以编程方式传递一个整数值,它会给出以下误差。
Error XTSE0620 line 11, column 190: When attribute select is present on variable, a sequence constructor cannot be used.
// My comment --> line 11 is below or I am not sure if this is guilty line as it works with hard coded value.
<xsl:variable name="playlist_tracks" select="/plist/dict/array/dict[integer[preceding-sibling::key[1]='Playlist ID']=$myplaylist]/array/dict/integer[preceding-sibling::key[1]='Track ID']" />
我被困在这里,非常需要XSLT专家的一些意见。
答案 0 :(得分:1)
我认为您只需确保具有xsl:variable
属性的任何select
元素都没有内容,因此请确保您拥有以下内容: <xsl:variable name="var-name" select="2"/>
或<xsl:variable name="var-name" select="2"></xsl:variable>
。
所以使用你的C ++,你可能想要,例如ss << "<xsl:variable name=\"myplaylist\" select=\"'"<< playlistID <<"'\"/>" ;
虽然我没有接受过读写C ++的训练。
我也想知道为什么在你的XSLT / XPath中你试图引用整数值,因为你在XPath中得到字符串,而XPath也有数字。因此,根据您的需求,您可能希望删除单引号:ss << "<xsl:variable name=\"myplaylist\" select=\""<< playlistID <<"\"/>" ;
。
答案 1 :(得分:1)
Consider below two lines:
// ss << "<xsl:variable name=\"myplaylist\" select=\"'6711'\"/>";
ss << "<xsl:variable name=\"myplaylist\" select=\"'"<< playlistID <<"'\">" ;
错误非常明显:在第二种情况下,您不会关闭生成的<xsl:variable>
元素。
必须(请注意最后的/
):
ss << "<xsl:variable name=\"myplaylist\" select=\"'"<< playlistID <<"'\"/>" ;