Hack 43 Choose Your Output Format in XSLT

   

figs/moderate.gif figs/hack43.gif

Take control of the output of an XSLT stylesheet.

The output element in XSLT has 10 attributes, each controlling a different aspect of XSLT output. This hack lists each of these attributes and shows you how to use them. All of output's attributes are optional.

An XSLT stylesheet can have more than one output element, and the values found in the attributes of those elements are combined. However, if a given attribute occurs on more than one output element, the last occurrence trumps all previous occurrences. Nevertheless, it is technically an error if an attribute (aside from cdata-section-elements) occurs on more than one output element in the stylesheet; however, processors can recover by choosing the value from the last output in the stylesheet.


Output method

The method attribute can have the value xml, html, or text for XML, HTML, and text output, respectively. XSLT 2.0 will support a value of xhtml for XHTML support. The method attribute can also have a QName as a value, but the QName must be recognized by the application processing it. If the first element in the result is html, the default method is html; xml if otherwise.


Indentation

The indent attribute takes the value yes or no. This tells the XSLT processor to indent the output to the result tree, which helps make output more readable. A value of yes is the default for the html output method, no for the xml output method.


XML declaration

The omit-xml-declaration attribute can have a value of yes or no. A value of yes instructs the XSLT processor to not insert an XML declaration onto the first line of output; a value of no instructs the processor to include one. If the output method is xml, the default is no.


Version information

The version attribute of output allows you to control the value used in the version information of the XML declaration; by default, it's 1.0 for the xml output method, 4.0 for the html method. With XML 1.1 now here (http://www.w3.org/TR/xml11/), it's good to know that you can control this value; for example, version="1.1".


Encoding declaration

The encoding attribute lets you specify a value for the encoding declaration on an XML declaration. Possible values include UTF-8, UTF-16, IS0-8859-1, and Shift_JIS. A hack in the previous chapter ( [Hack #27] ) covers encoding.


Standalone declaration

The standalone attribute can have a value of yes or no. This will then appear on the XML declaration. A value of yes means there are no external markup declarations in a DTD upon which the document depends, no if otherwise. Don't worry about this too much, as explained in an earlier hack on the XML document [Hack #1].


CDATA sections

The cdata-section-elements attribute can contain one or more QNames for the elements in the result tree that should output the text content of such an element in a CDATA section [Hack #1]. A CDATA section, which is formed like <![CDATA[Barnes & Noble]]>, hides characters such as the ampersand (&) from the processor. Use of this attribute is relatively rare.


Document type declarations

You can insert a document type declaration [Hack #7] for the output XML document by using the doctype-public attribute for inserting a public identifier, such as in <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 String//EN">, or the doctype-system attribute for inserting a system identifier, such as a simple filename (<!DOCTYPE time SYSTEM "time.dtd">).


Media type

The media-type attribute lets you specify a media (MIME) type explicitly, though you won't see such a type show up in the XSLT output. It is ostensibly for internal consumption, such as for the Content-Type header of an HTTP header. (I've never seen this attribute have a practical impact in downstream processing.) The default value is text/xml for the xml output method, text/html for the html method, and text/plain for the text method.

The following snippet shows a pair of output elements in an XSLT stylesheet. The values in both elements are used.

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> <xsl:output encoding="UTF-8" standalone="yes"/>



XML Hacks
XML Hacks: 100 Industrial-Strength Tips and Tools
ISBN: 0596007116
EAN: 2147483647
Year: 2006
Pages: 156

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