Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resolve XSL includes in a Transformation that loads XSL from a String?
    primarykey
    data
    text
    <p><strong>.NET 2.0/VS2005</strong></p> <p>I am trying to use the <code>XslCompiledTransform</code> class to perform a XSL Transformation. I have two XSL files, the first of which includes a reference to the other in the form of an <code>&lt;xsl:include&gt;</code> statement :</p> <p><strong>Main.xsl:</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:include href="Included.xsl" /&gt; ... ... &lt;/xsl:stylesheet&gt; </code></pre> <p>Now, If I could load the "Main.xsl" file itself as a URI, my transformation code would be as simple as :</p> <pre><code>// This is a function that works. For demo only. private string Transform(string xslFileURI) { XslCompiledTransform xslt = new XslCompiledTransform(); // This load works just fine, if I provide the path to "Main.xsl". // The xsl:include is automatically resolved. xslTransform.Load(xslFileURI); StringWriter sw = new StringWriter(); xslt.Transform(Server.MapPath("~/XML/input.xml"), null, sw); return sw.ToString(); } </code></pre> <p>The problem is that I receive the contents of the Main.xsl file as a string and need to load the string as an <code>XmlReader/IXpathNavigable</code>. <strong><em>This is a necessary restriction at this time.</em></strong> When I try to do the same using an <code>XmlReader/XpathDocument</code>, it fails because the code looks for "Included.xsl" in the <code>C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\</code> folder! Obviously, the <code>XmlResolver</code> is not able to resolve the relative URL because it only receives a string as input XSL.</p> <p><strong>My efforts in this direction look like:</strong></p> <pre><code>// This doesn't work! Halp! private string Transform(string xslContents) { XslCompiledTransform xslt = new XslCompiledTransform(); XmlUrlResolver resolver = new XmlUrlResolver(); resolver.Credentials = CredentialCache.DefaultCredentials; //METHOD 1: This method does not work. XmlReaderSettings settings = new XmlReaderSettings(); settings.XmlResolver = resolver; XmlReader xR = XmlReader.Create(new StringReader(xslContents), settings); xslt.Load(xR); // fails // METHOD 2: Does not work either. XPathDocument xpDoc = new XPathDocument(new StringReader(xslContents)); xslt.Load(xpDoc, new XsltSettings(true, true), resolver); //fails. StringWriter sw = new StringWriter(); xslt.Transform(Server.MapPath("~/XML/input.xml"), null, sw); return sw.ToString(); } </code></pre> <p>I have tried to use the <code>ResolveUri</code> method of the XmlUrlResolver to obtain a <code>Stream</code> referencing the XSL file to be included, but am confused as to how to use this Stream. IOW, how do I tell the <code>XslCompiledTransform</code> object to use this stream in addition to the Main.xsl XmlReader:</p> <pre><code>Uri mainURI = new Uri(Request.PhysicalApplicationPath + "Main.xsl"); Uri uri = resolver.ResolveUri(mainURI, "Included.xsl"); // I can verify that the Included.xsl file loads in the Stream below. Stream s = resolver.GetEntity(uri, null, typeof(Stream)) as Stream; // How do I use this Stream in the function above?? </code></pre> <p><br /> Any help is greatly appreciated. Sorry for the long post!</p> <p>For your reference, the Exception StackTrace looks like this:</p> <pre><code>[FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Included.xsl'.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +328 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) +113 System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +78 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +51 System.Xml.Xsl.Xslt.XsltLoader.CreateReader(Uri uri, XmlResolver xmlResolver) +22 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(Uri uri, Boolean include) +33 System.Xml.Xsl.Xslt.XsltLoader.LoadInclude() +349 System.Xml.Xsl.Xslt.XsltLoader.LoadRealStylesheet() +704 System.Xml.Xsl.Xslt.XsltLoader.LoadDocument() +293 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include) +173 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload