【问题标题】:How to use the XSLT fn:document function in Saxon-HE to read an XML string?如何使用 Saxon-HE 中的 XSLT fn:document 函数读取 XML 字符串?
【发布时间】:2013-10-03 19:06:32
【问题描述】:

我正在使用 Saxon-HE 的 .net 版本。

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

这是我的代码的 sn-p:

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);

到目前为止,这段代码运行良好!

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

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

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

【问题讨论】:

标签: .net xml xslt xpath saxon


【解决方案1】:

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

【讨论】:

  • 谢谢。这行得通!我让这段代码循环运行,以便转换许多 XML 文档。我注意到我的 XmlResolver 代码只被调用了一次(一件好事!),这让我想知道撒克逊从我的 XmlResolver 收到的数据的生命周期是多少。
  • XSLT 规范要求使用相同 URI 多次调用 document() 返回相同的文档。因此,在调用解析器之前,Saxon 会检查它之前是否见过这个 URI。这些文档保留在控制器(=JAXP 转换器)拥有的文档池中,除非您明确丢弃它们,否则它们将在控制器的生命周期内保留。
  • 再次感谢您提供的信息。与只支持 XSLT 1.0 的 .net XSLT 类相比,这个 Saxon 库使我能够为我的客户创建更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多