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

问题描述

我已经问过这个问题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总是通过调用XQueryExecutableload()创建的,因此您应该可以从那里开始。

,

如果您继承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")); 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...