[XML-XSLT] XSLT Transformation Examples

This tutorial shows how to use XSL (XSLT and XPath) to transform an XML file to other types of text files (XML, text …).


1. XML to XML

One of the most important role of XSLT is the creation of a new XML document from a source XML document. To transform an XML document to another XML document, following steps are required :

  • XML output formatting
  • Comments
  • PIs (Processing Instructions) Processing
  • CDATA Section Processing
  • Transformation

(1) XML Output Format

The output element is used to define the structure of the result document.

  • Method : indicates the output is a XML document. The XSLT processor ensures that the output conforms to the XML standard.
  • Media Type : specify the MIME type to the output document.
  • XML declaration : The “omit-xml-declaration” attribute specifies whether or not an XML declaration is to be included in the output document.
  • DOC Type declaration : The “doctype-system” attribute specifies that the output document should have an XML document type declaration. The “doctype-public” attribute specifies the public identifier to supply. When you use the “doctype-public” attribute, a “doctype-system” attribute must be given as well, even if it contains only an empty string.
<xsl:output method="xml" media-type="text/xml" omit-xml-declaration="no" />

(2) Comment Elements

An XML comment can be created the <xsl:comment> element.

<xsl:comment> ... </xsl:comment>

(3) Copying-through Comments

Comments in the source document can be copied through to the output by creating a template to match comment() nodes.

<xsl:template match="comment()">
    <xsl:value-of select="." />


<xsl:template match="comment()">
  <xsl:copy-of select="." />

(4) Constructing Comment text

You can transform an element in the source document into a comment in the output. In this case, <xsl:apply-templates /> is used.

<xsl:template match="...">
    <xsl:apply-templates />

(5) Processing Instructions

An XML PI can be created using the <xsl:processing-instruction/> element.  The “name” attribute is used to hold the name of the target application. The content of the element becomes the actual instruction.

<xsl:processing-instruction name="xml-stylesheet" >
  <xsl:text> type="text/xsl" href="myXSL.xsl" </xsl:text>

The output will be:

<?xml-stylesheet type="text/xsl" href="myXSL.xsl" ?>

(6) CDATA Sections

The “cdata-section-elements” attribute of <xsl:output>lists the elements whose text node children should be wrapped in CDATA sections.

<xsl:output cdata-section-elements="...." />



Because an XHTML document is an XML document (not pure HTML document), the XML output method should be used to generate an XHTML document from an XML document.

(1) XHTML Output Format

  • Method: set it te to “xml” which Indicates the output is a XML document.
  • Media Type : Both “text/html” and “text/xml” are possible.
  • XML declaration : An XML declaration is not essential for XHTML, but it’s a good idea to include it.
  • DOC Type declaration : To create a compliant XHTML document, the DOCTYPE declaration is needed.
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />

(2) Name Space

XML(xsl namespace) -> XHTML(default namespace)

<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml" >


3. XML to HTML

Converting XML to HTML is one of the most important roles of XSLT.

(1) HTML Output Format

  • Method : indicates the output is an HTML document.
  • Media Type : Use MIME type “text/html“.
  • XML declaration : Because an HTML document is not an XML document, omit the XML declaration.
  • DOC Type declaration : The Strict, Transitional, and Frameset DOCTYPEs are possible. For HTML5, simple doctype needs to be used.
  doctype-public="-//W3C//DTD HTML 4.01//EN"
  doctype-system="http://www.w3.org/TR/html4/strict.dtd" />

For Html5, it is a little bit simpler.

  doctype-system="" />


4. XML to Text

XML data often needs to be processed for delivery to tools that are not XML-compliant. In this case, it is necessary to avoid outputting all XML-sepecific markups.

(1) Text Output Format

  • Method : Indicates the output is a text document.
  • Media Type : Use “text/plain” as a MIME type.
  • XML declaration : Omit XML declaration
<xsl:output method="text" media-type="text/plain" omit-xml-declaration="yes" />

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s