[ Home | Syllabus | Course Notes | Assignments | Search]
Converting XML documents into HTML documents
Converting XML documents into other XML
documents
(conform to different schemas)
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>
<%@ 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've just found 10,000 ways that won'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'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
//// 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
F1: Start at the document root
F2: literal text that goes into the transformed output document
F3 Xpath expression which generates nodeset matching the Guitar Elements
F4: Write the transformed document to the browser client