XSLT Processing in .NET
This article is meant to help XML developers understand the different
ways XSLT transformations can be performed using the .NET framework. It
alsos describe how to use various input sources for an XSLT
transformation. In .NET, the System.Xml.Xsl.XslTransform
class is used for transforming XML data using an XSLT stylesheet.
System.Xml.Xsl.XslTransform supports the XSLT 1.0 syntax,
using the http://www.w3.org/1999/XSL/Transform namespace.
Input and Output Data Sources to the XslTransform Class
|
Related Reading
XSLT |
There are several classes that may be used to read XML and XSLT
documents for a transformation. The most versatile of these is the
System.Xml.XmlReader class. Since
System.Xml.XmlReader is an abstract class, another class must
inherit from it. The first class of this type is
System.Xml.XmlTextReader, which reads character streams and
checks that XML is well-formed, but does not validate the XML against a
DTD or schema.
The second class that inherits from System.Xml.XmlReader
is System.Xml.XmlNodeReader, which allows data to be read
from any XML Document Object Model (DOM) API; for example,
System.Xml.XmlNode. The System.Xml.XmlNode does
not have to be a complete XML document; it may be a child node.
The third type of XmlReader class is
System.Xml.XmlValidatingReader, a validating reader capable
of ensuring XML data conforms to a DTD, XML-Data Reduced (XDR) schema, or
W3C XML Schema.
// Load the String into a TextReader
System.IO.TextReader tr = new System.IO.StreamReader("numbers.xsl");
// Use that TextReader as the Source for the XmlTextReader
System.Xml.XmlReader xr = new System.Xml.XmlTextReader(tr);
// Create a new XslTransform class
System.Xml.Xsl.XslTransform trans = new System.Xml.Xsl.XslTransform();
// Load the XmlReader StyleSheet into the XslTransform class
trans.Load(xr);
Another type of input class is
System.Xml.XPath.XPathNavigator or any class that implements
the System.Xml.XPath.IXPathNavigable interface, including
System.Xml.XPath.XPathDocument,
System.Xml.XmlDocument, and XMLDataDocument. The
System.Xml.XPath.XPathNavigator is based on the XPath data
model and provides the methods needed to implement XPath queries over any
data store.
The System.Xml.XPath.XPathDocument is the fastest of such
classes, because it's read only, and is the preferred class when the speed
of XSLT transformations is the highest priority.
System.Xml.XmlDocument is the second most efficient class;
XMLDataDocument is not recommended for performing XSLT
transformations. System.Xml.XPath.IXPathNavigable may be
implemented against any data source, allowing any imaginable type of data
to be used as the source in an XSLT transformation.
// The following code may be appended to the preceding example
// Create a new XPathDocument, loading the source from a file
System.Xml.XPath.XPathDocument xp = new
System.Xml.XPath.XPathDocument("numbers.xml");
// Create the Navigator
System.Xml.XPath.XPathNavigator xpn = xp.CreateNavigator();
The XslTransform class requires the source of the XML or
XSLT data to be an XmlReader or
System.Xml.XPath.XPathNavigator. The Load and
Transform methods of the
System.Xml.Xsl.XslTransform class do not provide an
overloaded method for directly processing a Stream, but the
example below demonstrates an indirect method to use a
MemoryStream as the source location of the XML data and XSLT
stylesheet.
// Create a Stream and show how it would be used as the source
// This is only a sample since no data exists in the stream.
System.IO.Stream st = new System.IO.MemoryStream();
// You would populate the Stream with the XML File here and set the position to 0
st.Position = 0;
// Using the Stream, load it into an XPathDocument
System.Xml.XPath.XPathDocument xp = new System.Xml.XPath.XPathDocument(st);
// Now do the same with the Xslt document
System.IO.Stream XSLTStream = new System.IO.MemoryStream();
// You would populate the Stream with the XSLT File here and set the position to 0
st.Position = 0;
// Use an XmlReader and pass in a Stream as the Source
System.Xml.XmlReader xsltXR = new System.Xml.XmlTextReader(XSLTStream);
// The XmlReader may be passed into the Load Method of XslTransformm
The line containing the initialization of the Stream is
just to demonstrate the MemoryStream constructor; it is still
necessary to populate the data. One other source we may wish to use for
XML and XSLT documents is a document retrieved from a URL, in the form of
a string along with an optional System.Xml.XmlResolver, which
is the most efficient method to load the source from an existing file.
The xsl:output element is sometimes ignored depending on
the type of output class used; if, for example, the output class is either
an XmlWriter or an XmlReader class. For a
detailed description of which attributes are supported on
xsl:output and the classes that support them please read the
"Outputs
from an XslTransform" section of the MSDN documentation.
The minimum requirements to perform an XSLT transformation are an XML
document, an XSLT document, and a valid object to handle the output. In
the example below, an XPathDocument and stylesheet will be
loaded from a file, and output directed to the system console.
// 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 XmlTextWriter to output to the console.
System.Xml.XmlTextWriter writer = new
System.Xml.XmlTextWriter(System.Console.Out);
// Transform the file.
xslt.Transform(doc, null, writer);
// Be careful when using the console,
// closing it will not allow you to write any
// additional information to it
writer.Close();
Pages: 1, 2 |
