<xsl:output> |
Defines the characteristics of the output document.
Category
Top-level element
Required Attributes
None.
Optional Attributes
method Typically has one of three values: xml , html , or text . This value indicates the type of document that is generated. An XSLT processor can add other values to this list; how those values affect the generated document is determined by the XSLT processor. version Defines the value of the version attribute of the XML or HTML declaration in the output document. This attribute is used only when method= " html " or method= " xml " . encoding Defines the value of the encoding attribute of the XML declaration in the output document. omit-xml-declaration Defines whether the XML declaration is omitted in the output document. Allowable values are yes and no . This attribute is used only when method= " xml " . standalone Defines the value of the standalone attribute of the XML declaration in the output document. Valid values are yes and no . This attribute is used only when method= " xml " . doctype-public Defines the value of the PUBLIC attribute of the DOCTYPE declaration in the output document. This attribute defines the public identifier of the output document's DTD. It is used only when method= " html " or method= " xml " . doctype-system Defines the value of the SYSTEM attribute of the DOCTYPE declaration in the output document. It defines the system identifier of the output document's DTD. This attribute is used only when method= " html " or method= " xml " . cdata-section-elements Lists the elements that should be written as CDATA sections in the output document. All restrictions and escaping conventions of CDATA sections are handled by the XSLT processor. If you need to list more than one element, separate the element names with one or more whitespace characters. This attribute is used only when method= " xml " . indent Specifies whether the tags in the output document should be indented. Allowable values are yes and no . This attribute is used only when method= " xml " or method= " html " , and the XSLT processor is not required to honor it. media-type Defines the MIME type of the output document.
Content
None. <xsl:output> is an empty element.
Appears in
<xsl:output> is a top-level element and can only appear as a child of <xsl:stylesheet>.
Defined in
XSLT section 16, Output.
Example
To illustrate the three output methods defined in the XSLT specification, we'll create three stylesheets, each of which uses one of the three methods. We'll use the following XML document in all three examples:
    <?xml version="1.0"?>
    <list>
      <title>A few of my favorite albums</title>
      <listitem>A Love Supreme</listitem>
      <listitem>Beat Crazy</listitem>
      <listitem>Here Come the Warm Jets</listitem>
      <listitem>Kind of Blue</listitem>
      <listitem>London Calling</listitem>
      <listitem>Remain in Light</listitem>
      <listitem>The Joshua Tree</listitem>
      <listitem>The Indestructible Beat of Soweto</listitem>
    </list>
We'll now look at our three stylesheets and the results produced by each. First, let's look at the method="xml" stylesheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output
        method="xml"
        doctype-public="-//W3C/DTD XHTML 1.0//EN"
        doctype-system="file:///d:/xhtml.dtd"
        encoding="ISO-8859-1"
        indent="no"/>

      <xsl:template match="/">
        <html>
          <head>
            <title><xsl:value-of select="/list/title"/></title>
          </head>
          <body>
            <h1><xsl:value-of select="/list/title"/></h1>
            <p>
              <xsl:for-each select="/list/listitem">
                <xsl:number format="1. "/>
                <xsl:value-of select="."/>
                <br/>
              </xsl:for-each>
            </p>
          </body>
        </html>
      </xsl:template>

    </xsl:stylesheet>
This stylesheet generates the following results:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0//EN" "file:///d:/xhtml.dtd">
    <html><head><title>A few of my favorite albums</title>
    </head><body><h1>A few of my favorite albums</h1>
    <p>1. A Love Supreme<br/>2. Beat Crazy<br/>3. Here Come the
    Warm Jets<br/>4. Kind of Blue<br/>5. London Calling<br/>6.
    Remain in Light<br/>7. The Joshua Tree<br/>8. The Indestructible
    Beat of Soweto<br/></p></body></html>
(We actually added line breaks to this listing; the original output put everything from <html> through </html> on a single line.) The output document has the encoding we specified in our stylesheet, and the DOCTYPE declaration includes the PUBLIC and SYSTEM identifiers we requested as well. Even with the line breaks we added, it's still obvious that this document has not been formatted with any extra whitespace whatsoever. We also have empty <br/> elements in our output document; those elements will be handled differently when we specify method="html". Speaking of which, here is our method="html" stylesheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output
        method="html"
        encoding="ISO-8859-1"
        doctype-public="-//W3C/DTD HTML 1.0 Transitional//EN"/>

      <xsl:template match="/">
        <html>
          <head>
            <title><xsl:value-of select="/list/title"/></title>
          </head>
          <body>
            <h1><xsl:value-of select="/list/title"/></h1>
            <p>
              <xsl:for-each select="/list/listitem">
                <xsl:number format="1. "/>
                <xsl:value-of select="."/>
                <br/>
              </xsl:for-each>
            </p>
          </body>
        </html>
      </xsl:template>

    </xsl:stylesheet>
Here is the HTML document generated by this stylesheet:
    <!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 1.0 Transitional//EN">
    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>A few of my favorite albums</title>
    </head>
    <body>
    <h1>A few of my favorite albums</h1>
    <p>1. A Love Supreme<br>2. Beat Crazy<br>3. Here Come
    the Warm Jets<br>4. Kind of Blue<br>5. London Calling<br>6.
    Remain in Light<br>7. The Joshua Tree<br>8. The Indestructible
    Beat of Soweto<br>
    </p>
    </body>
    </html>
(As before, we added line breaks to make the listing legible.) Notice that the XSLT processor has automatically inserted a <META> element in the <head> of our HTML document. The <br> elements that were empty in our previous stylesheet are now old-fashioned <br> tags. Even though this style of XSLT output results in a document that is not valid XML (or XHTML), the document will work with existing HTML browsers. Our final stylesheet will use method="text":
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:output method="text"/>

      <xsl:template match="/">
        <html>
          <head>
            <title><xsl:value-of select="/list/title"/></title>
          </head>
          <body>
            <h1><xsl:value-of select="/list/title"/></h1>
            <p>
              <xsl:for-each select="/list/listitem">
                <xsl:number format="1. "/>
                <xsl:value-of select="."/>
                <br/>
              </xsl:for-each>
            </p>
          </body>
        </html>
      </xsl:template>

    </xsl:stylesheet>
Here are the results, such as they are, from this stylesheet:
    A few of my favorite albumsA few of my favorite albums1. A Love Supreme2. Beat
    Crazy3. Here Come the Warm Jets4. Kind of Blue5. London Calling6. Remain in
    Light7. The Joshua Tree8. The Indestructible Beat of Soweto
(As before, we inserted line breaks so the document would fit on the page.) These results are basically worthless. Why weren't our carefully coded HTML elements output to the text document? The reason is that the text output method only outputs text nodes to the result tree. Even though we requested that various HTML elements be generated along the way, they're ignored because we specified method="text".