如何使用 Saxon-HE 中的 XSLT fn:document 函数读取 XML 字符串? [英] How to use the XSLT fn:document function in Saxon-HE to read an XML string?

查看:45
本文介绍了如何使用 Saxon-HE 中的 XSLT fn:document 函数读取 XML 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 .net 版本的 Saxon-HE.

I'm using the .net version of Saxon-HE.

我编写了一些代码来设置 XSLT 转换,其中源 XSLT 从外部传入(而不是在运行时从文件中读取).

I've written some code to set up an XSLT transformation where the source XSLT is passed in from outside (not read from a file at run-time).

这是我的代码片段:

Saxon.Api.Processor processor = new Saxon.Api.Processor();

// Feed the XSLT into Saxon
XmlDocument document = new XmlDocument();
document.LoadXml(xslt);
Saxon.Api.XdmNode input = processor.NewDocumentBuilder().Build(document);
Saxon.Api.XsltCompiler xsltCompiler = processor.NewXsltCompiler();
Saxon.Api.XsltExecutable xsltExecutable = xsltCompiler.Compile(input);
Saxon.Api.XsltTransformer xsltTransformer = xsltExecutable.Load();

// Create The stream that will contain the transformed XML.
MemoryStream transformedXmlStream = new MemoryStream();
xsltTransformer.InputXmlResolver = null;
// Input the XML into the transformer.
xsltTransformer.InitialContextNode = processor.NewDocumentBuilder().Build(inputXml);
// Set up the serializer that will output the result.
Saxon.Api.Serializer dataSerializer = processor.NewSerializer(transformedXmlStream);
// Run the transformation and get the output as a stream.
xsltTransformer.Run(dataSerializer);

到目前为止,这段代码效果很好!

This code works great so far!

但是,我遇到了新要求的问题.我被要求使用 document() 函数来实现一些功能,这需要另一个具有自己 BaseURI 的 XML 文档.

However, I'm having a problem with a new requirement. I've been asked to implement some functionality using the document() function, which requires another XML document with its own BaseURI.

这个其他文档将作为字符串或流直接输入程序,就像 XSLT 和输入 XML 一样.问题是我很难弄清楚如何将文档提供给 Saxon,该文档将被 document() 函数引用.

This other document will be fed directly into the program as a string or stream, just like the XSLT and the input XML. The problem is that I'm stumped figuring out how to feed in the document to Saxon that will be referenced by the document() function.

如何使用 document() 函数在 Saxon XSLT 中读取 XML 流?

How can I use the document() function to read an XML stream in Saxon XSLT?

推荐答案

将 xsltTransformer 的 InputXmlResolver 属性设置为 XmlResolver,该 XmlResolver 识别传递给 document() 函数的 URI 并返回相应的输入流.

Set the InputXmlResolver property of the xsltTransformer to an XmlResolver that recognizes the URI passed to the document() function and returns the corresponding input stream.

这篇关于如何使用 Saxon-HE 中的 XSLT fn:document 函数读取 XML 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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