13.9 Transforming XML Documents with XSLT

 <  Day Day Up  >  

You want to use Extensible Stylesheet Transformations ( XSLT ) to transform an XML document into a different format.


Technique

The XslTransform class implements XSLT 1.0 and allows you to transform the contents of an XML file into a different file format. This different file format itself doesn't have to be XML but can be any other text-based file format, such as HTML or XHTML. If you use the XslTransform class in version 1.0 of the .NET Framework, then you'll notice that it has undergone several design changes with regards to its API in version 1.1.

The main workhorse of this class is the Transform method. Most of the version 1.0 overloaded methods of this class have been marked as obsolete and replaced with new methods, each one differing in how the data is read and the object to use to write the resultant output.

One of these methods is a special case, which is discussed later in this section, so we look at the remaining eight methods first. Each Transform method requires either an XPathNavigator or an object that implements the IXPathNavigable interface. You can retrieve an XPathNavigator object by calling the CreateNavigator method defined in the XmlDocument class. However, the XmlDocument class also implements the IXPathNavigable interface, which means you are free to simply pass the XmlDocument object itself into the method.

The second parameter to the Transform method, which is the same in all overloaded versions, is an argument list whose data type is XsltArgumentList . XSLT can access arguments passed into it similar to the way an application is able to access command-line arguments. To add a new parameter to the argument list, call the AddParam method, passing the name of the parameter, an optional namespace URI or empty string, and the value of the parameter.

The third parameter is the object used to handle the resultant output from the XSLT processor. As with many file output scenarios, and as mentioned in the previous chapter, it can be a stream-based or file-based object.

Finally, the final parameter shared among the six overloaded methods is an XmlResolver object. You use this object whenever you make a resolution of a DTD, schema, or entity declaration. In most cases, you can simply pass null for this parameter. However, if the references are not local to the file being parsed (they exist on a secure server, for instance), then you need to create an XmlResolver object to supply the proper credentials to resolve the reference. The following code shows how to load an XML document, create an output file, and perform the XSLT transformation using the parameters explained so far:

 
 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("some.xml"); XslTransform xslt = new XslTransform(); xslt.Load("stylesheet.xsl"); XsltArgumentList args = new XsltArgumentList(); args.AddParam( "param", "", "paramValue" ); XmlTextWriter writer = new XmlTextWriter("output.html", null ); xslt.Transform(xmlDoc.CreateNavigator(), args, writer, null); 

The following code contains a lot of setup to finally get to the point of transformation. Fortunately, the Transform method also contains an overloaded version, the special case mentioned earlier, that simply expects two filenames passed as string objects and an XmlResolver object, which can be null:

 
 XslTransform xsl = new XslTransform(); xsl.Load( "stylesheet.xsl" ); xsl.Transform( xmlDoc, "output.html", null ); 

Comments

We are big fans of XSLT. Anytime we even start to think that we need to transform an XML document in an application, we immediately start thinking of XSLT templates. Sure, you could write a class that would take an XML document and transform it into something else, but you lose one of the major advantages that XSLT provides, simplicity. That's not saying, of course, that the XSLT language is something you can pick up in a day. It's merely stating that once you are familiar with what XSLT can do, you'll see that it can provide tremendous cost savings due to the quick turnaround time of creating an XSLT stylesheet.

We wish we could really get into some of the intricacies of XSLT and how to utilize the templates to do some amazing things, but just like most other XML-based technologies, it warrants an entire book of its own.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net