Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


XML Processing - Continued

XSL Transformations (XSLT)

Consider the XML file

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Guitars.xsl"?>
<Guitars>
  <Guitar>
    <Make>Gibson</Make>
    <Model>SG</Model>
    <Year>1977</Year>
    <Color>Tobacco Sunburst</Color>
    <Neck>Rosewood</Neck>
  </Guitar>
  <Guitar>
    <Make>Fender</Make>
    <Model>Stratocaster</Model>
    <Year>1990</Year>
    <Color>Black</Color>
    <Neck>Maple</Neck>
  </Guitar>
</Guitars>

Assuming you have both this file and the stylsheet in the same web directory, opening the above file will produce

HOW? see the following stylesheet

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/"> 
    <html> 
      <body>
        <h1>My Guitars</h1>
        <hr />
        <table width="100%" border="1">
          <tr bgcolor="gainsboro">
            <td><b>Make</b></td>
            <td><b>Model</b></td>
            <td><b>Year</b></td>
            <td><b>Color</b></td>
            <td><b>Neck</b></td>
          </tr>
          <xsl:for-each select="Guitars/Guitar">
            <tr>
              <td><xsl:value-of select="Make" /></td>
              <td><xsl:value-of select="Model" /></td>
              <td><xsl:value-of select="Year" /></td>
              <td><xsl:value-of select="Color" /></td>
              <td><xsl:value-of select="Neck" /></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Converting XML to HTML on the Server

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

<%
  XPathDocument doc =
      new XPathDocument (Server.MapPath ("Quotes.xml"));
  XslTransform xsl = new XslTransform ();
  xsl.Load (Server.MapPath ("Quotes.xsl"));
  xsl.Transform (doc, null, Response.OutputStream);
%>
//// here is the xml document
<?xml version="1.0"?>
<Quotes>
  <Quote>
    <Text>Give me chastity and continence, but not yet.</Text>
    <Author>Saint Augustine</Author>
  </Quote>
  <Quote>
    <Text>The use of COBOL cripples the mind; its teaching should 
      therefore be regarded as a criminal offense.</Text>
    <Author>Edsger Dijkstra</Author>
  </Quote>
  <Quote>
    <Text>C makes it easy to shoot yourself in the foot; C++ makes it 
      harder, but when you do, it blows away your whole leg.</Text>
    <Author>Bjarne Stroustrup</Author>
  </Quote>
  <Quote>
    <Text>A programmer is a device for turning coffee into code.</Text>
    <Author>Jeff Prosise (with an assist from Paul Erdos)</Author>
  </Quote>
  <Quote>
    <Text>I have not failed. I&apos;ve just found 10,000 ways that 
      won&apos;t work.</Text>
    <Author>Thomas Edison</Author>
  </Quote>
  <Quote>
    <Text>Blessed is the man who, having nothing to say, abstains from 
      giving wordy evidence of the fact.</Text>
    <Author>George Eliot</Author>
  </Quote>
  <Quote>
    <Text>I think there is a world market for maybe five 
      computers.</Text>
    <Author>Thomas Watson</Author>
  </Quote>
  <Quote>
    <Text>Computers in the future may weigh no more than 1.5 
      tons.</Text>
    <Author>Popular Mechanics</Author>
  </Quote>
  <Quote>
    <Text>I have traveled the length and breadth of this country and 
      talked with the best people, and I can assure you that data 
      processing is a fad that won&apos;t last out the year.</Text>
    <Author>Prentice-Hall business books editor</Author>
  </Quote>
  <Quote>
    <Text>640K ought to be enough for anybody.</Text>
    <Author>Bill Gates</Author>
  </Quote>
</Quotes>
------
Quotes.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1 style="background-color: teal; color: white;
           font-size: 24pt; text-align: center; letter-spacing: 1.0em">
          Famous Quotes
        </h1>
        <table border="1">
          <tr style="font-size: 12pt; font-family: verdana;
            font-weight: bold">
            <td style="text-align: center">Quote</td>
            <td style="text-align: center">Author</td>
          </tr>
          <xsl:for-each select="Quotes/Quote">
            <xsl:sort select="Author" />
            <tr style="font-size: 10pt; font-family: verdana">
              <td><xsl:value-of select="Text"/></td>
              <td><i><xsl:value-of select="Author"/></i></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
//// will produce the following

Transform.cs

//// useful utility for testing stylesheets

using System;
using System.Xml.XPath;
using System.Xml.Xsl;

class MyApp
{
    static void Main (string[] args)
    {
        if (args.Length < 2) {
            Console.WriteLine ("Syntax: TRANSFORM xmldoc xsldoc");
            return;
        }

        try {
            XPathDocument doc = new XPathDocument (args[0]);
            XslTransform xsl = new XslTransform ();
            xsl.Load (args[1]);
            xsl.Transform (doc, null, Console.Out);
        }
        catch (Exception ex) {
            Console.WriteLine (ex.Message);
        }
    }
}

 

 

 

using

Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.