我正在尝试使用XSLT将播放列表保存在iTunes资料库中。我正在关注this article并使用第二页上的第一个XML作为转换方案:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="plist/dict/key[text()='Playlists']/ \
following-sibling::array/dict">
<xsl:value-of select="key[text()='Name']/ \
following-sibling::string" /><xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我的Java代码如下所示:
private File library = new File("iTunes Music Library.xml");
public String getPlaylists() {
Source source = new StreamSource(library);
StreamSource xsl = new StreamSource(getClass().getResourceAsStream("iTunesPL.xml")); //the XSL above
StringWriter w = new StringWriter();
Result result = new StreamResult(w);
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.transform(source, result);
return w.getBuffer().toString();
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
但它不起作用。显示的错误如下:(由我翻译成英文,因此在您的系统上可能看起来不一样......)
[Fatal Error] :2:6: Procession-command-target corresponding to "[xX][mM][lL]" is not allowed.
ERROR: 'Stylesheet couldn't be compiled.'
FATAL ERROR: "Stylesheet konnte nicht kompiliert werden"
:Procession-command-target corresponding to "[xX][mM][lL]" is not allowed.
javax.xml.transform.TransformerConfigurationException: Stylesheet couldn't be compiled
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:891)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:671)
at me.the_seppi.jaisb.ITunesPlaylistFetcher.convert(ITunesPlaylistFetcher.java:27)
at me.the_seppi.jaisb.Main.getITunesPlaylists(Main.java:193)
at me.the_seppi.jaisb.Main.main(Main.java:40)
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; Procession-command-target corresponding to "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1236)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Parser.java:427)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Parser.java:479)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:354)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:456)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:852)
... 6 more
---------
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; Procession-command-target corresponding to "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1236)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Parser.java:427)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.parse(Parser.java:479)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:354)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:456)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:852)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:671)
at me.the_seppi.jaisb.ITunesPlaylistFetcher.convert(ITunesPlaylistFetcher.java:27)
at me.the_seppi.jaisb.Main.getITunesPlaylists(Main.java:193)
at me.the_seppi.jaisb.Main.main(Main.java:40)
我做错了什么?该库由iTunes 10.5.3创建。
答案 0 :(得分:1)
反斜杠不应该在那里。尝试:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="plist/dict/key[text()='Playlists']/following-sibling::array/dict">
<xsl:value-of select="key[text()='Name']/following-sibling::string" />
<xsl:value-of select="$newline" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>