如何从 Saxon 的扩展函数返回文档节点? [英] How to return document node from Saxon's extension function?
问题描述
我正在查看 Saxon-HE 10 中的集成扩展功能(Java).
I'm looking at Integrated extension functions in Saxon-HE 10 (Java).
我需要函数返回一个 document-node()?
,它是从 StreamSource
构造的.
I need the function to return a document-node()?
which is constructed from StreamSource
.
对类似问题的回答提到了这段代码:
context.getConfiguration().buildDocument()
然而,context
不会传递给 ExtensionFunction
,只有 XdmValue[] 参数
.
However context
is not passed to ExtensionFunction
, only XdmValue[] arguments
.
ExtensionFunctionDefinition
确实接受 XPathContext context
,但看起来 getConfiguration().buildDocument()
在 10 中不存在.
ExtensionFunctionDefinition
does accept XPathContext context
, but it looks like getConfiguration().buildDocument()
is not present in 10.
取而代之的是context.getConfiguration().buildDocumentTree(Source source)
,但是我不知道如何将TreeInfo
转换为Sequence
接口签名所需.
Instead, there is context.getConfiguration().buildDocumentTree(Source source)
, but I don't know how to convert TreeInfo
it returns to Sequence
required by the interface signature.
推荐答案
简单接口" (s9api.ExtensionFunction
) 不允许传入 XPathContext 对象.但是,为此目的您不需要完整的 XPath 动态上下文;您只需要访问配置对象.事实上,您可以在 s9api 级别完成所有工作,而无需深入了解 Saxon 内部结构:
The "simple interface" (s9api.ExtensionFunction
) does not allow an XPathContext object to be passed in. However, for this purpose you don't need the full XPath dynamic context; you only need access to the configuration object. In fact you can do everything at the s9api level without diving into Saxon internals:
final Processor proc = new Processor();
ExtensionFunction ef = new ExtensionFunction() {
...
public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
Source source = ...;
return proc.newDocumentBuilder().build(source);
}
};
proc.registerExtensionFunction(ef);
如果您确实需要比这更多的上下文信息,例如,如果您需要访问调用扩展函数的表达式的静态基 URI,那么您需要使用带有单独 的完整接口"ExtensionFunctionDefinition
和 ExtensionFunctionCall
对象.
If you do need more context information than this, for example if you need access to the static base URI of the expression from which your extension function is called, then you need to use the "full interface" with separate ExtensionFunctionDefinition
and ExtensionFunctionCall
objects.
然后您将需要使用较低级别的 Saxon 接口,例如 Sequence
和 NodeInfo
.Configuration.buildDocumentTree()
返回一个 TreeInfo
,它有一个 getRootNode()
方法返回一个 NodeInfo
.NodeInfo
实现了实现 Sequence
的 Item
,因此您可以从 ExtensionFunctionCall.call()
方法.
You will then need to use lower-level Saxon interfaces such as Sequence
and NodeInfo
. Configuration.buildDocumentTree()
returns a TreeInfo
, which has a getRootNode()
method returning a NodeInfo
. NodeInfo
implements Item
which implements Sequence
, so you can return the NodeInfo
from your ExtensionFunctionCall.call()
method.
这篇关于如何从 Saxon 的扩展函数返回文档节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!