XSLT » Elements » xsl:output

Syntax:
<xsl:output
  cdata-section-elements="namelist"
  doctype-public="string"
  doctype-system="string"
  encoding="string"
  indent="yes" | "no"
  media-type="mimetype"
  method="html" | "name" | "text" | "xml"
  omit-xml-declaration="yes" | "no"
  standalone="yes" | "no"
  version="version_number"
/>

The xsl:output element is used to define the format of the output created by the stylesheet. This is accomplished by setting one or more of ten optional attributes. The most important of these ten attributes is the method attribute which dictates if the type of output is HTML, text, or XML. The type of output, in turn, dictates which of the other nine attributes can be applied to the output.

The following table defines which attributes can optionally be set for each of the three types of output. A dash signifies that the attribute cannot effect the output.
 

Attribute HTML Text XML
cdata-section-elements - - YES
doctype-public YES - YES
doctype-system YES - YES
encoding YES YES YES
indent YES - YES
media-type YES YES YES
omit-xml-declaration - - YES
standalone - - YES
version YES - YES

 
You can have zero or more xsl:output elements:
  • If there is more than one xsl:output element, the XSLT processor essentially combines the information.
  • If more than one xsl:output element sets the same attribute, the element with the highest import precedence will have its attribute selected.
  • If more than one xsl:output element repeats an attribute and has the same highest import precedence, either the last will be chosen or an error will be declared.
  • If there is more than one cdata-section-elements attribute, all of the values in the name lists will effectively be merged into one list.
The xsl:output element can only be a child of the xsl:stylesheet or the xsl:transform elements.
 
This is a self-closing element and it cannot contain any child elements or any content.

Examples

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0" />
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="devguru_staff/programmer">
<div>
NAME: <xsl:value-of select="name" />
<br />
DOB: <xsl:value-of select="dob" />
<br />
AGE: <xsl:value-of select="age" />
<br />
ADDRESS: <xsl:value-of select="address" />
<br />
PHONE: <xsl:value-of select="phone" />
<hr />
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:

In this example, we declare our output to be in HTML. (By default, the occurrence of the <html> tag in the code signifies to the Microsoft XSLT processor we are using that the output is to be in HTML.)

Language(s): XSLT

See Also: