Dynamic XSL transformation

P

PHILIPPE

Hi,
I'm facing a annoying problem.
In the old ASP times, I was able to construct dynamically an XSL file
and then apply it to my XML file to create the HTML output.
Today, with the .NET framework, I can't find how to do this.
Of course, if my XSL file is already existing in my disk, it's easy
as:
/////
XPathDocument doc = new
XPathDocument(Server.MapPath("XML/Customers.xml"));
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("XSLT/Customers.xslt"));
StringWriter sw = new StringWriter();
trans.Transform(doc,null,sw,null);
this.txtStringBuilder.Text = sw.ToString();
sw.Close();
///
But how can I apply a transformation base on an XSL memory created
file?
Thx & regards


Phil
 
S

Sergey Dubinets

You can generate XSL in string and load XslTransform from this string.
string xslText = "<foo xsl:version='1.0' xmlns:xsl='...' />";
XslTransform trans = new XslTransform();
trans.Load(StreangReader(xslText));

Note: loading stylesheet is a expensive. So if performance is important in
this case, I'd recommend you to cache loaded stylesheets.

Sergey
 
S

SQL Server Development Team

If your XSLT stylesheet is in a string then you can do

trans.Load(new XmlTextReader(new StringReader(xsltString)));
 
A

Anders Borum

Hello!

Now that we're talking about the importance of caching XSLT stylesheets, I
was wondering if any of you know whether the XML control provided by ASP.NET
keeps an internal cache of the loaded XSLT stylesheet?

It seems like they're setting a cachedependency on the physical XSLT
stylesheet (file), but I haven't investigated this. An easy way to check
this could be to display all keys in the Cache .. and see if anything
changes!

Ideas? :)
 
O

Oleg Tkachenko

Anders said:
Now that we're talking about the importance of caching XSLT stylesheets, I
was wondering if any of you know whether the XML control provided by ASP.NET
keeps an internal cache of the loaded XSLT stylesheet?
Yes, it does.
It seems like they're setting a cachedependency on the physical XSLT
stylesheet (file), but I haven't investigated this. An easy way to check
this could be to display all keys in the Cache .. and see if anything
changes!
They cache both XML and XSL, but they cache them in the internal cache.
 
P

PHILIPPE

SQL Server Development Team said:
If your XSLT stylesheet is in a string then you can do

trans.Load(new XmlTextReader(new StringReader(xsltString)));


Thx Both, I'm going to give it a try.
Ciao
Phil
 

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

Similar Threads


Top