spacer
Yehuda Shiran December 28, 2001
Populating an HTML Template with XML Data
Tips: December 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

To convert an XML file to HTML, you start by writing the skeleton of the HTML file, specifying all tags such as <BODY> and <H1>. The content between each pair of tags is selected by XML directives. Take a look at the following line:

<H2><xsl:value-of select="//summary/subhead"/></H2> 
It is written within matching HTML brackets, and it includes one XML tag: xsl:value of. This XML tag has also a switch, select, by which it matches XML data. In the above example, it searches for the <summary><subhead> combination. Here is the top portion of the XML file:

<summary>
  <heading>MyDvd Rental Store on the Web</heading>
  <subhead>Periodical Sales Report</subhead>
  <description>Sales report for January, February, 
               and March of 2001</description>
</summary>
You can see that the value matching the above specification (//summary/subhead) is Periodical Sales Report. In a similar fashion, we find that //summary/heading is "MyDvd Rental Store", and that //summary/description is "Sales Report for January, February, and March of 2001". Here is the full XSLT file:

<?xml version="1.0"?>
<xsl:stylesheet 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE><xsl:value-of select="//summary/heading"/></TITLE>
</HEAD>
<BODY>
<H1><xsl:value-of select="//summary/heading"/></H1>
<H2><xsl:value-of select="//summary/subhead"/></H2>
<P><xsl:value-of select="//summary/description"/></P>    
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Load the XML and XSLT files onto your drive. Try reading the XML file in your browser. You will get the header of the sales report formatted and displayed like this:


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger