XML Examples

Hobbies Example

XML is there to display and manage the data, but it does not have any built-in functions such as Date() and Time() as seen with the ASP examples. This example will show some sample data - Hobby information - in XML format. The following will be saved as a file called Hobbies.xml. On the University systems, this should be saved under your My Webs folder on the Windows labs or under your public_html directory on the Unix servers. Comments are enclosed in <!-- --> brackets:

<?xml version="1.0"?>
<!-- Hobbies Database -->
<hobbies>
  <hobby id="1">
    <name>Running</name>
    <paragraph> Run every Saturday morning </paragraph>
  </hobby>
  <hobby id="2">
    <name>Swimming</name>
    <paragraph> Swim every Sunday morning </paragraph>
  </hobby>
  <hobby id="3">
    <name>Football</name>
    <paragraph> Support Stockport County! </paragraph>
  </hobby>
</hobbies>

Click here to see what it looks like. How it appears will depend on your browser - some show the XML tags, others do not.

Stylesheets

Ideally the data should be presented in a table format as seen with the ASP examples. Stylesheets, similar to cascading stylesheets (CSS) can be used to format the data into a more acceptable layout.

The following is a stylesheet, which will be saved as hobbies.xsl (note the different file extension):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<HEAD><TITLE>Hobbies</TITLE></HEAD>
<BODY>
<TABLE BORDER="1" BGCOLOR="cyan">
<THEAD><TR>
<TH>Hobby ID</TH>
<TH>Type</TH>
<TH>Information</TH></TR></THEAD>
<!-- insert each name and paragraph element value into a table row-->
<xsl:for-each select="hobbies/hobby">
<TR><TD>
<xsl:value-of select="@id"/></TD>
<TD><xsl:value-of select="name"/></TD>
<TD><xsl:value-of select="paragraph"/></TD></TR>
</xsl:for-each> </TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

The XML file needs to be amended to use this stylesheet, the example above needs the following after the first line:
<?xml-stylesheet type="text/xsl" href="hobbies.xsl"?>

That is the full text is now:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hobbies.xsl"?>
<!-- Hobbies Database -->
<hobbies>
  <hobby id="1">
    <name>Running</name>
    <paragraph> Run every Saturday morning </paragraph>
  </hobby>
  <hobby id="2">
    <name>Swimming</name>
    <paragraph> Swim every Sunday morning </paragraph>
  </hobby>
  <hobby id="3">
    <name>Football</name>
    <paragraph> Support Stockport County! </paragraph>
  </hobby>
</hobbies>

Click here to see the difference.