<xsl:output>

magnify-glassThe XSLT <xsl:output> element helps determine the formatting of the files produced by the transformation.

The XSLT <xsl:output> is a top-level element and must be a child of the <xsl:stylesheet>. The <xsl:output> element determines the file format produced by the transform. The most used attribute for this element is the method attribute:

  • Set to ‘xml’ to output XML documents. This is also the default setting.
  • Set to ‘html’ to output HTML documents.
  • Set to ‘text’ to output a text stream / document.


 

Syntax

<xsl:output
method = "xml" | "html" | "text" | qname-but-not-ncname
version = nmtoken
encoding = string
omit-xml-declaration = "yes" | "no"
standalone = "yes" | "no"
doctype-public = string
doctype-system = string
cdata-section-elements = qnames
indent = "yes" | "no"
media-type = string />

 

Attributes

AttributeOptionalValue(s)DefaultDescription
methodYESxml
html
text
qname
xmlDescribes the output document format. Standard supported documents are XML, HTML or Text.
versionYES 1.0 
encodingYES  Sets the value of the encoding attribute on the XML declaration. For example: UTF-8
omit-xml-declarationYESyes
no
noSpecify whether the XSLT processors must omit the XML declaration for xml-type documents.

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no” ?>

standaloneYESyes
no
noSpecify whether the XSLT processors must set the  standalone  attribute on the XML document declaration. 

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”no” ?>

The value is only relevant when a DTD is used – the attribute is irrelevant when using a schema.

standalone=”yes” means that a DTD will be used for validation only.  In such a case the DTD will not be used for default attribute values, entity declarations or normalization. It may also be used to optimize for performance during document processing.

standalone=”no” means that a DTD is used to validate the document, define default values, entity declarations and normalization. I.o.w. an XML document must comply with the DTD rules.

doctype-publicYES   
doctype-systemYES   
cdata-section-elementsYES  Space-separated list of xml element names: Each of these elements are output as CDATA sections. The XSLT processor will preserve format and skip the content of CDATA sections during processing.
indentYESyes
no
noIndent the XML document – i.e.. output a pretty XML document or not.
media-typeYES  Defines the mime type of the output document. This may be anything; the mime type also depends on the method attribute (xml, html, text, or qname).

Examples

How to output an XML Document

An XSL transform takes the source data from an input document and transforms it into a desired output XML document.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!-- construct the XML document from the source through transformation. -->
<xsl:template match="/">
<library>
<category name="cats" />
</library>
</xsl:template>
</xsl:stylesheet>

The transform generates the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<library>
<category name="cats"/>
</library>

 

How to output an XML Document with formatted CDATA

The following XSL transform shows how to preserve formatting in an XML document output. Normally the XSLT processor will strip any leading and trailing whitespace or carriage returns. A CDATA element is skipped by the XML processor and can be used to present formatted data or an XML document on its own.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" cdata-section-elements="comments"/>
<xsl:template match="/">
<comments>This is a formatted text section.
Normally XML / XSLT parsers strips whitespace/carriage returns.

A
B
C

See - told you so.
</comments>
</xsl:template>
</xsl:stylesheet>

The XSL transform generates the following output. The formatted text is wrapped in a CDATA section.

<?xml version="1.0" encoding="UTF-8"?>
<comments>
<![CDATA[This is a formatted text section.
Normally XML / XSLT parsers strips whitespace/carriage returns.

A
B
C

See - told you so.
]]>
</comments>

How to output an HTML

The following XSL transform generates an HTML document as an output. This method is customarily used to generate custom HTML pages from source data represented as XML.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>HTML Document Example</title>
</head>
<body>
This is the example body for an HTML document.
</body>
</html>
</xsl:template>
</xsl:stylesheet>

The XSLT transform generates the following HTML.

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTML Document Example</title>
</head>
<body>
This is the example body for an HTML document.
</body>
</html>

How to output a Text Document

The XSLT text output is commonly used to generate flat-file target documents (plain-text). The format and layout is depends on requirements (i.e. tabbing, comma delimited, etc.).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">Outputs plain text.

The format is dependent on the XSL transform.
</xsl:template>
</xsl:stylesheet>

The example XSL transform generates the following text file.

Outputs plain text.

The format is dependent on the XSL transform.

Posted in XSLT and tagged .

Leave a Reply