Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
  • 💬 Discuss
From the author of

From the author of

Controlling Output Type

A lot of the examples in this chapter have converted XML into HTML, and you might have wondered how an XSLT processor knows to omit the <?xml?> declaration from the beginning of such output documents. It turns out that there’s a special rule here: If the document node of the output document is <HTML>, the XSLT processor knows that the output document type is HTML and writes the document accordingly.

In fact, you can specify three types of output documents:

  • XML. This is the default, and such documents start with an <?xml?> declaration. In addition, entity references will not be replaced with characters such as < or & in the output document; the actual entity reference will appear in the output.

  • HTML. This is standard HTML 4.0, without a XML declaration or any need to close elements that don’t normally have a closing tag in HTML 4.0. Empty elements can end with >, not />, and < and & characters in text are not escaped with the corresponding character entity references.

  • Text. This type of output represents pure text. In this case, the output document is simply the plain text of the document tree.

You can set the output method by setting the <xsl:output> element’s method attribute to "xml", "html", or "text". For example, if you want to create an HTML document, even though the root element is not <HTML>, you can use this <xsl:output> element:

<xsl:output method = "html"/>

Another useful attribute of <xsl:output> is the indent attribute, which enables the XSLT processor (but does not force it) to insert whitespace to indent the output. Here’s how you can use this attribute:

<xsl:output indent = "yes"/>

This next table shows some <xsl:output> attributes that you can use to create or modify XML declarations:

    Attribute

    Description

    encoding

    Specifies the value for the XML declaration’s encoding attribute.

    omit-xml-declaration

    Specifies whether the processor should omit the XML declaration. Set this to yes or no.

    Standalone

    Specifies the value for the XML declaration’s standalone attribute. Set this to yes or no.

    Version

    Specifies the value for the XML declaration’s version attribute.

Another useful attribute of <xsl:output> is media-type, which enables you to specify the MIME type of the output document. Here’s an example:

<xsl:output media-type="text/xml"/>

You can also use the <xsl:output> doctype-system and doctype-public attributes to specify an external DTD. For example, take a look at the following <xsl:output> element:

<xsl:output doctype-system = "planets.dtd"/>

It produces a <!DOCTYPE> element in the output document, like this:

<!DOCTYPE PLANETS SYSTEM "planets.dtd">

As you can see, there’s a tremendous amount going on in XSL transformations. In fact, there’s more than we can cover here—for plenty of additional details, take a look at the W3C XSLT specification at www.w3.org/TR/xslt, and the XPath specification at www.w3.org/TR/xpath.

There’s more to XSL—besides XSL transformations, XSL also includes a whole formatting language, and I’m going to take a look at that in the next chapter.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus