Creating a Simple HTML Page

It is traditional in many introductory programming texts to create a program that provides a “Hello World!” greeting.

Listing 10.1 shows a very short XML document that stores the message.

Listing 10.1. XSLTMessage.xml: A Simple Message in XML
<?xml version='1.0'?> 
<XSLTMessage> 
Hello World! 
</XSLTMessage> 

You can use the XSLT stylesheet shown in Listing 10.2 to create an HTML Web page that extracts the message from Listing 10.1 and places it in the HTML page.

Listing 10.2. XSLTMessage.xsl: A Stylesheet to Extract the Message from Listing 10.1
<?xml version='1.0'?> 
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        version="1.0" 
  > 

<xsl:template match="/"> 
<html> 
<head> 
<title><xsl:value-of select="name(/XSLTMessage)" /></title> 
</head> 
<body> 
<xsl:apply-templates select="/XSLTMessage" /> 
</body> 
</html> 
</xsl:template> 

<xsl:template match="XSLTMessage"> 
<p><xsl:value-of select="text()" /></p> 
</xsl:template> 
</xsl:stylesheet> 

Before looking at the HTML output document that you can create, let’s analyze what the XSLT stylesheet does.

The first line is an XML declaration. That is followed by the xsl:stylesheet element. It has a version attribute, which is compulsory, and a namespace declaration that associates the namespace prefix xsl with the XSLT namespace URI, http://www.w3.org/1999/XSL/Transform. So, it is clear that the xsl:stylesheet element and the other elements in the stylesheet with a namespace prefix xsl are XSLT elements.

When an XSLT processor is satisfied that a document is a well-formed XSLT stylesheet, it looks for an xsl:template element whose match attribute has a value of /. In other words, the template is applied to the root node.

Let’s look at the content of that xsl:template element a little more closely:

<xsl:template match="/"> 
<html> 
<head> 
<title><xsl:value-of select="name(/XSLTMessage)" /></title> 
</head> 
<body> 
<xsl:apply-templates select="/XSLTMessage" /> 
</body> 
</html> 
</xsl:template> 

The first two lines nested within the xsl:template element contain literal result elements. In other words, you create an html start tag followed by head and title start tags. The content of the title element is defined using an XSLT xsl:value-of element, which you will use in many of your XSLT stylesheets. One question immediately arises: “The value of what?” That is answered by the value of the select attribute of the xsl:value-of element.

In this case, the value of the select attribute is this:

name(/XSLTMessage) 

You use the name() function to extract the name of the document element. We have expressed that here by giving the element type name literally.

You then create the end tags for the title and head elements. Then you create the start tag for the body element.

The content of the body element is defined by the xsl:apply-templates element. Its select attribute indicates that a template that matches the context node defined by the XPath expression /XSLTMessage is to be instantiated.

<xsl:template match="XSLTMessage"> 
<p><xsl:value-of select="text()" /></p> 
</xsl:template> 

The only other template in the stylesheet matches the value in the select attribute of the xsl:apply-templates element. The content of that template defines the content of the body element of the HTML output document.

That content begins with a p start tag. It is followed by content defined by an xsl:value-of element. The value of the select attribute is text(); it selects the text node that is a child of the context node, which is the XSLTMessage element node. The template is completed by the creation of an end tag of the p element.

When that template is complete, you return to the template from which the template was instantiated. In other words, processing goes back to the template that matches the root node. Processing completes by outputting an end tag for the body and html elements—both literal result elements— of the HTML output document.

To run the transformation, assuming that Instant Saxon and Listings 10.1 and 10.2 are in the same directory, simply navigate to that directory and type this:

saxon XSLTMessage.xml XSLTMessage.xsl > XSLTMessage.html 

Listing 10.3 shows the HTML document output by the Instant Saxon XSLT processor.

Listing 10.3. XSLTMessage.html: The Output of Applying Listing 10.2 to Listing 10.1
<html> 
 <head> 
  <meta http-equiv="Content-Type" content="text/html; 
   charset=utf-8"> 
  <title>XSLTMessage</title> 
 </head> 
 <body> 
  <p> 
   Hello World! 
  </p> 
 </body> 
</html> 

The Instant Saxon processor has added a meta element to the head of the HTML document.

If you plan to regularly create HTML documents using XSLT, it is useful to have an XSLT template specific to HTML and save some repeated typing. Listing 10.4 gives a bare outline template that you might want to adapt to your own needs.

Listing 10.4. HTMLTemplate.xsl: An XSLT Stylesheet to Create a Basic HTML Document
<?xml version='1.0'?> 
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  > 
<xsl:output method="html" 
 indent="yes" /> 
<xsl:template match="/"> 
<html> 
<head> 
<title><!--title goes here--></title> 
</head> 
<body> 
<!-- XSLT code to create page content goes here. --> 
</body> 
</html> 
</xsl:template> 

</xsl:stylesheet> 

If you routinely want to include metadata about the author of the HTML document, its keywords, and so on, you can add those to the section that creates the head of the HTML document.

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

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