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 theres 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 dont 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> elements 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. Heres 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 declarations 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 declarations standalone attribute. Set this to yes or no. |
Version |
Specifies the value for the XML declarations version attribute. |
Another useful attribute of <xsl:output> is media-type, which enables you to specify the MIME type of the output document. Heres 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, theres a tremendous amount going on in XSL transformations. In fact, theres more than we can cover herefor 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.
Theres more to XSLbesides XSL transformations, XSL also includes a whole formatting language, and Im going to take a look at that in the next chapter.