Monday, January 26, 2009

ASP.NET: XML and XSLT

In this simple tutorial, I'm going to demonstrate how to load data from XML and transform it into HTML Format.

Introduction

What is XML?


XML Stands for eXtensible Markup Language. It is a generic data format widely used for sharing information over the internet.

<?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<title>Harry Potter</title>
<author>J. K. Rowling and Mary GrandPré</author>
<publisher>Hardcover</publisher>
</book>
<book>
<title>The Curious Case of Benjamin Button</title>
<author>F. Scott Fitzgerald</author>
<publisher>Paperback</publisher>
</book>

</books>

Figure 1. XML

What is XSL?

XSL Stands for eXtensible Stylesheet Language. Similar to CSS (Cascading Style Sheet), XSL is a language used to create stylesheets for XML.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:template match="Books">
<html>
<body>
<ul>
<xsl:value-of select="Title" />
<xsl:value-of select="Author" />
<xsl:value-of select="Publisher" />
<xsl:apply-templates/>
</ul>
</body>
</html>
</xsl:template>

<xsl:template match="Book">
<li>
<b><xsl:value-of select="Title" /></b>
<br/>
<xsl:value-of select="Author" />
<br/>
<xsl:value-of select="Publisher" />
</li>
</xsl:template>
</xsl:stylesheet>

Figure 2. XSLT


Applying the XSL stylesheet into the XML

XSL is useless if it is not applied into your XML. XSL association might look like this:

<?xml-stylesheet type="text/xsl" href="Books.xslt"?>

Your XML will looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Books.xslt"?>

<Books>
<Book>
<Title>Harry Potter</Title>
<Author>J. K. Rowling and Mary GrandPré</Author>
<Publisher>Hardcover</Publisher>
</Book>
<Book>
<Title>The Curious Case of Benjamin Button</Title>
<Author>F. Scott Fitzgerald</Author>
<Publisher>Paperback</Publisher>
</Book>

</Books>

Figure 3. XSL-Associated XML

Bring-On the ASP.NET

Now that we have created the XML and associated the XSL on it, Its time to create our asp page.
Here's the step by step on how to do it:

1. In you Visual Studio, Create a new project (ASP.NET Web Application).
2. Add new item and select XML. Copy and paste the code on Figure 3.
3. Add new item and select XSLT. Copy and paste the code on Figure 2.
4. Open the design view of the Default.aspx page.
5. Drag an xml control onto the page.
6. On your code behind add the following code in your page load event.
XmlDocument docXML = new XmlDocument();
XslTransform docXSL = new XslTransform();
docXML.Load(Server.MapPath("Books.xml"));
docXSL.Load(Server.MapPath("Books.xslt"));
this.Xml1.Document = docXML;
this.Xml1.Transform = docXSL;
Note: Don't forget to import the System.Xml and the System.Xml.Xsl Namespaces.

7. Run the project.

And finally, here is the output:

  • Harry Potter
    J. K. Rowling and Mary GrandPré
    Hardcover
  • The Curious Case of Benjamin Button
    F. Scott Fitzgerald
    Paperback