Listing the recipients of a memo

In order to generate a XHTML list of all <memo> recipients of a memo we have to use <xsl:output method="xhtml"> and embed the required HTML tags in our XSL style sheet:

<xsl:output method="xhtml" indent="yes"/>

<xsl:template match="/memo">
  <html>
    <head>
      <title>Recipient list</title>
    </head>
    <body>
      <ul>
        <xsl:apply-templates select="to"/>
      </ul>
    </body>
  </html>
</xsl:template>

<xsl:template match="to">
  <li>
    <xsl:value-of select="."/>
  </li>
</xsl:template>

Processing this style sheet for a <memo> document instance yields:

<?xml version="1.0" encoding="UTF-8"?>
<html>
   <head>
      <title>Recipient list</title>
   </head>
   <body>
      <ul>
         <li>Adam Hacker</li>
         <li>Eve intruder</li>
      </ul>
   </body>
</html>

The generated Xhtml code does not contain a reference to a DTD. We may supply this reference by modifying our <xsl:output/> directive:

<xsl:output method="xhtml" indent="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

This adds a corresponding header which allows to validate the generated HTML:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head> ...

This may be improved further by instructing the XSL formatter to use http://www.w3.org/1999/xhtml as default namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="xhtml" indent="yes"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

    <xsl:template match="/">
        <html><head> ...
     </xsl:template>
...
</xsl:stylesheet>

This yields the following output::

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
   <head> ...
</html>

The top level element html is now declared to belong to the namespace xmlns="http://www.w3.org/1999/xhtml. This will be inherited by all inner Xhtml elements.

exercise No. 55

Transforming book instances to Xhtml Create comment in forum

Q:

Create a XSL style sheet to transform instances of the first version of book.xsd (A schema for editing books ) into Xhtml 1.0 strict.

You should first construct a Xhtml document manually before coding the XSL. After you have a working Xhtml example document create a XSL style sheet which transforms arbitrary book.xsd document instances into a corresponding Xhtml file.

A:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:output indent="yes" method="xhtml"/>

  <xsl:template match="/book">
    <html>
      <head>
        <title><xsl:value-of select="title"/></title>
      </head>
      <body>
        <h1><xsl:value-of select="title"/></h1>
        <xsl:apply-templates select="chapter"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="chapter">
    <h2><xsl:value-of select="title"/></h2>
    <xsl:apply-templates select="para"/>
  </xsl:template>

  <xsl:template match="para">
    <p><xsl:value-of select="."/></p>
  </xsl:template>

</xsl:stylesheet>