Home Books Software Projects Forums

XSLT by Example

Creating XHTML compliant output.

Table of Contents

How do you specify that your XSLT output conforms to XHTML?

The following code shows how to indicate to the XSLT processor that you want your output to be XHTML conformant:

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

The xsl:output element is a top-level element which instructs the XSLT processor how to write the output stream to a serial output file.

  • The output method chosen is "xml" because XHTML is an XML grammar.
  • By selecting indent="yes", the output is neatly formatted with indentation to show the nesting of HTML elements within eachother.
  • The doctype-system attribute is used to produce the URL for the XHTML Transitional DTD in the resultant DOCTYPE tag in the output XHTML.

The following code is inserted in the output as a result of the xsl:output element:

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

References: [XSLT Programmer's Reference pp. 250-261]


What is XHTML and why is it important?

XHTML 1.0 is a reformulation of HTML 4 as an XML 1.0 compliant grammer.

The benefits of migrating HTML content to XHTML are related to the large number of tools available for developing and deploying XML compliant content.

  • An XML editor may be used to validate an XHTML document for conformance to the DTD's which specify XHTML.
  • An XSLT tool may be used to reformat an XHTML document.
  • A SAX parser may be used to write robots which read and interpret XHTML documents.
  • XHTML documents will display even in today's generation of browsers.

If you are developing XSLT applications which transform XML to HTML, it makes a lot of sense to be conformant with XHTML 1.0.


Who is advocating for HTML standards?

The Web Standards Project (WaSP) is a vocal advocacy for consistent HTML, CSS, and XML support in all browsers. Read their FAQ to understand why this is important for the web community.


What are the major differences between HTML and XHTML?

These are some of the core differences which distinguish XHTML from HTML:

  • All XHTML tags must be in lowercase.
  • All attribute values must be quoted.
  • All tags must be terminated.
  • All tags must be nested properly.

For a more detailed description of XHTML see the excellent webreview.com article, XHTML: Our last, best hope for clean code.


What is XHTML Transitional?

XHTML Transitional is a version of XHTML which allows you to employ styling in your HTML. In contrast, XHTML Strict requires all styling to be done using CSS. There are separate DTD's for each version. The DTD's enforce conformance to these rules. It is therefore easier to convert existing HTML to XHTML Transitional than to XHTML Strict.


How can you validate your XHTML?

There is an excellent validator at the World Wide Web Consortium (W3C) site which is available for free to validate your web pages for XHTML conformance.

If your pages are validated then you get to decorate them with the following XHTML certification seal:

Valid XHTML 1.0!

You also get to insert the following code in your XHTML to prove your certification to any skeptics. The code displays the certification seal as a button that your readers may press to invoke the validation program on the referring page (i.e. your XHTML page).

  <p>
    <a href="http://validator.w3.org/check/referer">
      <img src="http://www.w3.org/Icons/valid-xhtml10"
       alt="Valid XHTML 1.0!" height="31" width="88" />
    </a>
  </p>

What tools are available for XHTML development?

XML Spy 3.0 is a very popular XML editor which may be used to validate XHTML using the XHTML DTD's. In order to have XML Spy validate your XHTML, change the file suffix from '.html' to '.xml'. If you have set up your DOCTYPE as described above, XML Spy will read the DTD using the URL specified and use it to perform the validation.

HTML Tidy is a free, open-source utility which may be used to migrate existing HTML to XHTML. It is quite a powerful tool, comparable to a many-bladed Swiss Army knife because of the large number of configuration options it provides.


Where are the DTD's located for XHTML?

If you want to download the DTD's used to validate XHTML, you may find them using the following links:



Valid XHTML 1.0!