tags:

views:

337

answers:

2

In the Java XML transformation package, there is a concept known as a URIResolver, which is "An object that implements this interface that can be called by the processor to turn a URI used in document(), xsl:import, or xsl:include into a Source object."

So, if your XSL has an import like this

<xsl:import href="URI"/>

This allows you to take URI and map it to the Source of your choice - maybe it comes from a database, or maybe you want to map the URI to another URI. This can be useful, since you can't use an xsl:variable in the xsl:import href.

Here is some sample Java code that creates a transformer and does a transform.

URIResolver uriResolver = new MyURIResolver();  // sample
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setURIResolver(uriResolver);
Transformer transformer = transformerFactory.newTransformer();
transformer.setURIResolver(uriResolver);
transformer.transform(xml, result);

Note, there are two places in the code where the URIResolver is used:

  • TransformerFactory setURIResolver - "Set an object that is used by default during the transformation to resolve URIs used in document(), xsl:import, or xsl:include."
  • Transformer setURIResolver - "Set an object that will be used to resolve URIs used in document()."

Question: Is there a similar concept to the Java URIResolver in C# and .NET?

+1  A: 

Yes, the analog exists.
IXmlNamespaceResolver and also XmlNamespaceManager, which implements the interface.

@Lame Duck, I think you misunderstood the question.

Cheeso
+1  A: 

Yes, it's called XmlResolver: http://msdn.microsoft.com/en-us/library/system.xml.xmlresolver.aspx

XmlResolver is one of System.Xml abstractions, which means you can use it in various APIs, such as XslCompiledTransform, XmlDocument, XmlReader (via XmlReaderSettings), etc.

Here's a more in-depth on how it works, and how you can implement your own resolver: http://msdn.microsoft.com/en-us/library/aa302284.aspx

By default, these various APIs use the XmlUrlResolver: http://msdn.microsoft.com/en-us/library/system.xml.xmlurlresolver.aspx

...which can resolve URIs that start with http:// and file://

Max Toro
Thanks. XmlResolver is what I need to "process include and import elements found in XSL style sheets," and I also found that to enable the document() function, I need to create a XsltSettings object (http://msdn.microsoft.com/en-us/library/ms163499.aspx).
Kevin Hakanson