Output formats

Output from an XSLT processor can be customized in a number of ways. The Output element is an empty element, but has attributes that specify various requirements for formatting of the output:

<output ... />

The Method attribute controls fundamental formatting characteristics that are required for XML, HTML and text output. It holds the value 'xml', 'html' or 'text' accordingly. The implications of these settings are described in later chapters. By default, the setting is 'xml', except when an HTML document is recognized by its root element name ('<HTML>'). The Version attribute supports this feature and is used, for example, to indicate which version of HTML to output:

<output ... method="html" version="3.2" />

The Encoding attribute specifies the character set to use for output, such as 'UTF-8' (the default) or 'ISO-8859-1' (the usual set used on the Web):

<output ... encoding="ISO-8859-1" />

The Omit XML Declaration attribute specifies whether or not an XML declaration is to be included in the output document and it has the value 'yes' or 'no'. When set to 'no', the declaration is created:

<output ... omit-xml-declaration="yes" />


   <book>...</book>


<output ... omit-xml-declaration="no" />


   <?xml ...?>
   <book>...</book>

When this attribute is set to 'no' (the declaration is to be created), the Standalone attribute can be used to specify whether or not the output document is a standalone XML document (ie, one that does not require the DTD for accurate interpretation). It also takes a value of 'yes' or 'no'. If the Encoding attribute is used, its content will also be copied into the XML declaration:

<output ... omit-xml-declaration="no"
            standalone="yes"  encoding="ISO-8859-1" />


   <?xml version="1.0" encoding="ISO-8859-1"
                       standalone="yes" ?>

The Doctype System attribute specifies that the output document should have an XML document type declaration, with a system identifier given in this attribute:

<output ... doctype-system="dtds/book.dtd" />


   <!DOCTYPE book SYSTEM="dtds/book.dtd" >
   <book>...</book>

The Doctype Public attribute specifies the public identifier to supply. If used, the Doctype System attribute must be given as well, in order to conform to XML rules, even if it only contains an empy string:

<output ... doctype-public="myBookDef" doctype-system=""/>


   <!DOCTYPE book PUBLIC="myBookDef" "" >
   <book>...</book>

The Indent attribute can be used to format XML output, by indenting embedded element structures. It takes the value 'yes' or 'no' (the default). When set to 'yes', whitespace is added to make the document easier to read:

<output ... indent="yes" />


   <book>
     <title>The Book Title</title>
     <chapter>
       <title>The Chapter Title</title>
       <para>A paragraph.</para>
     </chapter>
   </book>

There is a danger with this kind of formatting. For elements that have mixed content, unwanted space may be added, as in the following example:

<para>Do
  <emph>NOT</emph>
 add whitespace.</para>

Do NOT add whitespace.

The text content of selected elements can be output as character data sections (CDATA sections), '<![CDATA[…]]>'. These structures are used to avoid the need for '&lt;', '&amp;' (and other entity references) to represent significant markup characters when they appear in the text. The Cdata Section Elements attribute holds the names of the output elements. For example:

   <software>if (6 &lt; 9) then ...</software>


<xsl:output cdata-section-elements="code ... ..." />
...
<xsl:template match="software">
  <code><xsl:apply-templates/></code>
</xsl:template>


   <code><![CDATA[if (6 < 9) then ...]]></code>

If the text happens to contain the string ']]>', then this must be broken into two parts to avoid indication of an early end to the CDATA section. Two CDATA sections are created, with the ']]' sequence between them and the '>' in the second:

<![CDATA[ ... ]]>]]<![CDATA[> ...]]>

Finally, the Media Type attribute specifies the type of data output. For XML, this would be 'text/xml' or 'text/application'. Use of this attribute has no effect on the output document itself, and it is only of interest if the XSLT processor is able to transmit the output data directly over a network that uses the MIME scheme for indicating the data type of information transferred, such as over the Internet:

<output ... media-type="text/xml" />

With HTML documents, it has more widespread application, as described later.

Note that the attributes from multiple Output elements from different parts of a stylesheet that have been imported are merged, with the highest-importance definitions for each attribute used (except for the CDATA Section Elements attribute, which combines values from all definitions).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset