3.1 The Output Method

As you have already seen, the output element has a method attribute. This attribute indicates explicitly the kind of output you want the XSLT processor to produce, namely, XML, HTML, or plain text. These three amigos the attribute values xml, html, and text should always be lowercase when used as values for method. (Again, XSLT 2.0 will also support the xhtml output method.)

3.1.1 The Default Output Methods

If you don't assign a value to method, you get a default output method depending on what a stylesheet produces. The default output method for XSLT is XML unless the document element in the result is html. In such a case, the default output method is HTML. The tag name html can be in uppercase, lowercase, or mixed case, but it must not have a namespace URI associated with it (no xmlns attribute).

3.1.1.1 Default HTML output

To understand how default HTML works, consider the document name.xml found in examples/ch03 (this is where all the examples files mentioned in this chapter are found):

<name>  <last>Churchill</last>  <first>Winston</first> </name>

Then look at default-html.xsl that produces HTML using literal result elements:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="name">  <html>   <body>   <p><xsl:apply-templates select="last"/></p>   <p><xsl:apply-templates select="first"/></p>   </body>  </html> </xsl:template>     </xsl:stylesheet>

Notice that there is no output element in default-html.xsl to tell the processor explicitly what the output method is. Apply this stylesheet to name.xml with Xalan:

xalan -m name.xml default-html.xsl

and it will produce a default HTML result:

<html> <head> </head> <body> <p>Churchill</p> <p>Winston</p> </body> </html>

The -m command-line option suppresses the META tag that Xalan would normally produce. The result does not have an XML declaration because Xalan evaluated the result as HTML, as it should. The result is also indented (line breaks at start tags, but zero space) because if the output method is HTML, a default value of yes for indent is assumed as if an output element with indent="yes" attribute were present.

With Xalan, you can also control the amount of indentation from the command line by using the -i option with an integer. For example, if you want to indent the output by three spaces, type this command:

xalan -i 3 -m name.xml default-html.xsl

The indented output will look like this:

<html>    <body>       <p>Churchill</p>       <p>Winston</p>    </body> </html>

The child elements body and p are nicely indented by three spaces. This indentation feature of Xalan is not specified by XSLT itself, but it is nice to have nevertheless. You can read more about the default HTML output method in Section 16.2 of the XSLT specification.

3.1.1.2 Default XML output

Now, check out default-xml.xsl, which produces a default XML result using literal result elements:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="name">  <name>   <family><xsl:apply-templates select="last"/></family>   <given><xsl:apply-templates select="first"/></given>  </name> </xsl:template>     </xsl:stylesheet>

Applying default-xml.xsl to name.xml with Xalan:

xalan name.xml default-xml.xsl

will produce the following result:

<?xml version="1.0" encoding="UTF-8"?> <name><family>Churchill</family><given>Winston</given></name>

The default XML output method for Xalan kicks out an XML declaration with an encoding declaration for UTF-8 (more about this in Section 3.2.1.2). The elements are output with no indentation added because the default for indent is no for XML output (as if an output element were present with indent="no").

You can use the default methods for HTML and XML, but it is always cleaner, and more apparent to other humans reading your stylesheet, if you specify the method attribute explicitly with a value of xml or html. (You can read even more about the default XML output method in Section 16.1 of the XSLT specification.)



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

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