XslLoadException: Resolving of external URIs was prohibited
Asked Answered
D

2

7

I have xslt sheet the have include tags for another xslt files, the all files compiled right and error free but when run the following code i got exception

var myXslTrans = new XslCompiledTransform();
XsltSettings sets = new XsltSettings();
sets.EnableScript = true;
myXslTrans.Load("visio.xsl",sets,null);
myXslTrans.Transform("page1.xml", "page.html");

following the exception text and stacktrace:

System.Xml.Xsl.XslLoadException: 
  XSLT compile error. An error occurred \bin\Debug\visio.xsl(116,40). 
  ---> System.Xml.XmlException: Resolving of external URIs was prohibited.
    at System.Xml.Xsl.Xslt.XsltLoader.Load(XmlReader reader)
       at System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
       at System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
       at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
       at System.Xml.Xsl.XslCompiledTransform.Load(String stylesheetUri, XsltSettings settings, XmlResolver stylesheetResolver)

I try to solve the problem by this but the problem still exist

Demosthenes answered 7/9, 2015 at 12:41 Comment(4)
What is the error you get? You show the stacktrace, but forgot the error and its description itself...Textuary
Also, you declared the path variable, but you do not use it. Did you mean to load the files from that location? In that case you should probably append it to the relative paths you have given.Textuary
System.Xml.Xsl.XslLoadException: XSLT compile error. An error occurred \bin\Debug\visio.xsl(116,40). ---> System.Xml.XmlException: Resolving of external URIs was prohibited.Demosthenes
i copy the files to the project to use it, path variable to get the base directory for the appDemosthenes
T
16

Quotes from comments / updated question:

i copy the files to the project to use it, path variable to get the base directory for the app

Yes, but the path variable is not used, so it has not effect.

System.Xml.Xsl.XslLoadException: XSLT compile error. An error occurred \bin\Debug\visio.xsl(116,40). ---> System.Xml.XmlException: Resolving of external URIs was prohibited.

This may mean one or more of several things:

  • Your settings do not allow loading external documents (this is the default for XslCompiledTransform, see documentation).
  • Your stylesheet contains a URI either directly (through xsl:import, xsl:include, document()) or indirectly (via a DTD or to resolve external entities).
  • If the above is not (entirely) true, at least the error points to exactly the point where the problem is. You didn't copy this in your question, but you will find it at (116, 40).

To resolve, just allow loading of external documents:

Replace this:

XsltSettings sets = new XsltSettings();
sets.EnableScript = true;

with this:

XsltSettings sets = new XsltSettings(true, true);

Update (after your comment)

I noticed another thing. You are setting the last argument to null, which according to Microsoft should give you a ArgumentNullException. It is not allowed to be null, but apparently Microsoft now allows it but then it has the effect that the UriResolver cannot resolve anything, because hey, it is null...

Not quite sure why you set it to null, but try setting it to a valid value, i.e.:

var resolver = new XmlUrlResolver();
myXslTrans.Load("visio.xsl", sets, resolver);
Textuary answered 7/9, 2015 at 13:17 Comment(7)
after replace i got the same error, yes my xsl contain xsl:includeDemosthenes
@mbugr, I updated my answer with that in mind, and I managed to repro your code. Essentially I just followed the MSDN documentation, please see links.Textuary
thank you the problem solved i face another problem that the document does not have root element but i will search to fix itDemosthenes
@Demosthenes no need to search, you just need to add a root element. An XML document cannot have multiple root elements, or cannot be empty. In both cases you would receive that error. If it helped, fee free to upvote / accept my answer ;)Textuary
Thank your for your helpDemosthenes
I have the same issue and cannot be resolved by the solution posted above. i still get exactly the same error with only the bare minimum of codeSateia
@duck, probably a bit late now... but if you're writing in .Net Core you might want to check out this: github.com/dotnet/runtime/issues/26969. It seems you need to opt-in now.Warp
F
4

The comment from Ian of Oz is highly pertinent, so promoting to answer.

In .net Core 6.0 (and probably later too) you MUST add the below at the top of your program.cs in addition to using the expected XsltSettings and

// override XmlException: Resolving of external URIs was prohibited.
AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);
// allow the use of the document() function in XSLT
var xsltSettings = new System.Xml.Xsl.XsltSettings(true,false);
var xslt = new XslCompiledTransform();
xslt.Load(path, xsltSettings, new XmlUrlResolver());
Foghorn answered 30/1 at 0:23 Comment(1)
In ASP.Net CORE that uses Startup.cs, the setting goes in "Configure" i.e. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // override XmlException: Resolving of external URIs was prohibited. AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);Incrassate

© 2022 - 2024 — McMap. All rights reserved.