Newer
Older
XML / xml2xslt.xsl
<?xml version="1.0"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsl-out="[irrelevant]"
	xmlns:exsl="http://exslt.org/common"
>

	<!-- OK, this is my first bash at an XSLT transformation for a master XML document defining how to transform elements in the course handbook source into HTML and LaTeX.  Hmm, meta-stylesheet?!  -->

	
	<!--	<xsl:output method="text" encoding="ISO-8859-1" media-type="text/xml"/> -->
	<xsl:output method="xml" encoding="ISO-8859-1" cdata-section-elements="html"/>

	<!-- Produce HTML or LaTeX output? -->
	<!--	<xsl:param name="format" select="'html'"/> -->
	<xsl:param name="format">html</xsl:param>


	<!-- Define an alias for the xsl namespace to avoid confusion when generating xsl: elements in the output of this stylesheet. -->
	<xsl:namespace-alias stylesheet-prefix="xsl-out" result-prefix="xsl"/>

	

	<xsl:variable name="newline">
<xsl:text>
</xsl:text>
	</xsl:variable>

	<xsl:variable name="space"><xsl:text> </xsl:text></xsl:variable>


	<xsl:template match="/">
		
		<xsl-out:stylesheet version="1.0" exclude-result-prefixes="exsl">
		
		<xsl-out:strip-space elements="*"/>

		<!-- Would it be more accurate to call this 'subject' rather than 'department'? -->
		<xsl-out:param name="department">INFO</xsl-out:param>
		<xsl-out:param name="paper"/>
		
		<xsl-out:param name="subject-code">INFO</xsl-out:param>
		<xsl-out:param name="paper-number"/>
		
		<xsl-out:param name="showanswers" select="'no'"/>
		
		<xsl-out:param name="base-path">.</xsl-out:param>

		<!-- copy in the generated Oracle documentation code -->
		<xsl:copy-of select="document('oracle-docs.xsl')/stylesheet/*"/>
		<!-- <xsl:include href="oracle-docs.xsl" /> -->

		
		<!-- root-level documents should say what type/class of document they are (lecture, tutorial, generic, ...).  Do we want to call this a class or a type? -->
		<!--
		<xsl:variable name="doctype"><xsl:value-of select="/document/@type"/></xsl:variable>
		-->
		<xsl:choose>
			<xsl:when test="$format='html'">
			
				<!-- *** HTML Output *** -->
				
				<!-- Old version: before using xsl:namespace-alias: -->
				<!--
				<xsl-out:element name="xsl:output">
					<xsl-out:attribute name="method">html</xsl-out:attribute>
					<xsl-out:attribute name="encoding">ISO-8859-1</xsl-out:attribute>
					<xsl-out:attribute name="media-type">text/html</xsl-out:attribute>
					<xsl-out:attribute name="doctype-public">-//W3C//DTD HTML 4.01 Transitional//EN</xsl-out:attribute>
				</xsl-out:element>
				-->
				
				<!-- New, simplified version: -->
				<xsl-out:output method="html" encoding="ISO-8859-1" media-type="text/html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
			<!-- Nope, includes can only appear as a child of xsl:stylesheet. -->
			<!-- <xsl-out:include href="xml2html-root.xsl"/> -->
				
				<xsl-out:template match="/document">
					<xsl-out:comment> THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT! </xsl-out:comment>
					<HTML>
						<HEAD>
							<xsl-out:element name="LINK">
								<xsl-out:attribute name="REL">
									<xsl-out:text>Stylesheet</xsl-out:text>
								</xsl-out:attribute>
								<xsl-out:attribute name="HREF">
									<xsl-out:text>http://info-nts-12.otago.ac.nz/</xsl-out:text>
									<xsl-out:value-of select="$department"/>
									<xsl-out:value-of select="$paper"/>
									<xsl-out:text>/db_styles.css</xsl-out:text>
								</xsl-out:attribute>
								<xsl-out:attribute name="TYPE">
									<xsl-out:text>text/css</xsl-out:text>
								</xsl-out:attribute>
							</xsl-out:element>
							<TITLE>
								<xsl-out:apply-templates select="title" mode="preamble"/>
							</TITLE>
						</HEAD>
						<BODY>
							<xsl-out:apply-templates/>

							<!-- How best to approach this - certain elements that need special handling (e.g. title, author) shouldn't be passed through here as well. -->
							<!-- How about we just match certain elements that we know can be handled safely, e.g. sections, paragraphs, ...?  (...and their aliases?) -->
							<!-- Are introductions just another section, or should there be an introduction element? -->
							
							<!-- The solution is to get cleverer with our templates, e.g., multiple templates for <title> that have different match patterns (document/title, section/title). -->
							<!-- We also need to define an empty template for the <document-metadata> element so that its contents get ignored. -->
							<!-- Once these are done, we can just go apply-templates and forget about it. -->
							
							<HR/>
							<!--
								Since HTML doesn't support footnotes as such, we
								instead include them as endnotes at the end of
								the document.
							-->
							<xsl-out:if test="count(//footnote) &gt; 0">
								<H3>Notes</H3>
								<xsl-out:apply-templates select="//footnote" mode="list"/>
								<HR/>
							</xsl-out:if>
							<ADDRESS>
								<xsl-out:apply-templates select="copyright"/>
								<xsl-out:apply-templates select="/document/@cvs-id"/>
							</ADDRESS>
						</BODY>
					</HTML>
				</xsl-out:template>
			</xsl:when>
			<xsl:when test="$format='latex'">
			
				<!--
				<xsl-out:output method="text" encoding="UTF-8" media-type="text/plain"/>
				-->
				<xsl-out:output method="text" encoding="ISO-8859-1" media-type="text/plain"/>	

				<!-- Set to pdf if using PDFLaTeX, otherwise eps. -->
				<xsl-out:param name="image-format">pdf</xsl-out:param>

				<!-- Set to no if you want this to be included inside another document. -->
				<xsl-out:param name="standalone">yes</xsl-out:param>

				<!-- *** LaTeX Source Output *** -->
				<!-- Should this produce a LaTeX source fragment or an entire valid source document? -->
				<xsl-out:template match="/document">
					<xsl-out:choose>
					<!-- Could perhaps deal with loading LaTeX packages using a <latex-package> element? -->
						<xsl-out:when test="$standalone = 'yes'">
% THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT!

\documentclass[12pt,a4paper]{article}

\usepackage[margin=1in]{geometry}
\usepackage{mathpple}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{verbatim} % needed for \verbatiminput
\usepackage{latexsym} % needed for \Join
<xsl-out:apply-templates select="environment/latex-packages"/>
\usepackage[pdftex]{hyperref}

<xsl-out:apply-templates select="environment/latex-commands"/>

<xsl-out:apply-templates select="title" mode="preamble"/>
<xsl-out:apply-templates select="author" mode="preamble"/>
<xsl-out:apply-templates select="date" mode="preamble"/>

\begin{document}
	\maketitle

	<xsl-out:apply-templates/>

	\vfill
	{\scriptsize \hfill \verb+<xsl-out:apply-templates select="/document/@cvs-id"/>+}
\end{document}
						</xsl-out:when>
						<!-- Not standalone: -->
						<!-- Hmm, maybe this should go in format-master.xml... -->
						<xsl-out:when test="/document/@class = 'tutorial'">
\tutorial{<xsl-out:apply-templates select="/document/title" mode="chapter"/>}
							<!-- <xsl-out:apply-templates select="/tutorial/introduction"/>
							<xsl-out:for-each select="/tutorial/section"> -->
							<xsl-out:apply-templates select="*[not(self::title)]"/>
							<!-- </xsl-out:for-each> -->

						\vfill {\scriptsize \hfill \verb+<xsl-out:value-of select="/document/@cvs-id"/>+}
						</xsl-out:when>
						<xsl-out:when test="/document/@class = 'laboratory'">
							\lab{<xsl-out:apply-templates select="/document/title" mode="chapter"/>}
							<!-- <xsl-out:apply-templates select="/laboratory/introduction"/>
							<xsl-out:for-each select="/tutorial/section"> -->
							<xsl-out:apply-templates select="*[not(self::title)]"/>
							<!-- </xsl-out:for-each> -->

							\vfill {\scriptsize \hfill \verb+<xsl-out:value-of select="/document/@cvs-id"/>+}
						</xsl-out:when>
						<xsl-out:otherwise>
							<!-- maybe we should assume that we're in a book documentclass and issue a \chapter here? -->
							\chapter{<xsl-out:apply-templates select="/document/title" mode="chapter"/>}
						</xsl-out:otherwise>
					</xsl-out:choose>
				</xsl-out:template>
				
			<!--
				<xsl-out:element name="xsl:output">
					<xsl-out:attribute name="method">text</xsl-out:attribute>
					<xsl-out:attribute name="encoding">ISO-8859-1</xsl-out:attribute>
					<xsl-out:attribute name="media-type">text/plain</xsl-out:attribute>
				</xsl-out:element>
				-->
				
			</xsl:when>
			<xsl:otherwise>
				<xsl:message terminate="yes">
					<xsl:text>Sorry, unknown format: </xsl:text><xsl:value-of select="$format"/>
				</xsl:message>
			</xsl:otherwise>
		</xsl:choose>


		<xsl:apply-templates/>

		</xsl-out:stylesheet>
	</xsl:template>


	<!-- General-purpose template-producing template. -->
	<!--<xsl:template match="template"><![CDATA[<xsl-out:template name="]]><xsl-out:value-of select="name"/><![CDATA[" match="]]><xsl-out:value-of select="match"/><![CDATA[">]]><xsl-out:copy-of select="*[local-name(.)=$format]"/><![CDATA[</xsl-out:template>]]></xsl-out:template>-->

	<xsl:template match="template">
		<xsl-out:template>
			<!-- Much easier to just copy all attributes across verbatim rather -->
			<!-- than copying specific named attributes, because we might want to use -->
			<!-- attributes that weren't originally anticipated. -->
			<!-- Might this be a problem in future? -->
			<xsl:copy-of select="@*"/>
<!-- 			<xsl:if test="@name"> -->
<!-- 				<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute> -->
<!-- 			</xsl:if> -->
<!-- 			<xsl:if test="@match"> -->
<!-- 				<xsl:attribute name="match"><xsl:value-of select="@match"/></xsl:attribute> -->
<!-- 			</xsl:if> -->
			<!-- Copy across code that is common to all formats. -->
			<xsl:copy-of select="common/node()"/>
			<!-- Copy across code that is specific to the current format. -->
			<xsl:copy-of select="*[name(.)=$format]/node()"/>
			<!-- <xsl:copy-of select="node()"/> -->
			<!--
			<xsl:if test="$format='latex'">
				<xsl:copy-of select="latex/*"/>
			</xsl:if>
			<xsl:if test="$format='html'">
				<xsl:copy-of select="html/*"/>
			</xsl:if>
			-->
		</xsl-out:template>
	</xsl:template>



	
	<!-- This template produces a template that calls another template, i.e. it implements a template alias. -->
	<!-- The generated template probably doesn't need a name, but we'll put one in anyway. -->

	<xsl:template match="alias">
		<xsl-out:template>
			<xsl:attribute name="name"><xsl:value-of select="@source"/></xsl:attribute>
			<xsl:attribute name="match"><xsl:value-of select="@source"/></xsl:attribute>
			<xsl-out:call-template>
				<xsl:attribute name="name"><xsl:value-of select="@target"/></xsl:attribute>
			</xsl-out:call-template>
		</xsl-out:template>
	</xsl:template>
	

	<!-- This template produces a template for processing style-oriented markup, such as empahsis, foreign terms, and quotations. -->
	<!--
	<xsl:template match="d">
	</xsl-out:template match>
	-->

	<!-- This template produces a template for processing an element that refers to a hyperlink. -->
	<xsl:template match="hyperlink">
	</xsl:template>
	

	
</xsl:stylesheet>