Wednesday, September 1, 2010

Using System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net





XSLTNamespace.aspx



<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
    string xmlFilePath = Request.PhysicalApplicationPath + @"App_Data\ITBookList.xml";
    string xsltFilePath = Request.PhysicalApplicationPath + @"App_Data\ITBookList.xslt";
    XPathDocument xPathDoc = new XPathDocument(xmlFilePath);
    XslCompiledTransform transform = new XslCompiledTransform();
    transform.Load(xsltFilePath);
    transform.Transform(xPathDoc, null, Response.Output);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Using System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>
   
ITBookList.xslt



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>XSLT document for display book list</title>
      </head>
      <body>
        <h2 style="color:DeepPink; font-style:italic;">How to use System.Xml, System.Xml.Xsl, System.Xml.XPath Namespace in asp.net</h2>
        <hr width="775" align="left" color="Pink" />
        <br />
        <table border="1" cellpadding="5" cellspacing="0" bordercolor="Orange">
          <tr bgcolor="OrangeRed" style="color:White; font-weight:bold">
            <td>Book ID</td>
            <td align="center">Name of Book</td>
            <td>Name of Author</td>
            <td>Book Price</td>
          </tr>
          <xsl:for-each select="books/book">
            <tr bgcolor="Snow" style="color:Crimson; font-weight:normal; font-style:italic;">
              <td height="10" style="font-weight:bold;" align="center">
                <xsl:value-of select="id"/>
              </td>
              <td height="10">
                <xsl:value-of select="name"/>
              </td>
              <td height="10">
                <xsl:value-of select="author"/>
              </td>
              <td height="10" style="font-weight:bold;" align="right">
                <xsl:value-of select="price"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
   
ITBookList.xml



<?xml version="1.0" ?>
<!-- Sample XML file for various XML, XSLT, XPath example-->
<books>
  <book Category="XML and XSLT">
    <id>1</id>
    <name>Beginning XML Databases</name>
    <author>Gavin Powell</author>
    <price>39.99</price>
  </book>
  <book Category="asp.net">
    <id>2</id>
    <name>ASP.NET 3.5 Website Programming: Problem - Design - Solution</name>
    <author>Chris Love</author>
    <price>44.99</price>
  </book>
  <book Category="asp.net">
    <id>3</id>
    <name>jQuery for ASP.NET Developers</name>
    <author>Joe Brinkman</author>
    <price>6.99</price>
  </book>
  <book Category="asp.net">
    <id>4</id>
    <name>ASP.NET MVC 1.0 Test Driven Development: Problem - Design - Solution</name>
    <author>Emad Ibrahim</author>
    <price>49.99</price>
  </book>
  <book Category="XML and XSLT">
    <id>5</id>
    <name>Beginning XSLT and XPath: Transforming XML Documents and Data</name>
    <author>Ian Williams</author>
    <price>49.99</price>
  </book>
  <book Category="XML and XSLT">
    <id>6</id>
    <name>XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition</name>
    <author>Michael Kay</author>
    <price>59.99</price>
  </book>
  <book Category="Database">
    <id>7</id>
    <name>Professional Microsoft SQL Server 2008 Programming</name>
    <author>Robert Vieira </author>
    <price>49.99</price>
  </book>
</books>
   


No comments: