Inline XSL (and CSS definition, if possible) with-in an XML document

Hi-

Can you please advise me an example that demonstrates an XML document containing an XSL style sheet with-in the document My project requirements do not allow referencing Xsl as an external file.

Ideally I will need to have Xsl, Xml and CSS with-in an Xml document and stream to a literal or label (using ASP.NET 2.0) for formatting and display.

Please advice. Thanks in advance.

Regards,

Prabhu




Answer this question

Inline XSL (and CSS definition, if possible) with-in an XML document

  • .net sukbir

    You can imbed XSLT into XML by adding xsl:transform element as a node in your XML.

    To load this XSLT to XslCompiledTransform you would need to create XmlReader over the node with xsl:transform and pass this reader to XslCompiledTransform.Load().

    To create xml reader that starts with xsl:transform:

    1. if you have entire XML loaded (most likely) you can use method XPathNavigator.ReadSubtree(). (You'd need create XPathNavigator over your cache first -- XmlNode.CreateNavigator() or XPathDocument.CreateNavigator().)

    2. If you don't you can XmlReader.Create(_entire_XML_file) and then use Read() method up to the moment it reaches xsl:transform element, then pass this reader to XslCompiledTransform.Load(). It would read its part of XML and stop on end tag of xsl:transform or right after it so you would be able to continue reading the XML with this reader.

    Last method is more advanced and less tested. (There is known bug in it – all namespace used in the imbedded xsl should be defined inside xsl:transform element.)



  • Johnny Kauffman

    From the MSDN http://msdn2.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx :

    The XslCompiledTransform object is thread safe once it has been loaded. In other words, after the Load method has successfully completed, the Transform method can be called simultaneously from multiple threads.

    If the Load method is called again in one thread while the Transform method is being called in another thread, the XslCompiledTransform object finishes executing the Transform call by continuing to use the old state. The new state is used when the Load method successfully completes.

    Note:

    The Load method is not thread safe when called simultaneously from multiple threads.



  • venp

    Hi Sergey- This makes perfect sense. Thank you, again.

    Regards, Prabhu



  • DRoden

    Thank you, Sergey. This has been very helpful, especially the usage of XslCompiledTransform. Just one quick question- Is there any way to make "XslCompiledTransform" object thread safe Will this be a problem in using this object in a website the usually has 120 sessions (approx. average) any time during business hours

    Thanks, Prabhu



  • xird

    To make the xsl and the xml together, surely you have to know the contents of the xml. If you already know that, why don't you just embed a web page that was previously generated from the data
    Maybe I am missing why you would want to put them together.
    It can be done, but the reason you don't see many examples of how to do it is there isn't (generally) any point in doing extra processing at runtime for no benefit.


  • Inline XSL (and CSS definition, if possible) with-in an XML document