Herong's Tutorial Notes On XML Technologies
Dr. Herong Yang, Version 3.04

XSL - Formatting Output

Part:   1   2 

This tutorial describes:

  • The "output" Element
  • Formatting Output in XML Format

The 'output' Element

output: An XSL element, serving as a system statement. It sets system configuration values to the XSL processor to control the output format.

<xsl:output method="xml"/>
<xsl:output method="html"/>
<xsl:output method="text"/>

where "method" is an attribute used to set the XSL processor's output formatting method. It takes one of the following 3 commonly used values:

  • xml: Asking the XSL processor to produce output in XML format. This is the default method.
  • html: Asking the XSL processor to produce output in HTML 4.0 format.
  • text: Asking the XSL processor to produce output in plain text format.

Let's try "xml" method first with hello_xml.xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml"/>
 <xsl:template match="p">&lt;/p>Hello world!&lt;p>
  - From hello.xsl.</xsl:template>
</xsl:stylesheet>

If you run this:

java XSLTransformer hello.xml outout.xml hello_xml.xsl

you will get output.xml:

<?xml version="1.0" encoding="UTF-8"?>
&lt;/p&gt;Hello world!&lt;p&gt;
  - From hello.xsl.

"xml" method converted ">" to "&gt;".

Now try text method with hello_text.xsl:

<?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="p">&lt;/p>Hello world!&lt;p>
  - From hello.xsl.</xsl:template>
</xsl:stylesheet>

If you run this:

java XSLTransformer hello.xml outout.txt hello_text.xsl

you will get output.txt:

</p>Hello world!<p>
  - From hello.xsl.

"text" method converted "&lt;" to "<".

Formatting Output in XML Format

The "output" element with method="xml" may take additional attributes, like:

<xsl:output method="xml" version="1.0" encoding="UTF-8"/>

Those attributes will be converted by the XSL processor into the "xml" declaration statement in the output:

<?xml version="1.0" encoding="UTF-8"?>

There are three ways to insert XML elements into the output:

  • Enter the XML elements as text strings inside the "template" statement.
  • Using "elemenet" and "attribute" statements.
  • Using "copy" statements.

1. Entering XML elements as text strings:

Here is an exampple:

<xsl:template match="update">
 <update><date><xsl:value-of select="@date"/></date></update>
</xsl:template>

In this template, I am trying to copy the "update" element from the source to the output. All the child nodes and attributes of "update" are ignored, except the "date" attribute, which is transformed into a child element with the same name. The value of "date" is inserted into the output as "date" element's content.

But if I try to keep the "date" attribute as an attribute in output:

<xsl:template match="update">
 <update date="<xsl:value-of select="@date"/>"/>
</xsl:template>

I will get into syntax errors.

To avoid syntax problems, when a "value-of" statement is used as part of the value string of an XML attribute, it can be abbreviated as "{...}". So the following statement:

 <e_name a_name="{pattern}"/>

is logically equivalent to:

 <e_name a_name="<xsl:value-of select="pattern"/>"/>

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes On XML Technologies - XSL - Formatting Output