XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

XSLT Processing in .NET
by Joe Feser | Pages: 1, 2

XSLT Transformations Involving Streams

It's also possible to output the results of a transformation directly to a System.IO.Stream object. In the following example, System.IO.MemoryStream will be used for output, and then a System.IO.StreamReader used to read the information back as a string and return the value from the function.

// Create the XslTransform.
System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
// Load the stylesheet.
xslt.Load("numbers.xsl");
// Load the XML data file.
System.Xml.XPath.XPathDocument doc = new 
  System.Xml.XPath.XPathDocument("numbers.xml");
// Create the Stream to place the output.
System.IO.Stream str = new System.IO.MemoryStream();
// Transform the file.
xslt.Transform(doc, null, str);
// Remember to Flush the Stream and set the position to 0
str.Flush();
str.Position = 0;

A System.Xml.XmlDocument or System.Xml.XPath.XPathDocument that is already loaded into memory is also on option for input. Both objects implement the System.Xml.XPath.IXPathNavigable interface, which allows each to be passed as parameters directly into the Transform method of the System.Xml.Xsl.XslTransform class.

// Create the XslTransform.
System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
// Load the stylesheet that creates XML Output.
xslt.Load("numbersXML.xsl");
// Load the XML data file into an XPathDocument.
System.Xml.XPath.XPathDocument doc = new 
  System.Xml.XPath.XPathDocument("numbers.xml");
// Create the Stream to place the output.
System.IO.Stream str = new System.IO.MemoryStream();
System.Xml.XmlWriter xw = new 
  System.Xml.XmlTextWriter(str,System.Text.Encoding.UTF8);
// Transform the file.
xslt.Transform(doc, null, xw);
// Flush the XmlWriter and set the position of the Stream to 0
xw.Flush();
str.Position = 0;

Stylesheet Parameters and Extension Objects

Parameters or extension objects may also be passed to the stylesheet. This is accomplished using the System.Xml.Xsl.XsltArgumentList class. Passing a parameter to a stylesheet gives the programmer the ability to initialize a globally scoped variable, which is defined as any xsl:variable that is a child of the xsl:stylesheet and not contained inside a xsl:template. A parameter may be added to the System.Xml.Xsl.XsltArgumentList class by calling the AddParam method providing a qualified name, the namespace URI and value. If the parameter value is not a String, Boolean, Number, Node Fragment, or NodeSet, it will be forced to a double or string. An extension object is any .NET class that provides methods that return an XSLT data type. Extension objects are added to the System.Xml.Xsl.XsltArgumentList using the AddExtensionObject method providing a qualified name and namespace URI.

// Using the TextReader, load it into an XPathDocument
System.Xml.XPath.XPathDocument xp = new 
  System.Xml.XPath.XPathDocument("numbers.xml");
// Create a new XslTransform class and load the stylesheet
System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform();
// Load the XmlReader StyleSheet into the Transformation
trans.Load("numbersExtension.xsl");

// Create the XsltArgumentList class and add the 
// parameter using AddParam
System.Xml.Xsl.XsltArgumentList xslArg = new 
  System.Xml.Xsl.XsltArgumentList();
xslArg.AddParam("displayme","","Is this fun?");

// Create and add the extension object using AddExtensionObject
SayHello hi = new SayHello();
xslArg.AddExtensionObject("urn:SayHello",hi);

// Create the System.IO.Stream to place the output.
System.IO.Stream str = new System.IO.MemoryStream();

// Transform the file.
trans.Transform(xp, xslArg, str);

// Flush the XmlWriter and set the position of the Stream to 0
str.Flush();
str.Position = 0;

// Create a StreamReader to Read the Stream and Return the String
System.IO.StreamReader sr = new System.IO.StreamReader(str);
string xmlOut = sr.ReadToEnd();
// Close the StreamReader and the base Stream
sr.Close();
// Write the Results to the Console
Console.Write(xmlOut);

// Extension Object to return Hello Name
public class SayHello
{
  public string HelloName(string name) {
    return "Hello " + name;
}
}

There are advantages to using an extension object instead of embedding all the instructions within a script block, including better reuse of the classes and smaller, easier to maintain stylesheets.

Embedding Script or Code in XSLT

Programming and scripting language constructs may also be embedded and utilized in XSLT stylesheets by using the msxsl:script element. The prefix "msxsl" is assumed to be bound to the urn:schemas-microsoft-com:xslt namespace. Languages supported by the script tag include C#, VB.NET, and JScript.NET, which is the default. An implements-prefix attribute that contains the prefix representing the namespace associated with the script block must also exist in the msxsl:script element. Multiple script blocks may exist in a stylesheet, but only one language may be used per namespace.

<xsl:stylesheet version='1.0' 
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
     xmlns:msxsl='urn:schemas-microsoft-com:xslt'
     xmlns:theScript='urn:CustomScript'>
  <xsl:output omit-xml-declaration='yes' method='text'
       media-type='text/plain' indent='no' />
  <xsl:variable name='displayme' />
 
  <msxsl:script implements-prefix='theScript' language='C#'>
  <![CDATA[
  public string HelloName(string name)
  {
    return "Hello " + name;
  }
  ]]>
  </msxsl:script>
  
  <xsl:template match='/'>
    <xsl:text disable-output-escaping='yes'>Print Integers > 3</xsl:text> 
    <xsl:apply-templates select='Root/Numbers' />
    Script Result: <xsl:value-of select='theScript:HelloName("Joe")' />
    Done: <xsl:value-of select='$displayme' />
  </xsl:template>
  
  <xsl:template match='Numbers'>
    Numbers:<xsl:apply-templates select='Integer[@value > 3]' />
  </xsl:template>
  
  <xsl:template match='Integer'>
    Integer: <xsl:value-of select='@value' />
  </xsl:template>
</xsl:stylesheet>

Resources

Acknowledgments:

I would like to thank Asad Jawahar, Arpan Desai and Praj Joshi for their help in developing this article.



1 to 3 of 3
  1. XslTransform.Transform Method doesn't work when xsl contains
    2003-06-18 08:58:59 Tanya Gillingham
  2. Using the XmlNavigator
    2002-10-30 08:26:24 Steve Skalski
  3. XSL Transforms and .NET DataSets
    2002-10-29 17:33:17 Steve Skalski
1 to 3 of 3