Output Types

Contact Us or call 1-877-932-8228
Output Types

Output Types

The type of output is determined by the method attribute of the xsl:output element, which must be a direct child of the xsl:stylesheet element.

Text

<xsl:output method="text"/>

As we have seen in the examples thus far, XSLT can be used to output plain text. However, XSLT is much more commonly used to transform one XML structure into another XML structure or into HTML.

XML

<xsl:output method="xml"/>

The default output method (in most cases) is XML. That is, an XSLT with no xsl:output element will output XML. In this case, XSLT tags are intermingled with the output XML tags. Generally, the XSLT tags are prefixed with xsl:, so it is easy to tell them apart. The output XML tags may also be prefixed, but not with the same prefix as the XSLT tags.

Literal Result Elements

The output XML tags are called literal result elements. That is because they are output literally rather than used by the XSLT to determine what or how something should be output.

Useful xsl:output Attributes When Outputting XML
Attribute Description
method Should be set to xml.
indent yes or no. If yes, the resulting XML document will be pretty printed. The default is usually no.
omit-xml-declaration yes or no. If no, the resulting XML document will begin with an XML declaration. The default is usually no.
version Specifies the XML version of the resulting XML document.

The documents below show how XSLT can be used to output XML.

Code Sample:

XsltBasics/Demos/Name.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Name.xsl" type="text/xsl"?>
<person>
	<name>
		<firstname>Paul</firstname>
		<lastname>McCartney</lastname>
	</name>
</person>

Code Sample:

XsltBasics/Demos/Name.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" version="1.0"/>
	<xsl:template match="name">
		<Matches>
			<Match>We found a name!</Match>
			<Name><xsl:value-of select="."/></Name>
		</Matches>
	</xsl:template>
</xsl:stylesheet>

Code Sample:

XsltBasics/Demos/NameResult.xml
<?xml version="1.0" encoding="UTF-8"?>
<Matches>
	<Match>We found a name!</Match>
	<Name>PaulMcCartney</Name>
</Matches>

HTML

<xsl:output method="html"/>

It is very common to use XSLT to output HTML. In fact, XSLT even has a special provision for HTML: if the document element of the resulting document is html (not case sensitive) then the default method is changed to HTML. However, for the sake of clarity, it is better code to specify the output method with the xsl:output tag.

The documents below show how XSLT can be used to output HTML.

Code Sample:

XsltBasics/Demos/NameHTML.xml
<?xml version="1.0"?>
<?xml-stylesheet href="NameHTML.xsl" type="text/xsl"?>
<person>
	<name>
		<firstname>Paul</firstname>
		<lastname>McCartney</lastname>
	</name>
</person>

Code Sample:

XsltBasics/Demos/NameHTML.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	<xsl:template match="name">
		<html>
			<head>
				<title>We found a name!</title>
			</head>
			<body>
				<h1>
					<xsl:value-of select="firstname"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="lastname"/>
				</h1>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

If you open XsltBasics/Demos/NameHTML.xml in Internet Explorer, you'll see the following result.

Next