英文:
Problem with XSLT transformation from Mavan to create multiple documents with result-document
问题
我想转换和拆分一个XML文档。所以我使用了"result-document",它有效果。但是当我尝试用mavan启动XSLT时,我只得到一个带有XML声明的输出XML文档。
XSL:
<xsl:result-document method="xml" href="{$filename}_{$Number}.html">
<html>
<head>
<style>
body {
font-family: "Times New Roman", Times, serif;
font-size: 17pt;
line-height: 19pt;
}
</style>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:result-document>
JAVA:
TransformerFactory factory = TransformerFactory.newInstance();
InputStream inputStream = accessFile(xslpath);
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
f.setAttribute("http://saxon.sf.net/feature/linenumbering", Boolean.TRUE);
StreamSource schemaSource = new StreamSource(inputStream);
Transformer t = f.newTransformer(schemaSource);
StreamSource src = new StreamSource(new FileInputStream(inputpath));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
t.transform(src, res);
String a = res.getOutputStream().toString();
出了什么问题?
提前感谢。
英文:
I want to transform and split a XML document. So i use "result-document" and it works. But when I try to start the XSLT with mavan, i get a output xml document just with the xml declaration.
XSL:
<xsl:result-document method="xml" href="{$filename}_{$Number}.html">
<html>
<head>
<style>
body {
font-family: "Times New Roman", Times, serif;
font-size: 17pt;
line-height: 19pt;
}
</style>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:result-document>
JAVA:
TransformerFactory factory = TransformerFactory.newInstance();
InputStream inputStream= accessFile(xslpath);
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
f.setAttribute("http://saxon.sf.net/feature/linenumbering", Boolean.TRUE);
StreamSource schemaSource = new StreamSource(inputStream);
Transformer t = f.newTransformer(schemaSource);
StreamSource src = new StreamSource(new FileInputStream(inputpath));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
t.transform(src, res);
String a= res.getOutputStream().toString();
What is wrong?
Thanks in advance
答案1
得分: 0
作为一个猜测,我假设您在xsl:result-document method="xml" href="{$filename}_{$Number}.html"
中使用的相对URI可能出现了错误;因为您将其转换为ByteArrayOutputStream
上的StreamResult
,我认为处理器没有绝对基本输出URI,用于解析href
属性中构造的相对URI。
假设XSLT 2处理器是Saxon 9或10的某个版本,如何在使用JAXP Transformer
API时设置基本输出URI可能取决于确切的版本;我认为在Saxon 10中,您可以使用类似 ((TransformerImpl)t).getUnderlyingXsltTransformer().setBaseOutputURI("file:///dir/subfolder/subsubfolder/");
的方式来写入特定文件夹。
如果您改用Saxon的自有API——s9api
(http://saxonica.com/html/documentation/using-xsl/embedding/s9api-transformation.html),所有这些事情都会更加简单和直接处理,您将使用Processor
、XsltCompiler
、XsltExecutable
以及XsltTransformer
或Xslt30Transformer
。
英文:
As a wild guess, I assume that you are getting an error with the relative URIs you have in xsl:result-document method="xml" href="{$filename}_{$Number}.html"
; as you transform to a StreamResult
over a ByteArrayOutputStream
I think the processor does not have an absolute base output URI against which to resolve the relative URI constructed in the href
attribute.
Assuming the XSLT 2 processor is some version of Saxon 9 or 10, it might depend on the exact version on how to set the base output URI when using the JAXP Transformer
API; I think with Saxon 10 you can use e.h. ((TransformerImpl)t).getUnderlyingXsltTransformer().setBaseOutputURI("file:///dir/subfolder/subsubfolder/");
to write to a certain folder.
All such things are easier and more straightforward if you change to Saxon's own API, the s9api
(http://saxonica.com/html/documentation/using-xsl/embedding/s9api-transformation.html) where you deal with Processor
, XsltCompiler
, XsltExecutable
and XsltTransformer
or Xslt30Transformer
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论