Dynamic XSL transformation

  • Thread starter Thread starter PHILIPPE
  • Start date Start date
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
 
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
 
If your XSLT stylesheet is in a string then you can do

trans.Load(new XmlTextReader(new StringReader(xsltString)));
 
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? :-)
 
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.
 
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
 
Back
Top