How to use the XSLT fn:document function in Saxon-HE to read an XML string?

女生的网名这么多〃 提交于 2019-12-12 03:06:36

问题


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

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

Here's a snippet of my code:

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!

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.

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/19167240/how-to-use-the-xslt-fndocument-function-in-saxon-he-to-read-an-xml-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!