Outputting Processing Instructions

Welcome to our free XSLT 1.0 and XPath 1.0 tutorial. This tutorial is based on Webucator's XSLT 1.0 and XPath 1.0 Training course.

Contact Us or call 1-877-932-8228
Outputting Processing Instructions

Outputting Processing Instructions

It is sometimes necessary to output processing instructions with XSLT; for example, if you need to transform one XML document to another XML structure, which will then be transformed to HTML via XSLT. This is illustrated in the diagram below.

Processing instructions are output with the xsl:processing-instruction element as shown below.

<xsl:processing-instruction name="Name_Of_Instruction"> Name-Value Pairs of Instruction </xsl:processing-instruction>

Let's look at an example.

Code Sample:

AdvancedXsltTechniques/Demos/Beatles.xml
<?xml version="1.0"?>
<?xml-stylesheet href="Beatles2Artists.xsl" type="text/xsl"?>
<beatles>
	<beatle link="http://www.paulmccartney.com">
		<name>
			<firstname>Paul</firstname>
			<lastname>McCartney</lastname>
		</name>
	</beatle>
	<beatle link="http://www.johnlennon.com">
		<name>
			<firstname>John</firstname>
			<lastname>Lennon</lastname>
		</name>
	</beatle>
	<beatle link="http://www.georgeharrison.com">
		<name>
			<firstname>George</firstname>
			<lastname>Harrison</lastname>
		</name>
	</beatle>
	<beatle link="http://www.ringostarr.com">
		<name>
			<firstname>Ringo</firstname>
			<lastname>Starr</lastname>
		</name>
	</beatle>
	<beatle link="http://www.webucator.com" real="no">
		<name>
			<firstname>Nat</firstname>
			<lastname>Dunn</lastname>
		</name>
	</beatle>
</beatles>

Code Sample:

AdvancedXsltTechniques/Demos/Beatles2Artists.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"/>
	<xsl:template match="/beatles">
		<xsl:text>&#10;</xsl:text>
		<xsl:processing-instruction name="xml-stylesheet">
			<xsl:text>href="Artists.xsl" type="text/xsl"</xsl:text>
		</xsl:processing-instruction>
		<xsl:text>&#10;</xsl:text>
		<Artists>
			<xsl:apply-templates/>
		</Artists>
	</xsl:template>
	<xsl:template match="beatle">
		<Artist>
			<FirstName>
				<xsl:value-of select="name/firstname"/>
			</FirstName>
			<LastName>
				<xsl:value-of select="name/lastname"/>
			</LastName>
		</Artist>
	</xsl:template>
</xsl:stylesheet>

The result of transforming AdvancedXsltTechniques/Demos/Beatles.xml against AdvancedXsltTechniques/Demos/Beatles2Artists.xsl is shown below.

Code Sample:

AdvancedXsltTechniques/Demos/BeatlesOutput.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Artists.xsl" type="text/xsl"?>
<Artists>
	<Artist>
		<FirstName>Paul</FirstName>
		<LastName>McCartney</LastName>
	</Artist>
	<Artist>
		<FirstName>John</FirstName>
		<LastName>Lennon</LastName>
	</Artist>
	<Artist>
		<FirstName>George</FirstName>
		<LastName>Harrison</LastName>
	</Artist>
	<Artist>
		<FirstName>Ringo</FirstName>
		<LastName>Starr</LastName>
	</Artist>
	<Artist>
		<FirstName>Nat</FirstName>
		<LastName>Dunn</LastName>
	</Artist>
</Artists>

As you can see from the output above, the generated XML document has a processing instruction which indicates that it should be transformed against Artists.xsl (shown below).

Code Sample:

AdvancedXsltTechniques/Demos/Artists.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"/>
	<xsl:template match="/">
		<html>
			<head>
				<title>Artists</title>
			</head>
			<body>
				<h1>Artists</h1>
				<table>
						<tr>
							<th>First Name</th>
							<th>Last Name</th>
						</tr>
						<xsl:apply-templates/>
				</table>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="Artist">
		<tr>
			<td><xsl:value-of select="FirstName"/></td>
			<td><xsl:value-of select="LastName"/></td>
		</tr>
	</xsl:template>
</xsl:stylesheet>

The result of transforming AdvancedXsltTechniques/Demos/BeatlesOutput.xml against AdvancedXsltTechniques/Demos/Artists.xsl is shown below.

Code Sample:

AdvancedXsltTechniques/Demos/Beatles.html
<?xml version="1.0" encoding="UTF-8"?>
<html>
	<head>
		<title>Artists</title>
	</head>
	<body>
		<h1>Artists</h1>
		<table>
			<tr>
				<th>First Name</th>
				<th>Last Name</th>
			</tr>
			<tr>
				<td>Paul</td>
				<td>McCartney</td>
			</tr>
			<tr>
				<td>John</td>
				<td>Lennon</td>
			</tr>
			<tr>
				<td>George</td>
				<td>Harrison</td>
			</tr>
			<tr>
				<td>Ringo</td>
				<td>Starr</td>
			</tr>
			<tr>
				<td>Nat</td>
				<td>Dunn</td>
			</tr>
		</table>
	</body>
</html>
Next

This tutorial is based on Webucator's XSLT 1.0 and XPath 1.0 Training Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.