首页 文章

使用java的xsl多输出

提问于
浏览
1

我想编写一个xsl文档,根据xml生成.html中的1个输出的motre

xml:

<?xml version="1.0" encoding="UTF-8"?>

<book>
    <title>Libro</title>
    <chapter>
        <title>Chapter 1</title>
        <content></content>
    </chapter>
    <chapter>
        <title>Chapter 2</title>
        <content></content>
    </chapter>
</book>

我想在xml中为每个“章节”获取一个html,在这种情况下,结果必须是2个html文档

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"
                version="1.1">

<xsl:output method="html" indent="yes"/>

<xsl:template match="./book/">
    <xsl:for-each select="./chapter">
        <xsl:variable name="filename" select="concat(title,'.html')"/>
        <xsl:document href="{$filename}">
            <html>
                <head>
                    <title>
                        <xsl:value-of select="title" />
                    </title>
                </head>
                <body>
                    <xsl:value-of select="body" />
                </body>
            </html>
        </xsl:document>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

java代码:

DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(input);
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslt));

DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);

我有一些问题

1.-我运行程序但是我遇到了这个错误:

java.lang.RuntimeException:Elemento'http://www.w3.org/1999/XSL/Transform:document'de XSL no soportado

我不知道为什么,我在我的xsl:stylesheet中添加了路径但似乎没有工作

我应该改变吗?

2.-在我的java项目中添加saxon库之后我有这个错误:

元素上不允许使用@href属性

我该如何解决这个问题?

1 回答

  • 1

    我看到两个问题:

    • 在您的XSL文件中,您应该使用 xsl:result-document 而不是 xsl:document .

    • 在Transformer对象上调用transform时,结果应为空文档: transformer.transform(source, new DOMResult());

相关问题