XSLT转换从Maven到使用result-document创建多个文档时出现问题。

huangapple go评论90阅读模式
英文:

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:

&lt;xsl:result-document method=&quot;xml&quot; href=&quot;{$filename}_{$Number}.html&quot;&gt;
	&lt;html&gt;
		&lt;head&gt;
			&lt;style&gt;
body {
font-family: &quot;Times New Roman&quot;, Times, serif;
font-size: 17pt;
line-height: 19pt;
}
			&lt;/style&gt;
		&lt;/head&gt;
		&lt;body&gt;
			&lt;xsl:apply-templates/&gt;
		&lt;/body&gt;
	&lt;/html&gt;
&lt;/xsl:result-document&gt; 

JAVA:

TransformerFactory factory = TransformerFactory.newInstance(); 
InputStream inputStream= accessFile(xslpath);
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute(&quot;http://saxon.sf.net/feature/version-warning&quot;, Boolean.FALSE);
f.setAttribute(&quot;http://saxon.sf.net/feature/linenumbering&quot;,  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),所有这些事情都会更加简单和直接处理,您将使用ProcessorXsltCompilerXsltExecutable以及XsltTransformerXslt30Transformer

英文:

As a wild guess, I assume that you are getting an error with the relative URIs you have in xsl:result-document method=&quot;xml&quot; href=&quot;{$filename}_{$Number}.html&quot;; 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(&quot;file:///dir/subfolder/subsubfolder/&quot;); 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.

huangapple
  • 本文由 发表于 2020年7月29日 15:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63148968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定