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 are not replaced with characters such as < or & in the output document; the actual entity reference appears in the output.

  • HTML This is standard HTML 4.0, without an 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 /> . In addition, < 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 allows 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"/> 

Here are some <xsl:output> attributes you can use to create or modify XML declarations:

  • encoding Specifies the value for the XML declaration's encoding attribute.

  • omit-xml-declaration Specifies whether the processor should omit the XML declaration. Set to "yes" or "no" .

  • standalone Specifies the value for the XML declaration's standalone attribute. Set 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 lets you 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, this <xsl:output> element

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

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 that we can cover here. For plenty of additional details, take a look at New Riders' Inside XSLT (it's a great bookwell, that's what I think, anyway, because I wrote it), 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 includes a whole formatting language. I'm going to take a look at that next .



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net