ARTICLE

Using XML and XSLT

Posted by Krishnan LN Articles | XML in VB.NET August 11, 2006
XML Stylesheet Transformation(XSLT) is defined as a language for converting XML documents to other document formats. This article shows how to perform the transformation using classes of .NET Framework.
Download Files:
 
Reader Level:

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 classes of .NET Framework.The class we are going to use are
XmlUrlResolver,XslTransform.

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 called xsltDoc. 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.

<?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>

                                    <br><xsl:value-of select="name"/></br>

                              </td>

                              <td>

                                    <br><xsl:value-of select="cost"/></br>

                              </td>

                        </tr>

                        </xsl:for-each>

                  </table>

            </xsl:template>

      </xsl:stylesheet>

Now let us look ata 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.

XslTransform Class Methods

  • Load()
    Loads an XSLT document. This method can accept an XmlReader, a document URL, or a variety of other objects.
  • Transform()
    The Transform() 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):
 
Now let us look at our code,

     Dim resolver As New XmlUrlResolver

        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        Dim xsltrans As New XslTransform

       xsltrans.Load(XsltFile, resolver)

        xsltrans.Transform(XmlFile, OutputFile, resolver)

When u look at the output,

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


So now having the knowledge of creating an XSLT document and perform the transformation, you can even create a webpage using an xml file.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).  

share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor