XSLT for compound documents tutorial

The educational technology and digital learning wiki
Jump to navigation Jump to search

Draft

Introduction

Learning goals
  • Learn how to create XSLT that can handle XML documents that combine several vocabulariescombined XHTML/SVG/MathML/etc documents
Prerequisites
Level and target population
  • Intermediate XML/XSLT users
Remarks
  • This tutorial provides a short overview about XSLT dealing with several XML namespaces. It includes minimal knowledge needed for an XML class. Most parts also can be used in introductory web technologies class.

Introduction

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

Principles:

  • 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 !

Warning:

  • 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.

Examples

XHTML with RDF, Dublin core and our own soup

Tested on April 2013 with IE9 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">
	<head>
		<title>Compound XML document demo</title>
	</head>
	<body>
		<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
			     xmlns:dc="http://purl.org/dc/elements/1.1/">
			<dc:Description rdf:about="http://edutechwiki.unige.ch/XSLT_for_compound_documents_tutorial">
				<dc:title>XSLT for compound documents demo</dc:title>
				<dc:creator>DKS</dc:creator>
				<dc:format>XHTML + private XML + DC</dc:format>
				<dc:rights>Free as in free beer</dc:rights>
			</dc:Description>
		</rdf:RDF>
		<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>
		<hr/>
		<my:cd-list xmlns:my="http://edutechwiki.unige.ch/XML">
			<my:title>My (reduced) Hard Bop list</my:title>
			<my:cd>
				<my:artist>John Coltrane</my:artist>
				<my:title>Blue Train</my:title>
				<my:genre>Jazz</my:genre>
				<my:description>From Wikipedia: ...... </my:description>
				<my:track-list>
					<my:track no="1">
						<my:title>Blue Train</my:title>
						<my:artist>John coltrane</my:artist>
						<my:genre>Blues</my:genre>
					</my:track>
					<my:track no="2">
						<my:title>Moment's Notice</my:title>
						<my:artist>John coltrane</my:artist>
						<my:genre>Hard Bop</my:genre>
					</my:track>
				</my:track-list>
			</my:cd>
			<my:cd>
				<my:artist>Art Blakey</my:artist>
				<my:title>Moanin'</my:title>
			</my:cd>
		</my:cd-list>
	</body>
</html>

XSLT input:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:h="http://www.w3.org/1999/xhtml" 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns:dc="http://purl.org/dc/elements/1.1/" 
	xmlns:my="http://edutechwiki.unige.ch/XML" 
	version="1.0">
	
	<xsl:output method="xml" 
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
		doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="yes"/>

	<xsl:template match="h:html">
		<html>
			<head>
				<title>
					<xsl:value-of select="h:head/h:title"/>
				</title>
			</head>
			<body bgcolor="#FFFFFF">
				<xsl:apply-templates select="h:body"/>
			</body>
		</html>
	</xsl:template>
	
	<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"/>
	</xsl:template>
	
	<!-- 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>
	
	<xsl:template match="my:cd">
		<h3><xsl:value-of select="my:artist"/>: 
		    <xsl:value-of select="my:title"/> - 
		    <xsl:value-of select="my:genre"/>
		</h3>
		<p><xsl:value-of select="my:description"/></p>
		<xsl:apply-templates select="my:track-list"/>
	</xsl:template>
	
	<xsl:template match="my:track-list">
        <ol>
		<xsl:apply-templates select="my:track"/>
		</ol>
	</xsl:template>
		<xsl:template match="my:track">
        <li>
 	      <xsl:value-of select="my:title"/> -
 	      <xsl:value-of select="my:artist"/> -
 	      <xsl:value-of select="my:genre"/>
		</li>
	</xsl:template>
	
	
	<!-- metadata -->
	<xsl:template match="rdf:RDF/dc:Description">
	    <hr/>
	     <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"/>
	     </p>
	</xsl:template>	
	
  <!-- HTML tags and contents are just copied -->
	
  <xsl:template match="h:*">
     <xsl:copy>
         <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
     </xsl:copy>
   </xsl:template>
   
</xsl:stylesheet>

Links

XSLT and namespaces
RDF and Dublin Core (older version)