xsl使用java的多个输出 [英] xsl multiple outputs using java

查看:107
本文介绍了xsl使用java的多个输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



xml:

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

< book>
< title> Libro< / title>
< chapter>
< title>第1章< / title>
< content>< / content>
< / chapter>
< chapter>
< title>第2章< / title>
< content>< / content>
< / chapter>
< / book>

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

xsl:

 < ?xml version =1.0encoding =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 =htmlindent =yes/>

< xsl:template match =./ book />
< xsl:for-each select =./ chapter>
< 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);
//使用Transformer输出
TransformerFactory tFactory = TransformerFactory.newInstance();
变压器变压器= tFactory.newTransformer(新的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中添加了路径,但似乎没有工作correclty



我应该改变吗?



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

属性@href元素是不允许的



我如何解决这个问题?

解决方案

我看到两个问题:


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

  2. Transformer对象的结果应该是一个空文档: transform.transform(source,new DOMResult());



I want to write an xsl document to generate motre than 1 output in .html according to the xml

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>

I want to get one html for each "chapter" in the xml, in this case, the result will have to be 2 html document

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 code :

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);

I've some questions

1.- I ran the program but I had this error:

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

I dont know why, I've add the path in my xsl:stylesheet but doesn't seem to work correclty

should I change for ?

2.- After I add to my java project the saxon library I have this error:

Attribute @href is not allowed on element

How do I solve this problem ?

解决方案

I see two problems:

  1. In your XSL file you should be using xsl:result-document instead of xsl:document.

  2. When you invoke transform on the Transformer object the result should be an empty document: transformer.transform(source, new DOMResult());

这篇关于xsl使用java的多个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆