从 Saxon 中的 XQueryEvaluator 获取序列化属性

Getting serialization properties from an XQueryEvaluator in Saxon

我已经问过这个问题 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 上下文中可用.

我正在使用 Saxon 10 HE。

如果您将 Serializer 子类化并将方法 getReceiver 重写为 access/store 传递给该方法的 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")); 

来自 XQueryExecutable e:

e.getUnderlyingCompiledQuery().getExecutable().getPrimarySerializationProperties()

我无法立即从 XQueryEvaluator 中找到执行此操作的方法,但由于 XQueryEvaluator 始终是通过调用 load()XQueryExecutable 创建的,你应该可以从那里开始。