Library code snippets

Applying XSL transformations to XML in .NET

XML Stylesheet Transformation(XSLT) is defined as a language for converting xml documents to other document formats. XSLT processors parse the input XML document, as well as the XSLT stylesheet and then process the instructions found in the XSLT stylesheet, using the elements from input XML document. During the processing of the XSLT instructions, a structured XML output is created.

We are going to perform the transformation using the XmlUrlResolver, XSLTransform classes from the .NET Framework. The XslTransform class, found in the System.Xml.Xsl namespace, is the XSLT processor that implements the XSLT version 1.0 recommendation.

In order to perform a transformation using XslTransform, first create an XslTransform object and load it with the desired XSLT document (by calling Load). The Load method of XslTransform class loads the XSL Stylesheet from disk. Finally, the transform is executed by calling Transform method of the XslTransform class object.

Let us look this with an example.

Products.xml

<?xml version="1.0" encoding="utf-8" ?>
<Products>
  <product>
    <name>Cereal Milk</name>
    <cost>R.S 100</cost>
  </product>
  <product>
    <name>Badam Pista</name>
    <cost>R.S 230</cost>
  </product>
</Products>

We are now going to frame an XSLT document for the above XML.

Products.xsl

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <table border="1">
      <tr>
        <th>Name</th>
        <th>Price</th>
      </tr>
      <xsl:for-each select="Products/product">
        <tr>
          <td><xsl:value-of select="name"/></td>
          <td><xsl:value-of select="cost"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Now let us look at the transformation part. We are using the XmlUrlResolver and XslTransform class to perform the transformation. XmlUrlResolver class is used to resolve external XML resources such as entities, document type definitions (DTDs), or schemas. It is also used to process include and import elements found in Extensible StyleSheet Language (XSL) stylesheets or XML Schema Definition language (XSD) schemas.
 
Now let us look at our code,

XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials; 
XslTransform xsltrans=new XslTransform(); 
xsltrans.Load(XsltFile,resolver); 
xsltrans.Transform(XmlFile,OutputFile,resolver); 

We use the Load method that loads an XSLT document. This method can accept an XmlReader, a document URL, or a variety of other objects. We then call the Transform method. This method is overloaded and can therefore accept a variety of parameters. The most common form of the method that you'll likely use in your ASP.NET applications is shown next (check the .NET SDK for the other overloaded versions of the method):

The output will look something like this.

Name Price
Cereal Milk R.S 100
Badam Pista R.S 230

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Applying XSL transformations to XML in .NET.

Leave a comment

Sign in or Join us (it's free).

 Globalking
AddThis

Related discussion

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

Events coming up

  • Aug 28

    St. Louis Day of .NET

    St. Charles, United States

    Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend

Want to stay in touch with what's going on? Follow us on twitter!