问题描述
我已经问过这个问题before,但是现在我试图获取XQuery评估而不是XSLT转换的序列化属性。
使用此代码:
public static void main(String[] args) throws SaxonApiException {
Processor p = new Processor(false);
XQueryEvaluator e = p.newXQueryCompiler().compile(
"xquery version \"3.0\";" +
"declare namespace output = \"http://www.w3.org/2010/xslt-xquery-serialization\"; " +
"declare option output:encoding \"utf-8\";" +
"<x s='ó'/>").load();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Serializer s = p.newSerializer(os);
e.setDestination(s);
e.run();
assert "utf-8".equals(s.getCombinedOutputProperties(null).getProperty("encoding")); //fails
}
原始问题答案中提出的方法似乎不适用于此处,因为我无法从XQueryEvaluator
实例化序列化程序,并且getDeclaredSerializationProperties
仅在XSLT上下文中可用。 / p>
我正在使用Saxon 10 HE。
解决方法
从XQueryExecutable
e:
e.getUnderlyingCompiledQuery().getExecutable().getPrimarySerializationProperties()
我无法立即从XQueryEvaluator
中看到一种方法,但是由于XQueryEvaluator
总是通过调用XQueryExecutable
从load()
创建的,因此您应该可以从那里开始。
如果您继承Serializer
的子类并覆盖方法getReceiver
来访问/存储传递给该方法的SerializationParameters,则可能是可行的:
public class MySerializer extends Serializer {
public MySerializer(Processor processor) {
super(processor);
}
private SerializationProperties mySerProps;
public SerializationProperties getSerProps() {
return mySerProps;
}
@Override
public Receiver getReceiver(PipelineConfiguration arg0,SerializationProperties arg1) throws SaxonApiException {
mySerProps = arg1;
return super.getReceiver(arg0,arg1);
}
}
然后使用例如
MySerializer s = new MySerializer(p);
s.setOutputStream(os);
e.setDestination(s);
e.run();
assert "utf-8".equals(s.getSerProps().getProperty("encoding"));