473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

xsl Transform - document('') won't resolve!

I've got a c# app that does some transforms using XSL files that are stored
as Embedded Resources in the app. That turns out to be significant; i.e.,
the problem I've encountered goes away if I reference the XSL files from the
file system.

The problem is that within the XSL file I have a reference to document(''),
intending to "self reference" the stylesheet. As I mentioned above, this
works fine as long as the stylesheet is file based; however when it is
instead an embedded resource then the document('') call returns an empty
node set.

I'm guessing this has to do with the required resolver argument to the
Transform method of the XslTransform class. My code looks like:

XmlTextReader xslreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(reportTra nsformResource)));
....
xlt.Load(xslreader, null, typeof(ProcessModel).Assembly.Evidence);
....
XmlUrlResolver resolver = new XmlUrlResolver();
xlt.Transform(xpdoc.CreateNavigator(), null, xmlw, resolver);

I'm further guessing that the problem is with the Credentials of the
resolver. I've tried using the default credentials, but that doesn't help.

So, why does this fail for an embedded resource, but not for a file?

Help!!

Bill

Nov 12 '05 #1
6 1634
Bill Cohagan wrote:
I've got a c# app that does some transforms using XSL files that are stored
as Embedded Resources in the app. That turns out to be significant; i.e.,
the problem I've encountered goes away if I reference the XSL files from the
file system.

The problem is that within the XSL file I have a reference to document(''),
intending to "self reference" the stylesheet. As I mentioned above, this
works fine as long as the stylesheet is file based; however when it is
instead an embedded resource then the document('') call returns an empty
node set.

I'm guessing this has to do with the required resolver argument to the
Transform method of the XslTransform class. My code looks like:

XmlTextReader xslreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(reportTra nsformResource)));
....
xlt.Load(xslreader, null, typeof(ProcessModel).Assembly.Evidence);
....
XmlUrlResolver resolver = new XmlUrlResolver();
xlt.Transform(xpdoc.CreateNavigator(), null, xmlw, resolver);


XmlUrlResolver is just plain URL resolver and obviously can't resolve
XSLT embedded into an assembly. You need to create your own XmlResolver,
which resolves using GetManifestResourceStream() method.
Take a look at "Resolving the Unknown: Building Custom XmlResolvers in
the .NET Framework" article at
http://msdn.microsoft.com/xml/defaul.../cusxmlres.asp
for a sample.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2
Oleg
Thanks!

Bill

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill Cohagan wrote:
I've got a c# app that does some transforms using XSL files that are
stored as Embedded Resources in the app. That turns out to be
significant; i.e., the problem I've encountered goes away if I reference
the XSL files from the file system.

The problem is that within the XSL file I have a reference to
document(''), intending to "self reference" the stylesheet. As I
mentioned above, this works fine as long as the stylesheet is file based;
however when it is instead an embedded resource then the document('')
call returns an empty node set.

I'm guessing this has to do with the required resolver argument to the
Transform method of the XslTransform class. My code looks like:

XmlTextReader xslreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(reportTra nsformResource)));
....
xlt.Load(xslreader, null, typeof(ProcessModel).Assembly.Evidence);
....
XmlUrlResolver resolver = new XmlUrlResolver();
xlt.Transform(xpdoc.CreateNavigator(), null, xmlw, resolver);


XmlUrlResolver is just plain URL resolver and obviously can't resolve XSLT
embedded into an assembly. You need to create your own XmlResolver, which
resolves using GetManifestResourceStream() method.
Take a look at "Resolving the Unknown: Building Custom XmlResolvers in the
.NET Framework" article at
http://msdn.microsoft.com/xml/defaul.../cusxmlres.asp
for a sample.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #3
Oleg
I've checked out the referenced URL and the information looks promising. I
particularly happy to find the XmlUriResolver class code; however I find
that it is quite buggy -- clearly hasn't even been compiled! One thing it
references that confuses me is CacheStreams. What is that and where might I
find it?

Thanks,
Bill

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill Cohagan wrote:
I've got a c# app that does some transforms using XSL files that are
stored as Embedded Resources in the app. That turns out to be
significant; i.e., the problem I've encountered goes away if I reference
the XSL files from the file system.

The problem is that within the XSL file I have a reference to
document(''), intending to "self reference" the stylesheet. As I
mentioned above, this works fine as long as the stylesheet is file based;
however when it is instead an embedded resource then the document('')
call returns an empty node set.

I'm guessing this has to do with the required resolver argument to the
Transform method of the XslTransform class. My code looks like:

XmlTextReader xslreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(reportTra nsformResource)));
....
xlt.Load(xslreader, null, typeof(ProcessModel).Assembly.Evidence);
....
XmlUrlResolver resolver = new XmlUrlResolver();
xlt.Transform(xpdoc.CreateNavigator(), null, xmlw, resolver);


XmlUrlResolver is just plain URL resolver and obviously can't resolve XSLT
embedded into an assembly. You need to create your own XmlResolver, which
resolves using GetManifestResourceStream() method.
Take a look at "Resolving the Unknown: Building Custom XmlResolvers in the
.NET Framework" article at
http://msdn.microsoft.com/xml/defaul.../cusxmlres.asp
for a sample.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #4
Oleg
OK, now I'm really confused. One of the problems I detected when I tried
to compile the code provided at the site is that it referenced the class URI
and I could only find a Uri class. That led in turn to an error on the
OriginalString reference since Uri has no such property.

Now I find that indeed using AbsoluteUri instead of OriginalString is no
good because everything is lowercased which breaks the resource reference.
So, I'm guessing that in spite of my inability to find it, there really is a
URI class out there in some namespace. Could you perhaps give me a pointer
to URI?

Thanks,
Bill

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill Cohagan wrote:
I've got a c# app that does some transforms using XSL files that are
stored as Embedded Resources in the app. That turns out to be
significant; i.e., the problem I've encountered goes away if I reference
the XSL files from the file system.

The problem is that within the XSL file I have a reference to
document(''), intending to "self reference" the stylesheet. As I
mentioned above, this works fine as long as the stylesheet is file based;
however when it is instead an embedded resource then the document('')
call returns an empty node set.

I'm guessing this has to do with the required resolver argument to the
Transform method of the XslTransform class. My code looks like:

XmlTextReader xslreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(reportTra nsformResource)));
....
xlt.Load(xslreader, null, typeof(ProcessModel).Assembly.Evidence);
....
XmlUrlResolver resolver = new XmlUrlResolver();
xlt.Transform(xpdoc.CreateNavigator(), null, xmlw, resolver);


XmlUrlResolver is just plain URL resolver and obviously can't resolve XSLT
embedded into an assembly. You need to create your own XmlResolver, which
resolves using GetManifestResourceStream() method.
Take a look at "Resolving the Unknown: Building Custom XmlResolvers in the
.NET Framework" article at
http://msdn.microsoft.com/xml/defaul.../cusxmlres.asp
for a sample.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #5
Bill Cohagan wrote:
OK, now I'm really confused. One of the problems I detected when I tried
to compile the code provided at the site is that it referenced the class URI
and I could only find a Uri class. That led in turn to an error on the
OriginalString reference since Uri has no such property.


Well, all you need is to invent your own URI schema and implement that
resolver. And because '' URI is relative one, you need to provide base
URI to your stylesheet. Here is a sketchy sample:

public class XmlResourceResolver : XmlUrlResolver
{
private Assembly assembly;

public XmlResourceResolver(Assembly a)
{
assembly = a;
}

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)
{
if (absoluteUri.Scheme == "res")
{
string res = absoluteUri.Query;
if (res.StartsWith("?"))
res = res.Substring(1);
return assembly.GetManifestResourceStream(res);
}
else
return base.GetEntity(absoluteUri, role, ofObjectToReturn);
}
}

And it's usage:

namespace Test
{
public class MyApp
{
public static void Main (string[] args)
{
XPathDocument doc = new XPathDocument("../../XmlFile1.xml");
XslTransform xslt = new XslTransform();
Assembly assembly = typeof(MyApp).Assembly;
XmlReader xsltReader =
new XmlTextReader("res://assembly?Test.XSLTFile1.xslt",
assembly.GetManifestResourceStream("Test.XSLTFile1 .xslt"));
xslt.Load(xsltReader);
xslt.Transform(doc, null, Console.Out,
new XmlResourceResolver(assembly));
}
}
}

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #6
Oleg
OK, I see how this works. The "trick" is to provide the base URI to the
XmlTextReader constructor in the form of a query so that the actual resource
name is preserved without being lower cased. I'll give this a try.

Thanks for the help!

Bill

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:Oc**************@TK2MSFTNGP12.phx.gbl...
Bill Cohagan wrote:
OK, now I'm really confused. One of the problems I detected when I
tried to compile the code provided at the site is that it referenced the
class URI and I could only find a Uri class. That led in turn to an error
on the OriginalString reference since Uri has no such property.


Well, all you need is to invent your own URI schema and implement that
resolver. And because '' URI is relative one, you need to provide base URI
to your stylesheet. Here is a sketchy sample:

public class XmlResourceResolver : XmlUrlResolver
{
private Assembly assembly;

public XmlResourceResolver(Assembly a)
{
assembly = a;
}

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)
{
if (absoluteUri.Scheme == "res")
{
string res = absoluteUri.Query;
if (res.StartsWith("?"))
res = res.Substring(1);
return assembly.GetManifestResourceStream(res);
}
else
return base.GetEntity(absoluteUri, role, ofObjectToReturn);
}
}

And it's usage:

namespace Test
{
public class MyApp
{
public static void Main (string[] args)
{
XPathDocument doc = new XPathDocument("../../XmlFile1.xml");
XslTransform xslt = new XslTransform();
Assembly assembly = typeof(MyApp).Assembly;
XmlReader xsltReader =
new XmlTextReader("res://assembly?Test.XSLTFile1.xslt",
assembly.GetManifestResourceStream("Test.XSLTFile1 .xslt"));
xslt.Load(xsltReader);
xslt.Transform(doc, null, Console.Out,
new XmlResourceResolver(assembly));
}
}
}

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Clifton | last post by:
Hello all, I am very new to this subject and learning by example. I have a small inconsistency in an XML file when transformed using different processors. I was wondering if someone could shed...
7
by: Peter | last post by:
I have noticed a difference in the processing of XSL transforms between dotnet and MSXML. Dotnet formats the resulting output differently, and since we use the <pre> tag in the HTML output it makes...
4
by: ashkaan57 | last post by:
Hi, I am using the following code to show/hide part of an html page. It works in Netscape and Firefox but dies in IE: "Error: document.layers is null or not an object" <style> ..noshow {...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
3
by: Jason S | last post by:
Hello Group, I am just about tearing my hair out with this one and thought someone may have some insight. I have a transform that wasn't working so I grabbed the nearest debugger (xselerator)...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
3
by: Peter Row | last post by:
Hi, I have 2 XML files and 1 XSLT file. The second XML file has the following declarative 1st line: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ....the 1st one (the one to be...
1
by: sudhaMurugesan | last post by:
Hi all, I am using VS2003. I am new to VS2003. I cannot transform an xml file into a string output in my windows application. But the same function works well with VS2005. it also asks for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.