gpt4 book ai didi

java - XSL 转换无法编译样式表

转载 作者:行者123 更新时间:2023-12-01 13:39:59 25 4
gpt4 key购买 nike

我正在尝试使用 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 创建的。

最佳答案

反斜杠不应该出现在那里。尝试:

<?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 - XSL 转换无法编译样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com