Working with xsl:output

There are two things we need to consider when generating output from our XML Document. The first is how the output is generated (what tool does the work), and the second is the format of the output. Of course, all XSL transformations produce some sort of output. XSLT would be of dubious worth otherwise. Control over the output format is provided via the xsl:output element. xsl:output is a topmost element within an XSL stylesheet and is responsible for how the output of the stylesheet is actually formatted. xsl:output has a number of attributes, but one of the most important is the method attribute. The method attribute must contain one of three values: xml, html, or text. For example

<xsl:output method="html"/>

specifies that the XSL transform will produce well-formed HTML. In addition, the method attribute, if it matches a namespace, is expanded into that namespace on output.

The following stylesheet fragment

<?xml version="1.0"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
    xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output method="html"/>

when applied to an XML document, might produce

<html xmlns="http://www.w3.org/TR/REC-html40">...

Table 5.2 lists the most common attributes of xsl:output.

Table 5.2. Attributes of xsl:output
Attribute Description
version= The version of the output method. Combines with, for example, version="4.0".
indent=(yes|no) Defines whether the processor can add additional whitespace to the output.
media-type Specifies the output media type. Note that output types must be MIME compliant. Only applicable to method="text". One example might be media-type="text/plain".
standalone=(yes|no) Specifies whether the result will be a standalone document or not.
xml-declaration=(yes|no) Specifies whether the transform should output an XML declaration.
cdata-section-elements Specifies elements that should be output as CDATA sections.

It should be noted that multiple xsl:output statements, either via one stylesheet or through multiple stylesheets included or embedded, are logically merged together into a simple xsl:output element.

In general, the xsl:output statement allows the developer to state exactly what he or she expects the output to look like, and the XSL processor would then take this information into account when generating the result. For example, HTML supports the tag <br> that results in a paragraph or hard break in a document. Any rule that resulted in <br></br> or </br> could be output simply as <br> because HTML supports it. Likewise, escaping and/or implicit conversion to CDATA sections may also be implied. Again, using HTML as a guide, an output result containing a well-known entity, such as &lt, should normally be output as < rather then [CDATA [<]]. In a similar fashion, method="text" results in nothing being escaped at all.

Multiple xsl:output statements come in handy when processing elements that can be interpreted as, perhaps, JavaScript, or if we knew that every element of a certain type should be output as CDATA. In our earlier example, if we wanted all <County> elements to be encased as CDATA sections, we could write the following:

<xsl:output cdata-section-elements="County"/>

As a result, elements found with contents such as &lt, for example

<County>Middlesex County, a county with &lt 10000 people</County>

would generate something to the effect of

<!CDATA[Middlesex County, a county with < 10000 people]>

disable-output-escaping="yes"

For XML output, output escaping can be disabled for specific text elements written to the result tree. For example, <xsl:text> something to be output &lt</xsl:text> would normally result in something to be output &lt being written to preserve the entity less-than. However, the same rule with output escaping disabled would result in something to be output < being output; note the replacement of &lt with <. In this fashion, a fine grain of control can be established where required, perhaps to correctly generate script or other elements of HTML.


With output formatting in mind, let's move on to output generation!

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

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