原文来自:http://ieproxy.com/

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
="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