XmlUrlResolver xsl:include

G

Gideon de Swardt

I am trying to do a server side transformation using an xsl:include in my
xslt stylesheet. The include stylesheet is stored /Library/abo.Library.xslt
and is included in multiple xslts, one example would be
/Valuation/ValuationSelect.xslt which is also used on the client side via
JavaScript and MSXML2.FreeThreadedDOMDocument.4.0. Client side the include
and transformation without any problems but it is when I am trying to do the
transformation on the server that I do get a XsltCompileException and
IOException error "the network path was not found". Worth mentioning also is
that the Library is a virtual folder in IIS, although I would have thought
that the XmlUrlResolver would resolve this problem for me it does not look
like it.

Here is the header of my xslt


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="/Library/abo.Library.xslt"/>
.....
</xsl:stylesheet>

and my C# code snippet for the transformation

XslTransform transform = new XslTransform();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
transform.Load(Server.MapPath("/Organization/OrganizationSearch.xslt"),
resolver);


Any ideas will be much appreciated

Gideon de Swardt
 
M

Martin Dechev

Hi, Gideon de Swardt,

The problem is that your includes are web-relative *and* dependent on the
root of the web-application, but the XmlUrlResolver tries to resolve them
relatively to the filesystem, because you pass the first parameter of the
Load method as a file:\\. You should either pass the absolute url of the
document (http://) or you should change the includes to be relative to the
document and independent of the web-application structure (using ../ instead
of /).

Hope this helps
Martin
 
G

Gideon de Swardt

Hi Martin

Thanks for your response

but

dont want to specify the url cause same solution might be installed on
different servers using different versions of this file.

tried relative path but because my Library folder is virtual directory in
IIS and the physical location on the file system is outside the root of the
website i returns the wrong path and returns an IOException example

webroot folder is c:\inetpub\wwwroot\webapp
where as the library folder is c:\common\library

so the url resolver will try and return
c:\inetpub\wwwroot\webapp\Library\abo.Library.xslt instead of
c:\common\library\abo.library.xslt. I would have thought that the Resolver
would be able to resolve urls across virtual folders. The reason for the
virtual folder is that multiple websites use the same folder and all have a
virtual folder pointing to the c:\common\library folder.

and the last thing i want to do is hard code the location of this file in
the xslt using file:// cause then i would not be able to use the xslt on the
client side.


so the question is what to do

Regards

G
 
M

Martin Dechev

Hi, Gideon de Swardt,

You should use something like:

transform.Load("http://domain.com/site/Organization/OrganizationSearch.xslt"
, resolver);

or

transform.Load("http://some IP
Address/site/Organization/OrganizationSearch.xslt", resolver);

for the website-root-relative paths to be resolved.

You can store the the first part of the url in a config file and change it
for each solution. i.e.:

transform.Load(
System.Configuration.ConfigurationSettings.AppSettings["RootUrl"] +
"/Organization/OrganizationSearch.xslt", resolver);

and in the web.config or the app.config:

<configuration>
<appSettings>
<add key="RootUrl" value="http://domain.com/site" />
</appSettings>
</configuration>

Greetings
Martin
 
G

Gideon de Swardt

Thanks Martin

I changed my code to point to the abs url
http://localhost/Valuation/ValuationSelect.xslt and that works if i use the
XslTransform object, but if I use System.Web.UI.WebControls.Xml to do the
transformation I can not pass the full url as the TransformSource, i receive
the following error.

Invalid path for MapPath
'/Valuation/http://localhost/Valuation/ValuationSelect.xslt'. A virtual path
is expected.

Is there any way to indicate to the Xml Control not to use virtual paths but
absolute paths, without writing my own control to do the transformation for
me?

Regards

Gideon
 
G

Gideon de Swardt

The TransformSource property is set in the aspx page rather than in the
compiled C# code, which allows for customization between multiple websites
without changing source code and having to re-compile the binaries.

My work around for this was to create a custom web control as bellow, which
also allowed me to set my default ArgumentLists as well, while loading the
base.Transform Xslt with the full url and XmlUrlResolver.

public class Xml : System.Web.UI.WebControls.Xml
{
public System.Xml.XmlUrlResolver UrlResolver;
protected override void Render(HtmlTextWriter writer)
{
Company.Web.Page thePage = (Company.Web.Page) this.Page;
//TransformArgumentList
this.TransformArgumentList = new XsltArgumentList();

this.TransformArgumentList.AddExtensionObject("http://www.company.net/XslLib
rary", new Company.Library());
//XmlUrlResolver
if (this.UrlResolver == null)
{
this.UrlResolver = new XmlUrlResolver();
this.UrlResolver.Credentials =
System.Net.CredentialCache.DefaultCredentials;
}
//Load Transform Source
if (this.TransformSource.Length > 0)
{
string transformSource = thePage.UrlBase +
thePage.ResolveUrl(this.TransformSource); //thePage.UrlBase returns the base
url eg. http://localhost
this.TransformSource = null;
base.Transform = new XslTransform();
base.Transform.Load(transformSource, this.UrlResolver);
}
//Render
base.Render(writer);
}
}


Thanks for all your help

Regards

Gideon de Sward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top