Books / Understanding XML / Chapter 8

Document Transformation and XSL

“If at first you don’t succeed, transform your data.” - The law of computability applied to social sciences

As explained above, XML is not a fixed tag set (like HTML) so the tags do not carry a fixed, application-specific meaning. A generic XML processor has no idea what is “meant” by the XML. Because of this, a number of other standards to process the XML files are developed. Extensible Stylesheet Language (XSL) is one such standard. XML markup usually does not include formatting information. The information in an XML document may not be in the form in which it is desired to be presented. There must be something in addition to the XML document that provides information on how to present or otherwise process the XML. XSL transforms and translates XML data from one XML format into another. It is designed to help browsers and other applications display XML. Stated simply, a style sheet contains instructions that tell a processor (such as a Web browser, print composition engine, or document reader) how to translate the logical structure of a source document into a presentational structure.

The XML/XSL relationship is reminiscent of the Model-View-Controller design pattern, which separates the core data from the way it gets visualized. Likewise, XSL enables us to separate the view from the actual data represented in XML. This has following advantages:

  • Reuse of data: When the data is separate you do not need to transform the actual data to represent it in some other form. We can just use a different view of the data. Multiple output formats: When view is separate from the data we can have multiple output formats for the same data e.g. the same XML file can be viewed using XSL as VRML, HTML, XML (of some other form)
  • Reader’s preferences: The view of the same XML file can be customized with the preferences of the user.
  • Standardized styles: Within one application domain there can be certain standard styles which are common throughout the developer community.
  • Freedom from content authors: A person not so good at presentation can just write the data and have a good presenter to decide on how to present the data.

Different ways of displaying an XML files are shown below.

How XSL transformation works.

How XSL transformation works

Example of XSL

Original XML Source:

<?xml version='1.0'?>
<para>This is a <emphasis>test</emphasis>.</para> 

XSL Stylesheet:

<?xml version='1.0'?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Format" version="1.0">
	<xsl:template match="para">
		<p>
			<xsl:apply-templates/>
		</p>
	</xsl:template>
	<xsl:template match="emphasis">
		<i>
			<xsl:apply-templates/>
		</i>
	</xsl:template>
</xsl:stylesheet> 

Resulting HTML Source:

<?xml version="1.0" encoding="utf-8"?> 
<p>This is a <i>test</i>.</p> 

Licenses and Attributions


Speak Your Mind

-->