0

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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. This works! I have this code running in a loop so that it transforms many XML documents. I noticed that my XmlResolver code is called only once (a good thing!), which set me wondering about what the lifetime is of the data that Saxon receives from my XmlResolver.
The XSLT spec requires that multiple calls to document() with the same URI return the same document. So before calling the resolver, Saxon checks whether it has seen this URI before. The documents are retained in a document pool owned by the Controller (=JAXP Transformer) and unless you explicitly discard them, they are retained for the life of the Controller.
Thanks again for the information. This Saxon library is enabling me to create far superior solution to my client compared to if I were stuck with the .net XSLT classes that only support XSLT 1.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.