Creating an HTML List

This section looks at how to create an HTML list from an XML source document.

The XML source document contains information about a series of reports produced for XMML.com. In this HTML Web page, you will choose to display only reports for which the year of the report is 2002. Listing 10.5 shows the source XML document.

Listing 10.5. XMMLReports.xml: Reports Presented to XMML.com
<?xml version='1.0'?> 
<XMMLReports> 
<Report year="2000"> 
<Title>Sales Opportunities</Title> 
<Author>Peter Mallan</Author> 
<Summary>The opportunities for XML consultancy look good for 
 2001.</Summary> 
<Content> 
<!-- Main text would go here. --> 
</Content> 
</Report> 
<Report year="2001"> 
<Title>SVG - A Graphics Standard</Title> 
<Author>Pamela Askew</Author> 
<Summary>Scalable Vector Graphics, SVG, looks to have enormous 
 potential in multinamespace XML documents.</Summary> 
<Content> 
<!-- Main text would go here. --> 
</Content> 
</Report> 
<Report year="2002"> 
<Title>Market Conditions</Title> 
<Author>Stephen J. Doppelganger</Author> 
<Summary>Market conditions are much less favorable than in 
 2001.</Summary> 
<Content> 
<!-- Main text would go here. --> 
</Content> 
</Report> 
<Report year="2002"> 
<Title>XML Schema Languages</Title> 
<Author>Karen Clark</Author> 
<Summary>W3C XML Schema and RelaxNG both have positive 
 aspects.</Summary> 
<Content> 
<!-- Main text would go here. --> 
</Content> 
</Report> 
</XMMLReports> 

Listing 10.6 shows the XSLT stylesheet that selects for display reports dated 2002 and displays the title, year, and author name for each such report.

Listing 10.6. XMMLReports.xsl: A Stylesheet to Display Year 2002 Reports
<?xml version='1.0'?> 
<xsl:stylesheet 
 version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  > 
<xsl:output method="html" 
 indent="yes" /> 
<xsl:template match="/"> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<h1>XMML.com Reports for 2002</h1> 
<ul> 
<xsl:apply-templates select="//Report[@year='2002']" /> 
</ul> 
</body> 
</html> 
</xsl:template> 

<xsl:template match="Report"> 
<li> 
<xsl:value-of select="Title" />(<xsl:value-of 
  select="@year"/>):<xsl:text> 
</xsl:text><xsl:value-of select="Author" /> 
</li> 
</xsl:template> 

</xsl:stylesheet> 

The preceding stylesheet bears many similarities to those you saw earlier in this chapter.

Notice the xsl:apply-templates element in the template that matches the root node. It is nested between the start and end tags of a ul element. So, the xsl:apply-templates element is used to create the content of the unordered list. The value of its select attribute is //Report[@year='2002']. The pair of forward slash characters (//) is abbreviated syntax for the descendant-or-self axis. Essentially, //Report means to select any Report element node in the document. The predicate [@year='2002'] filters that node-set so that it includes only Report element nodes that possess a year attribute whose value is 2002.

The XSLT template that matches the nodes in that node-set is then processed. The xsl:template element with the match attribute whose value is Report is instantiated. A list item element, li, is created. The xsl:value-of element is used three times:

<xsl:value-of select="Title" />(<xsl:value-of 
  select="@year"/>):<xsl:text> </xsl:text> 
<xsl:value-of select="Author" /> 

First, the text content of the Title element is obtained, followed by the value of the year attribute of the Report element in parentheses. The third xsl:value-of element selects the text content of the Author element node for the report.

The xsl:text element is used to insert whitespace—in this case, a single space character—between the colon character and the author’s name. Using the xsl:text element to output whitespace ensures that it is preserved in the output document.

The output of the transformation is shown in Listing 10.7.

Listing 10.7. XMMLReports.html: The Output of Applying Listing 10.6 to Listing 10.5
<html> 
 <head> 
  <meta http-equiv="Content-Type" content="text/html; 
   charset=utf-8"> 
  <title></title> 
 </head> 
 <body> 
  <h1>XMML.com Reports for 2002</h1> 
   <ul> 
    <li>Market Conditions(2002): Stephen J. Doppelganger</li> 
    <li>XML Schema Languages(2002): Karen Clark</li> 
   </ul> 
 </body> 
</html> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset