Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic xslt and transform with-out System.IO

3 views
Skip to first unread message

schneider

unread,
Dec 15, 2004, 5:57:26 AM12/15/04
to
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 transform via code?

Any examples would be great.

Thanks,

Schneider


Oleg Tkachenko [MVP]

unread,
Dec 15, 2004, 6:08:27 AM12/15/04
to
schneider wrote:

> 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 transform via code?

Basically any XSLT stylesheet is just XML document, so of course you can
build it in-memory using XmlDocument. Another common approach is to
generate XSLT stylesheets on the fly using XSLT (again - as XSLT is XML,
the best way to generate/transform it is XSLT itself).

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

schneider

unread,
Dec 15, 2004, 4:15:21 PM12/15/04
to
So you are saying to create a xslt with a XmlDocument object?

I still don't see any way to create a XslTransform from a XmlDocument?

Have any sample code?

Thanks,

Schneider

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OfrMnap4...@TK2MSFTNGP09.phx.gbl...

Dennis Myrén

unread,
Dec 16, 2004, 4:17:44 AM12/16/04
to
You would have to first load the XSLT document into an XmlDocument object.
XmlDocument xsltDoc = new XmlDocument();
xsltDoc.LoadXml(xsltString);
Then, create an XslTransform and, now, load the XSLT document into the
transform by creating an XPathNavigator from the document:
XslTransform transform = new XslTransform();
transform.Load(xsltDoc.CreateNavigator(), null, null);
Assuming you have the XML document loaded into a XmlDocument as well,
you are now ready to perform the transformation.
Let's use a System.IO.MemoryStream as the output channel.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XsltArgumentList args = new XsltArgumentList();
MemoryStream output = new MemoryStream(100000);
// Put any XSLT transform parameters into 'args' here.
transform.Transform
(xmlDoc.CreateNavigator(), args, output, null);

All this can be done in RAM without a single read/write to the hard drive.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"schneider" <00...@0000.SPAM> wrote in message
news:eFYDvpu4...@TK2MSFTNGP15.phx.gbl...

Oleg Tkachenko [MVP]

unread,
Dec 16, 2004, 7:56:57 AM12/16/04
to
schneider wrote:
> So you are saying to create a xslt with a XmlDocument object?
>
> I still don't see any way to create a XslTransform from a XmlDocument?

Hmmm, I really wonder why... Have you looked at XslTransform.Load() methods?
There is probably something wrong with the API as you (and you arent'
the first one) can't see that XmlDocument implements IXPathNavigable and
you can just pass XmlDocument instance into the XslTransform.Load() method.

It's as as simple as:

XslTransform xslt = new XslTransform();
xslt.Load(doc);

In .NET 1.1 you'll get compiler warning that you have to pass an
evidence, so use
xslt.Load(doc, null, null);

or

xslt.Load(doc, new XmlUrlResolver(),
Assembly.GetExecutingAssembly().Evidence);

if you need a URI resolving during stylesheet's load (e.g. you are using
xsl:include/xsl:import).

0 new messages