XSLT for compound documents tutorial

The educational technology and digital learning wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.



In principle, it should be easy to transform so-called compound documents with XSLT. In practice it is not because (a) documentation only can be found by searching specialized XML web sites and (b) there are some very tricky issues. For now, this article just includes an example that demonstrates the principle with a working "life" example.

Learning goals
  • Learn how to create XSLT that can handle XML documents that combine several vocabularies, for example combined XHTML + RDF/dc + your own XML documents
Level and target population
  • Intermediate XML/XSLT users
  • Only works with more recent versions of IE explorer, i.e. version 9 or better. Should work well with older Firefox version. Not tested with Safari.

XSLT can handle XML documents that include more than one namespace.


  • Declare all the namespaces found in the XML document on top of the XSLT stylesheet
  • If you produce XHTML, you must declare the XHTML namespace twice, as default namespace and with a prefix for the XSLT rules
  • Each XPath expression must use a prefix, and that includes the XHTML ones !


  • The namespace URI/URN/URLs must be identical between the XML and the XSLT. One spelling mistake and nothing will work
  • Really, every XPath element and attribute name must have a prefix, e.g. "match" and "select" attributes. See the example below.

The XSLT engine of your navigator may not be able to handle namespaces. Transform in your XML editor, or use a server-side solution. E.g. in php, do it like this:

 # Made by DKS in 2005, still works in 2015. Substitute "YOUR" by your file names.
$xml_file = 'YOUR.xml';
$xsl_file = 'YOUR.xsl';
// load the xml file (and test first if it exists)
$dom_object = new DomDocument();
if (!file_exists($xml_file)) exit('Failed to open $xml_file');
// create dom object for the XSL stylesheet and configure the transformer
$xsl_obj = new DomDocument();
if (!file_exists($xsl_file)) exit('Failed to open $xsl_file');

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl_obj); // attach the xsl rules
$html_fragment = $proc->transformToXML($dom_object);
print ($html_fragment);


XHTML with RDF, Dublin core and our own soup

Tested on April 2013 with IE9, Chrome and Firefox 20 under Windows 7. In principle, this should work with all modern browsers ....

Life files:

XML input:

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?xml-stylesheet href="compound-cd-list.xsl" version="1.0" type="text/xsl"?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
		<title>Compound XML document demo</title>
		<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
			<dc:Description rdf:about="http://edutechwiki.unige.ch/XSLT_for_compound_documents_tutorial">
				<dc:title>XSLT for compound documents demo</dc:title>
				<dc:format>XHTML + private XML + DC</dc:format>
				<dc:rights>Free as in free beer</dc:rights>
		<p>This is an XML document with a compound vocabulary, 
		   i.e. XHMTL + CD-list + Dublin Core metadata.
		   Read the <a href="http://edutechwiki.unige.ch/en/XSLT_for_compound_documents_tutorial">XSLT for compound documents tutorial</a></p>
		<p>Stuff below belongs to a "my" namespace. Stuff at the bottom are "dc" contents. Output is ugly, no time for styling - DKS/4/2013.</p>
		<my:cd-list xmlns:my="http://edutechwiki.unige.ch/XML">
			<my:title>My (reduced) Hard Bop list</my:title>
				<my:artist>John Coltrane</my:artist>
				<my:title>Blue Train</my:title>
				<my:description>From Wikipedia: ...... </my:description>
					<my:track no="1">
						<my:title>Blue Train</my:title>
						<my:artist>John coltrane</my:artist>
					<my:track no="2">
						<my:title>Moment's Notice</my:title>
						<my:artist>John coltrane</my:artist>
						<my:genre>Hard Bop</my:genre>
				<my:artist>Art Blakey</my:artist>

XSLT file:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	<xsl:output method="xml" 
		doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/>

	<xsl:template match="h:html">
					<xsl:value-of select="h:head/h:title"/>
			<body bgcolor="#FFFFFF">
				<xsl:apply-templates select="h:body"/>
	<xsl:template match="h:body">
		<!-- for all HTML tags -->
		<xsl:apply-templates select="h:*"/>
		<!-- CD list will come first -->
		<xsl:apply-templates select="my:cd-list"/>
		<!-- Metadata at the end, skip the RDF part -->
		<xsl:apply-templates select="rdf:RDF/dc:Description"/>
	<!-- CD list contents -->
	<xsl:template match="my:cd-list">
		<h1><xsl:value-of select="my:title"/></h1>
		<xsl:apply-templates select="my:cd"/>
	<xsl:template match="my:cd">
		<h3><xsl:value-of select="my:artist"/>: 
		    <xsl:value-of select="my:title"/> - 
		    <xsl:value-of select="my:genre"/>
		<p><xsl:value-of select="my:description"/></p>
		<xsl:apply-templates select="my:track-list"/>
	<xsl:template match="my:track-list">
		<xsl:apply-templates select="my:track"/>
		<xsl:template match="my:track">
 	      <xsl:value-of select="my:title"/> -
 	      <xsl:value-of select="my:artist"/> -
 	      <xsl:value-of select="my:genre"/>
	<!-- metadata -->
	<xsl:template match="rdf:RDF/dc:Description">
	     <p style="font-size:60%;">Meta data:
	     Title:<xsl:value-of select="dc:title"/> -
	     Creator: <xsl:value-of select="dc:creator"/> -
	     Format: <xsl:value-of select="dc:format"/> -
	     Copyright: <xsl:value-of select="dc:rights"/>
  <!-- HTML tags and contents are just copied -->
  <xsl:template match="h:*">
         <xsl:copy-of select="@*"/>

Using XSLT with documents that have a single name space

The same principle applies, you must use prefixes in your XSLT code. See also XML Schema tutorial - Basics


    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="oms:outputTree">
	<title>SPSS Codebook</title> 
      <body bgcolor="#ffffff">
  <xsl:template match="oms:pivotTable/oms:dimension//oms:category[@text='Measurement']/oms:dimension/oms:category/oms:cell[@text='Nominal']">

XML (a codebook file in XML generated by SPSS)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="spss-codebook.xsl"?>

 xsi:schemaLocation="http://www.ibm.com/software/analytics/spss/xml/oms http://www.ibm.com/software/analytics/spss/xml/oms/spss-output-1.8.xsd">

  <command command="Codebook" displayOutlineValues="label" displayOutlineVariables="label" displayTableValues="label" displayTableVariables="label" lang="en" text="Codebook">
    <pivotTable subType="Variable Information" text="id">
      <dimension axis="row" text="Attributes">
	<group text="Standard Attributes">
	  <category text="Position">
	    <dimension axis="column" text="Values">
	      <category text="Value">
		<cell number="1" text="1"/>


A live example should be here: http://tecfa.unige.ch/proj/ccl/ILICS

Alternatively, you also could use XSLT functions instead, e.g.

The following two are equivalent for retrieving the same elements (but I did not test the second expression, may have a syntax error ...)




XSLT and namespaces
RDF and Dublin Core (older version)