从 Saxon 转换中获取序列化属性 [英] Getting the serialization properties from Saxon transformation

查看:45
本文介绍了从 Saxon 转换中获取序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSL 样式表:

我正在使用 Saxon 9.9 将其转换成这样.

public String transform(InputStream input, InputStream stylesheet, OutputStream output){处理器 p = 新处理器(假);XsltCompiler c = p.newXsltCompiler();XsltExecutable e = c.compile(new StreamSource(stylesheet));Xslt30Transformer xf = e.load30();序列化器 s = p.newSerializer(output);xf.transform(new StreamSource(input), s);返回 s.getOutputProperty(Serializer.Property.ENCODING));}

我希望返回值是样式表中指定的UTF-8",但它返回 null.(序列化器实例似乎根本没有属性.)这是为什么,获取输出属性的正确方法是什么?

解决方案

不幸的是,XSLT 转换引擎和序列化程序之间的接口非常混乱.它在 XSLT 规范中很混乱,在 JAXP 中很混乱,在 Saxon 中也很混乱.多年来,我已经进行了多次尝试来改进它,但它仍然很混乱.在 Saxon 9.9 中有一些更改,新的和看似无害的(在 XSLT 中,几乎无用)item-separator 属性使其成为必要,该属性具有将单个项目组合到文档树中的效果(如果它真的发生了)必须发生在边界的序列化端,而不是在转换端.

Saxon 9.9 s9api 接口中的情况是(通常)Serializer 对象对样式表中定义的序列化属性一无所知,它只知道那些通过 Serializer API 直接提供的属性.只有 Transformer 调用 Serializer 进行序列化时,两组序列化属性才会合并.

例外情况是当您使用 Xslt30Transformer.newSerializer() 创建序列化程序时.在这种情况下,序列化程序使用样式表中未命名的 xsl:output 声明中定义的属性进行初始化,就好像它们是使用 serializer.setOutputProperty() 显式设置的一样.>

样式表中定义的序列化属性可作为 s9api XsltExecutable 的属性使用(虽然不是很直接).它有一个方法 getUnderlyingCompiledStylesheet() 返回一个 PreparedStylesheet 对象,还有一个方法 getDeclaredSerializationProperties() 返回定义在默认值上的属性(未命名) xsl:output 声明.还有一个方法 getOutputProperties(name) 返回命名输出格式的属性(如在 xsl:result-document 中使用)

I have following XSL stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
   <xsl:output encoding="UTF-8" method="xml"/>
   <xsl:template match="/">
      <test/>
   </xsl:template>
</xsl:stylesheet>

I am using Saxon 9.9 to transform it like this.

public String transform(InputStream input, InputStream stylesheet, OutputStream output){
  Processor p = new Processor(false);
  XsltCompiler c = p.newXsltCompiler();
  XsltExecutable e = c.compile(new StreamSource(stylesheet));
  Xslt30Transformer xf = e.load30();
  Serializer s = p.newSerializer(output);
  xf.transform(new StreamSource(input), s);
  return s.getOutputProperty(Serializer.Property.ENCODING));
}

I would expect the return value to be "UTF-8" as specified in the stylesheet, but it returns null instead. (The serializer instance seems to have no properties at all.) Why is this, and what is the correct way to get the output properties?

解决方案

The interface between the XSLT transformation engine and the serializer is unfortunately very messy. It's messy in the XSLT spec, it's messy in JAXP, and it's messy in Saxon. I've made a number of attempts over the years to improve it, but it's still messy. There were some changes in Saxon 9.9, made necessary by the new and innocent-seeming (and in XSLT, almost useless) item-separator attribute, which has the effect that composing individual items into a document tree (if it happens at all) has to happen on the serialization side of the boundary, not on the transformation side.

The situation in the Saxon 9.9 s9api interface is that (in general) the Serializer object knows nothing about the serialization properties defined in the stylesheet, it only knows about those supplied directly via the Serializer API. Only when the Transformer invokes the Serializer to perform serialization do the two sets of serialization properties get combined.

The exception is when you use Xslt30Transformer.newSerializer() to create the serializer. In that case the serializer is initialized with the properties defined in unnamed xsl:output declarations in the stylesheet, as if they were set explicitly using serializer.setOutputProperty().

The serialization properties defined in the stylesheet are available (though not very directly) as properties of the s9api XsltExecutable. This has a method getUnderlyingCompiledStylesheet() which returns a PreparedStylesheet object, and this has a method getDeclaredSerializationProperties() which returns properties defined on the default (unnamed) xsl:output declarations. There is also a method getOutputProperties(name) which returns properties for a named output format (as used in xsl:result-document)

这篇关于从 Saxon 转换中获取序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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