Newer
Older
XML / format-master.xml
<?xml version="1.0"?>
<!-- This will mostly be a list of element names to match, along with the corresponding HTML and LaTeX handling XSLT code. -->
<stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<hyperlink>
		<element>OraAdmin</element>
		<url>http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96521/toc.htm</url>
		<label>test<!--<xsl:call-template name="OracleServer"/> Database Administrator<xsl:call-template name="apostrophe"/>s Guide--></label>
	</hyperlink>

	<template name="PaperCode" match="PaperCode">
		<latex><xsl:value-of select="$department"/>~<xsl:value-of select="$paper"/></latex>
		<html>
			<xsl:value-of select="$department"/>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
			<xsl:value-of select="$paper"/>
		</html>
	</template>

	<template name="OracleServer" match="OracleServer">
		<latex>Oracle9\textit{i}</latex>
		<html>Oracle9<I>i</I></html>
	</template>


	<template name="paragraph" match="paragraph">
		<latex><xsl:text>


</xsl:text><xsl:if test="@indent = 'no'">\noindent </xsl:if><xsl:apply-templates/><xsl:text>
	
		
</xsl:text></latex>
		<!-- HTML is weird about what things you cannot include inside paragraphs (e.g. lists of any kind).  However, the end P tag is optional, so one option might simply be not to output it. -->
		<html><P><xsl:apply-templates/></P></html>
	</template>
	<alias source="para" target="paragraph"/>
	<alias source="p" target="paragraph"/>
	
	
	<!-- Special characters (should maybe all be named, in case they need to be called explicitly with xsl:call-template) -->
	<!-- I'd like to use the Unicode names for these; will look them up some time! -->
	<template name="newline" match="newline">
		<latex> \\</latex>
		<html><BR/></html>
	</template>
	
	<template name="page-break" match="page-break">
		<latex>\newpage</latex>
	</template>
	<alias source="new-page" target="page-break"/>

	<template name="non-breaking-space" match="non-breaking-space">
		<latex>~</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text></html>
	</template>	
	<alias source="nbsp" target="non-breaking-space"/>

	<!--
		Technically we don't need to disable output escaping for all of
		these, but it's probably better to code them all consistently,
		rather than trying to figure out which ones do need it (e.g.,
		&#160; for a non-breaking space) and which ones don't (e.g.,
		&amp;).
	-->
	<template name="ellipsis-sign" match="ellipsis-sign">
		<latex>{\ldots}</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8230;</xsl:text></html>
	</template>
	<alias source="etc" target="ellipsis-sign"/>
	<alias source="ellipsis" target="ellipsis-sign"/>
	<alias source="dots" target="ellipsis-sign"/>

	<!-- Misc typographic symbols -->
	<template name="endash" match="endash">	
		<latex>--</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8211;</xsl:text></html>
	</template>
	<alias source="en-dash" target="endash"/>

	<template name="emdash" match="emdash">
		<latex>---</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8212;</xsl:text></html>
	</template>
	<alias source="em-dash" target="emdash"/>

	<template name="apostrophe" match="apostrophe">
		<latex>'</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8217;</xsl:text></html>
	</template>

	<template name="dollar" match="dollar">
		<latex>\$</latex>
		<html>$</html>
	</template>

	<template name="percent-sign" match="percent-sign">
		<latex>\%</latex>
		<html>%</html>
	</template>

	<template name="ampersand" match="ampersand">
		<latex>\&amp;</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;amp;</xsl:text></html>
	</template>

	<template name="trademark-sign" match="trademark-sign">
		<latex>\texttrademark</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8482;</xsl:text></html>
	</template>
	<alias source="trademark" target="trademark-sign"/>
	<alias source="TM" target="trademark-sign"/>

	<template name="copyright-sign" match="copyright-sign">
		<latex>\copyright</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#169;</xsl:text></html>
	</template>
	<!--	<alias source="copyright" target="copyright-sign"/> -->

	<template name="degree-sign" match="degree-sign">
		<latex>$^{\circ}$</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#176;</xsl:text></html>
	</template>
	<alias source="degrees" target="degree-sign"/>

	<template name="section-sign" match="section-sign">
		<latex>{\S}</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#167;</xsl:text></html>
	</template>

	<template name="paragraph-sign" match="paragraph-sign">
		<latex>{\P}</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#182;</xsl:text></html>
	</template>

	<template name="pi" match="pi">
		<latex>$\pi$</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#960;</xsl:text></html>
	</template>

	<template name="LaTeX" match="LaTeX">
		<latex>{\latex}</latex>
		<html>LaTeX</html>
	</template>
	<alias source="latex" target="LaTeX"/>

	<!-- Note: no equivalent of \triangleright in HTML, use right arrow instead. -->
	<template name="menu-separator" match="menu-separator">
		<latex>$\triangleright$</latex>
		<html><xsl:text disable-output-escaping="yes">&amp;#8594;</xsl:text></html>
	</template>


	<!-- Special styles -->
	<template name="emph" match="emph">
		<latex>\emph{<xsl:apply-templates/>}</latex>
		<html><EM><xsl:apply-templates/></EM></html>
	</template>
	<template name="italic" match="italic">
		<latex>\textit{<xsl:apply-templates/>}</latex>
		<html><I><xsl:apply-templates/></I></html>
	</template>
	<template name="strong" match="strong">
		<latex>\textbf{<xsl:apply-templates/>}</latex>
		<html><STRONG><xsl:apply-templates/></STRONG></html>
	</template>
	<template name="bold" match="bold">
		<latex>\textbf{<xsl:apply-templates/>}</latex>
		<html><B><xsl:apply-templates/></B></html>
	</template>
	<template name="term" match="term">
		<latex>\term{<xsl:apply-templates/>}</latex>
		<html><I CLASS="term"><xsl:apply-templates/></I></html>
	</template>
	<template name="foreign" match="foreign">
		<latex>\foreign{<xsl:apply-templates/>}</latex>
		<html><I CLASS="foreign"><xsl:apply-templates/></I></html>
	</template>
	<template name="code" match="code">
		<!-- <latex>\code{<xsl:apply-templates/>}</latex> -->
		<latex>\verb+<xsl:apply-templates/>+</latex>
		<html><CODE><xsl:apply-templates/></CODE></html>
	</template>

	<template name="code-block" match="code-block">
		<latex>
			\begin{verbatim}
				<xsl:apply-templates/>
			\end{verbatim}
		</latex>
		<html>
			<PRE CLASS="code"><xsl:apply-templates/></PRE>
		</html>
	</template>
	
	<!-- Quoted text. Handles nested quotes correctly. -->
	<template name="single-quote" match="quote[@single='yes']">
		<latex>`<xsl:apply-templates/>'</latex>
		<html>
			<xsl:text disable-output-escaping="yes">&amp;#8216;</xsl:text>
			<xsl:apply-templates/>
			<xsl:text disable-output-escaping="yes">&amp;#8217;</xsl:text>
		</html>
	</template>
	<template name="double-quote" match="quote">
		<latex>``<xsl:apply-templates/>''</latex>
		<html>
			<xsl:text disable-output-escaping="yes">&amp;#8220;</xsl:text>
			<xsl:apply-templates/>
			<xsl:text disable-output-escaping="yes">&amp;#8221;</xsl:text>
		</html>
	</template>
	<template name="nested-double-quote" match="quote//quote">
		<common>
			<xsl:choose>
				<xsl:when test="(count(ancestor::quote) mod 2) = 0">
					<xsl:call-template name="double-quote"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="single-quote"/>
				</xsl:otherwise>
			</xsl:choose>
		</common>
	</template>
	<template name="nested-single-quote" match="quote[@single='yes']//quote" priority="2">
		<common>
			<xsl:choose>
				<xsl:when test="(count(ancestor::quote) mod 2) = 0">
					<xsl:call-template name="single-quote"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="double-quote"/>
				</xsl:otherwise>
			</xsl:choose>
		</common>
	</template>
	<alias source="q" target="single-quote"/>
	<alias source="qq" target="double-quote"/>


	<!-- Environments (in the LaTeX terminology): enumerated, ordered, and numbered lists. -->

	<!-- Unordered lists -->
	
	<template name="itemised-list" match="itemised-list">
		<latex>
			\begin{itemize}
				<xsl:apply-templates/>
			\end{itemize}
		</latex>
		<html>
			<UL>
				<xsl:apply-templates/>
			</UL>
		</html>
	</template>
	
	<alias source="itemize" target="itemised-list"/>
	<alias source="unordered-list" target="itemised-list"/>
	<alias source="bulleted-list" target="itemised-list"/>
	<alias source="bullet-list" target="itemised-list"/>
	<alias source="bullet-points" target="itemised-list"/>


	<!-- Ordered lists -->
	
	<template name="enumerated-list" match="enumerated-list">
		<latex>
			\begin{enumerate}
				<xsl:apply-templates/>
			\end{enumerate}
		</latex>
		<html>
			<OL>
				<xsl:apply-templates/>
			</OL>
		</html>
	</template>

	<alias source="enumerate" target="enumerated-list"/>
	<alias source="ordered-list" target="enumerated-list"/>
	<alias source="numbered-list" target="enumerated-list"/>
	
	
	<!-- Definition or description lists. -->
	<template name="definition-list" match="definition-list">
		<latex>
			\begin{description}
				<xsl:apply-templates/>
			\end{description}
		</latex>
		<html>
			<!--
				Hmm, <DL>s in HTML tend to come out with the spacing a
				bit wrong (or maybe it's just my browser?). Might using
				a table be better? Messy if the keyword is long,
				though, and you don't get a proper hanging indent...
				
				Maybe this is something thaty could be handled in CSS 
				instead.
			-->
			<DL>
				<xsl:apply-templates/>
			</DL>
<!-- 			<TABLE BORDER="0" CELLPADDING="10"> -->
<!-- 				<xsl:apply-templates/> -->
<!-- 			</TABLE> -->
		</html>
	</template>
	
	<alias source="description-list" target="definition-list"/>


	<!-- List elements -->
	<template name="item" match="item">
		<latex>\item <xsl:apply-templates/></latex>
		<html>
			<xsl:choose>
				<xsl:when test="count(paragraph|para|p) != 0">
					<LI><xsl:apply-templates/></LI>
				</xsl:when>
				<xsl:otherwise>
					<LI><P><xsl:apply-templates/></P></LI>
				</xsl:otherwise>
			</xsl:choose>
		</html>
	</template>

	<template name="definition-item" match="definition-list/item">
		<latex>\item[<xsl:apply-templates select="keyword"/>] <xsl:apply-templates select="definition"/></latex>
		<html>
			<DT><P><xsl:apply-templates select="keyword"/></P></DT>
			<DD><xsl:apply-templates select="definition|description|discourse"/></DD>
<!-- 			<TR VALIGN="TOP"> -->
<!-- 				<TD><P><xsl:apply-templates select="keyword"/></P></TD> -->
<!-- 				<TD><xsl:apply-templates select="definition|description|discourse"/></TD> -->
<!-- 			</TR> -->
		</html>
	</template>
	
	<template name="keyword" match="keyword">
		<latex><xsl:apply-templates/></latex>
		<html><STRONG><xsl:apply-templates/></STRONG></html>
	</template>
	
	<alias source="topic" target="keyword"/>
	
	<!--
		Need to provide some context here as, e.g., description is also
		used within <image>.
	-->
	<template name="definition" match="item/definition|item/description|item/discourse">
		<html>
			<xsl:choose>
				<xsl:when test="count(paragraph|para|p) != 0">
					<xsl:apply-templates/>
				</xsl:when>
				<xsl:otherwise>
					<P><xsl:apply-templates/></P>
				</xsl:otherwise>
			</xsl:choose>
		</html>
	</template>
	
	
	<!-- Sections, subsections, subsubsections, ... -->
	<template name="section" match="section">
		<latex>
			<xsl:apply-templates>
				<xsl:with-param name="label">
					<xsl:value-of select="@label"/>
					<xsl:if test="not(@label)">THERE_IS_NO_LABEL</xsl:if>
				</xsl:with-param>
			</xsl:apply-templates>
		</latex>
		<html>
			<xsl:if test="@label"><A NAME="{@label}"></A></xsl:if>
			<xsl:apply-templates/>
			<xsl:if test="count(.//footnote) &gt; 0">
				<P><STRONG>Notes:</STRONG></P>
				<xsl:apply-templates select=".//footnote" mode="list"/>
			</xsl:if>
		</html>
	</template>
	
	
	<!-- Footnotes. -->
	<template name="footnote" match="footnote">
		<latex>\footnote{<xsl:apply-templates/>}</latex>
		<html>
			<A>
				<xsl:attribute name="NAME">
					<xsl:text>footnote-</xsl:text>
					<xsl:number level="any" count="footnote"/>
					<xsl:text>-source</xsl:text>
				</xsl:attribute>
				<xsl:attribute name="HREF">
					<xsl:text>#footnote-</xsl:text>
					<xsl:number level="any" count="footnote"/>
					<xsl:text>-target</xsl:text>
				</xsl:attribute>
				<xsl:text>[</xsl:text>
				<xsl:number level="any" count="footnote"/>
				<xsl:text>]</xsl:text>
			</A>
		</html>
	</template>

	<!-- This is only required for HTML, as LaTeX does it all for you. -->
	<template name="footnote-list" match="footnote" mode="list">
		<html>
			<P>
				<A>
					<xsl:attribute name="NAME">
						<xsl:text>footnote-</xsl:text>
						<xsl:number level="any" count="footnote"/>
						<xsl:text>-target</xsl:text>
					</xsl:attribute>
					<xsl:text>[</xsl:text>
					<xsl:number level="any" count="footnote"/>
					<xsl:text>]</xsl:text>
				</A>
				<xsl:text> </xsl:text>
				<xsl:apply-templates/>
				<xsl:text> </xsl:text>
				<A>
					<xsl:attribute name="HREF">
						<xsl:text>#footnote-</xsl:text>
						<xsl:number level="any" count="footnote"/>
						<xsl:text>-source</xsl:text>
					</xsl:attribute>
					<xsl:text>[Back]</xsl:text>
				</A>
			</P>
		</html>
	</template>

	
	<!-- Document preamble stuff: title, author, date. -->
	<template name="preamble-title" match="document/title" mode="preamble">
		<latex>\title{<xsl:apply-templates/>}</latex>
		<html>
			<xsl:apply-templates/>
		</html>
	</template>

	<template name="preamble-author" match="document/author" mode="preamble">
		<latex>\author{<xsl:apply-templates/>}</latex>
	</template>

	<template name="preamble-date" match="document/date" mode="preamble">
		<latex>\date{<xsl:apply-templates/>}</latex>
	</template>

	<template name="document-title" match="document/title">
		<!-- Need to do something sensible for LaTeX here. -->
		<latex><xsl:if test="$standalone = 'no'"></xsl:if></latex>
		<html>
			<H1><xsl:apply-templates/></H1>
		</html>
	</template>
	
	<template name="document-author" match="document/author"/>
	<template name="document-date" match="document/date"/>

	<!--
		Generate some number of "sub" so we can produce, e.g., "subsubsection"
		for LaTeX.
		
		Parameter: $depth
		Meaning: the number of repetitions (0 <= $depth <= 2, but there's no
		need to do bounds checking at the upper end, because that's already
		dealt with in the "section-title" template below).
	-->
	<template name="generate-subs">
		<latex>
			<xsl:param name="depth">1</xsl:param>
			<xsl:if test="$depth &gt; 1">
				<xsl:text>sub</xsl:text>
				<xsl:call-template name="generate-subs">
					<xsl:with-param name="depth"><xsl:value-of select="$depth - 1"/></xsl:with-param>
				</xsl:call-template>
			</xsl:if>
		</latex>
	</template>
	
	<template name="section-title" match="section/title">
		<latex>
			<xsl:param name="label"/>
			<xsl:text>\</xsl:text>
			<!-- Depth calculation for LaTeX hasn't been tested! -->
			<xsl:call-template name="generate-subs">
				<xsl:with-param name="depth">
					<xsl:number value="count(ancestor::section)"/>
					<xsl:if test="count(ancestor::section) &gt; 3">3</xsl:if>
				</xsl:with-param>
			</xsl:call-template>
			<xsl:text>section{</xsl:text>
			<xsl:apply-templates/><xsl:text>}</xsl:text>
			<xsl:if test="$label != 'THERE_IS_NO_LABEL'">
				\label{<xsl:value-of select="$label"/>}
			</xsl:if>
		</latex>
		<html>
			<xsl:variable name="depth">
				<xsl:number value="1 + count(ancestor::section)"/>
				<xsl:if test="1 + count(ancestor::section) &gt; 6">6</xsl:if>
			</xsl:variable>
			<xsl:text disable-output-escaping="yes">&lt;H</xsl:text><xsl:number value="$depth"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
			<xsl:number count="section" level="multiple" format="1.1.1.1.1.1"/>
			<xsl:text> </xsl:text>
			<xsl:apply-templates/>
			<xsl:text disable-output-escaping="yes">&lt;/H</xsl:text><xsl:number value="$depth"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
		</html>
	</template>


	<!-- A quotation. -->
	<template name="quotation" match="quotation">
		<latex>
			<xsl:choose>
				<xsl:when test="(@align = 'left') or (@align = 'right')">
					\begin{flush<xsl:value-of select="@align"/>}\itshape <xsl:apply-templates/>\end{flush<xsl:value-of select="@align"/>}
				</xsl:when>
				<xsl:when test="@align = 'center'">
					\begin{center}\itshape <xsl:apply-templates/>\end{center}
				</xsl:when>
				<xsl:otherwise>
					\begin{quote}\itshape <xsl:apply-templates/>\end{quote}
				</xsl:otherwise>
			</xsl:choose>
		</latex>
		<html>
			<P>
				<xsl:if test="@align">
					<xsl:attribute name="ALIGN">
						<xsl:value-of select="@align"/>
					</xsl:attribute>
				</xsl:if>
				<I><xsl:apply-templates/></I>
			</P>
		</html>
	</template>
	
	
	<!--
		Anything inside an ID element is ignored completely. (More accurately,
		we pick bits out of it manually as required; it's only ignored by
		apply-templates.)
	-->
	<template name="id" match="id"/>
	
	
	<!--
		Anything inside an OMIT element is also ignored completely.
	-->
	<template name="omit" match="omit"/>
	
	
	<!-- Tabular structures (LaTeX {tabular}, HTML <TABLE>). -->
	<template name="tabular" match="tabular">
		<latex>
			<xsl:text>\begin{</xsl:text>
			<xsl:choose>
				<xsl:when test="(@align = 'left') or (@align = 'right')">
					<xsl:text>flush</xsl:text><xsl:value-of select="@align"/>
				</xsl:when>
				<xsl:when test="@align = 'center'">
					<xsl:text>center</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>flushleft</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>}</xsl:text>
			\begin{tabular}{<xsl:apply-templates select="columns"/>}
				<xsl:apply-templates select="tabular-header"/>
				<xsl:apply-templates select="tabular-body"/>
				<xsl:apply-templates select="tabular-footer"/>
			\end{tabular}
			<xsl:text>\end{</xsl:text>
			<xsl:choose>
				<xsl:when test="(@align = 'left') or (@align = 'right')">
					<xsl:text>flush</xsl:text><xsl:value-of select="@align"/>
				</xsl:when>
				<xsl:when test="@align = 'center'">
					<xsl:text>center</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>flushleft</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>}</xsl:text>
		</latex>
		<html>
			<TABLE>
				<xsl:attribute name="BORDER">
					<xsl:value-of select="@border"/>
					<xsl:if test="not(@border)">0</xsl:if>
				</xsl:attribute>
				<xsl:if test="@align">
					<xsl:attribute name="ALIGN">
						<xsl:value-of select="@align"/>
					</xsl:attribute>
				</xsl:if>
				<!--
					Note different ordering of tabular components:
					HTML requires THEAD and TFOOT to precede TBODY.
				-->
				<xsl:apply-templates select="tabular-header"/>
				<xsl:apply-templates select="tabular-footer"/>
				<xsl:apply-templates select="tabular-body"/>
			</TABLE>
		</html>
	</template>
	
	<!-- Set left- or right-border to "|" to include a column separator. -->
	<template name="aligned-column" match="column[@align]">
		<latex>
			<xsl:value-of select="@left-border"/>
			<xsl:value-of select="substring(@align, 1, 1)"/>
			<xsl:value-of select="@right-border"/>
		</latex>
	</template>
	
	<template name="unaligned-column" match="column[not(@align)]">
		<latex>
			<xsl:value-of select="@left-border"/>
			<xsl:text>l</xsl:text>
			<xsl:value-of select="@right-border"/>
		</latex>
	</template>
	
	<template name="tabular-header" match="tabular-header">
		<latex></latex>
		<html>
			<THEAD>
				<xsl:apply-templates/>
			</THEAD>
		</html>
	</template>

	<template name="tabular-footer" match="tabular-footer">
		<latex></latex>
		<html>
			<TFOOT>
				<xsl:apply-templates/>
			</TFOOT>
		</html>
	</template>

	<template name="tabular-body" match="tabular-body">
		<latex><xsl:apply-templates/></latex>
		<html>
			<TBODY>
				<xsl:apply-templates/>
			</TBODY>
		</html>
	</template>

	<template name="row" match="row">
		<latex><xsl:apply-templates/> \\<xsl:if test="@no-page-break = 'yes'">*</xsl:if><xsl:text> </xsl:text></latex>
		<html>
			<TR>
				<xsl:apply-templates/>
			</TR>
		</html>
	</template>

	<template name="row-rule-full" match="row-rule[not(@columns)]">
		<latex>\hline</latex>
	</template>

	<template name="row-rule-partial" match="row-rule[@columns]">
		<latex>\cline{<xsl:value-of select="@columns"/>}</latex>
	</template>
	
	<template match="cell" mode="multi-row">
		<latex>
			<xsl:text>\multirow{</xsl:text>
			<xsl:value-of select="@rows"/>
			<xsl:text>}{*}{</xsl:text>
			<xsl:apply-templates/>
			<xsl:text>}</xsl:text>
		</latex>
	</template>
	
	<template match="cell" mode="multi-column">
		<latex>
			<xsl:param name="num-columns">1</xsl:param>
			<xsl:text>\multicolumn{</xsl:text>
			<xsl:value-of select="$num-columns"/>
			<xsl:text>}{</xsl:text>
			<xsl:value-of select="@left-border"/>
			<xsl:choose>
				<xsl:when test="@align">
					<xsl:value-of select="@align"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>l</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="@right-border"/>
			<xsl:text>}{</xsl:text>
			<xsl:choose>
				<xsl:when test="@rows">
					<xsl:apply-templates select="." mode="multi-row"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>}</xsl:text>
		</latex>
	</template>
	
	<template match="cell">
		<common>
			<!-- position() doesn't seem to work very well in this context -->
			<xsl:variable name="column-no"><xsl:number/></xsl:variable>
		</common>
		<!--
			Doing this sensibly for LaTeX is actually pretty ugly,
			because different attribute combinations produce different
			code :(
			
			This is the sort of algorithm that can only really be
			clearly described by a flow chart :)
		-->
		<latex>
			<xsl:choose>
				<xsl:when test="@columns">
					<xsl:apply-templates select="." mode="multi-column">
						<xsl:with-param name="num-columns" select="@columns"/>
					</xsl:apply-templates>
				</xsl:when>
				<xsl:when test="@align">
					<xsl:apply-templates select="." mode="multi-column"/>
				</xsl:when>
				<xsl:when test="@rows">
					<xsl:apply-templates select="." mode="multi-row"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="$column-no != last()"> &amp; </xsl:if>
		</latex>
		<!--
			It's much easier in HTML, because a <TD> is a <TD> is a
			<TD>, regardless of the attributes supplied.
		-->
		<html>
			<TD>
				<xsl:attribute name="ALIGN">
					<xsl:value-of select="@align"/>
					<xsl:if test="not(@align)">
						<xsl:value-of select="ancestor::tabular/columns/column[position() = $column-no]/@align"/>
						<xsl:if test="not(ancestor::tabular/columns/column[position() = $column-no]/@align)">left</xsl:if>
					</xsl:if>
				</xsl:attribute>
				<xsl:attribute name="COLSPAN">
					<xsl:value-of select="@columns"/>
					<xsl:if test="not(@columns)">1</xsl:if>
				</xsl:attribute>
				<xsl:attribute name="ROWSPAN">
					<xsl:value-of select="@rows"/>
					<xsl:if test="not(@rows)">1</xsl:if>
				</xsl:attribute>
				<xsl:apply-templates/>
			</TD>
		</html>
	</template>
	
	
	<!-- Hyperlinks. -->
	<template name="hyperlink-label" match="hyperlink[@label]">
		<latex>\hyperref[<xsl:value-of select="@label"/>]{<xsl:apply-templates/>}</latex>
		<html><A HREF="#{@label}"><xsl:apply-templates/></A></html>
	</template>

	<template name="hyperlink-url" match="hyperlink[@url and node()]">
		<latex>\href{<xsl:value-of select="@url"/>}{<xsl:apply-templates/>}</latex>
		<html><A HREF="{@url}"><xsl:apply-templates/></A></html>
	</template>

	<!-- An empty <hyperlink> element with a URL defaults to the URL as the link text. -->
	<template name="empty-hyperlink-url" match="hyperlink[@url and not(node())]">
		<!-- Maybe we should be using the url package and \url{...}? -->
		<latex>\href{<xsl:value-of select="@url"/>}{\texttt{<xsl:value-of select="@url"/>}}</latex>
		<html><A HREF="{@url}"><CODE><xsl:value-of select="@url"/></CODE></A></html>
	</template>
	
	<!-- A plain URL. -->
	<template name="url" match="url">
		<!-- Maybe we should be using the url package and \url{...}? -->
		<latex>\texttt{<xsl:apply-templates/>}</latex>
		<html><CODE><xsl:apply-templates/></CODE></html>
	</template>
	
	<alias source="uri" target="url"/>
	<alias source="email" target="url"/>
	<alias source="email-address" target="url"/>


	<!-- References. -->
	<template name="reference" match="reference">
		<common>
			<!-- Find the element whose label is the same as our label. -->
			<xsl:apply-templates select="//*[@label = current()/@label]" mode="reference"/>
		</common>
	</template>
	
	<template name="section-reference" match="section" mode="reference">
		<!-- It's probably more consistent to just use \ref* and insert "Section" ourselves rather than let LaTeX do it. -->
		<latex>Section~\ref*{<xsl:value-of select="@label"/>}</latex>
		<html>
			<xsl:text disable-output-escaping="yes">Section&amp;#160;</xsl:text>
			<xsl:number count="figure" level="multiple" format="1.1.1.1.1.1"/>
		</html>
	</template>
	
	<template name="figure-reference" match="figure" mode="reference">
		<latex>Figure~\ref*{<xsl:value-of select="@label"/>}</latex>
		<html>
			<xsl:text disable-output-escaping="yes">Figure&amp;#160;</xsl:text>
			<xsl:number count="figure" level="any" format="1"/>
		</html>
	</template>
	
	<template name="table-reference" match="table" mode="reference">
		<latex>Table~\ref*{<xsl:value-of select="@label"/>}</latex>
		<html>
			<xsl:text disable-output-escaping="yes">Table&amp;#160;</xsl:text>
			<xsl:number count="table" level="any" format="1"/>
		</html>
	</template>
	
	
	<!-- Figures & tables. -->
	<template name="figure" match="figure">
		<latex>
			\begin{figure}
				\centering
				<xsl:apply-templates select="*[not(self::caption)]"/>
				<xsl:if test="count(caption) != 0">
					\caption{<xsl:apply-templates select="caption"/>}
					<xsl:if test="@label">\label{<xsl:value-of select="@label"/>}</xsl:if>
				</xsl:if>
			\end{figure}
		</latex>
		<html>
			<DIV CLASS="figure">
				<xsl:if test="@label"><A NAME="{@label}"></A></xsl:if>
				<xsl:apply-templates select="*[not(self::caption)]"/>
				<xsl:if test="count(caption) != 0">
					<BR/>
					<xsl:apply-templates select="caption"/>
				</xsl:if>
			</DIV>
		</html>
	</template>
	
	<template name="table" match="table">
		<latex>
			\begin{table}
				\centering
				<xsl:apply-templates select="*[not(self::caption)]"/>
				<xsl:if test="count(caption) != 0">
					\caption{<xsl:apply-templates select="caption"/>}
					<xsl:if test="@label">\label{<xsl:value-of select="@label"/>}</xsl:if>
				</xsl:if>
			\end{table}
		</latex>
		<html>
			<DIV CLASS="table">
				<xsl:if test="@label"><A NAME="{@label}"></A></xsl:if>
				<xsl:apply-templates select="*[not(self::caption)]"/>
				<xsl:if test="count(caption) != 0">
					<BR/>
					<xsl:apply-templates select="caption"/>
				</xsl:if>
			</DIV>
		</html>
	</template>
	
	<template name="figure-caption" match="figure/caption">
		<latex><xsl:apply-templates/></latex>
		<html>
			<STRONG>Figure <xsl:number count="figure" level="any" format="1"/>. </STRONG><xsl:apply-templates/>
		</html>
	</template>
	
	<template name="table-caption" match="table/caption">
		<latex><xsl:apply-templates/></latex>
		<html>
			<STRONG>Table <xsl:number count="table" level="any" format="1"/>. </STRONG><xsl:apply-templates/>
		</html>
	</template>
	
	
	<!-- Images. Includes a scaling factor for LaTeX.  To do: alternatively, allow defining the scaling factor by specifying what proportion of the page width the image should occupy. -->
	
	<!-- LaTeX images are always handled the same way regardless. -->
	<template match="image" mode="latex">
		<latex>
			<xsl:text>\includegraphics</xsl:text>
			<xsl:if test="count(latex-scaling) != 0">
				<xsl:text>[scale=</xsl:text>
				<xsl:value-of select="latex-scaling"/>
				<xsl:text>]</xsl:text>
			</xsl:if>
			<!-- Use whatever standard image folder is appropriate for the context. -->
			<xsl:choose>
				<xsl:when test="name(/child::node()[1])='tutorial'">
					<xsl:text>{\tutroot/</xsl:text><xsl:value-of select="/tutorial/id/number-code"/><xsl:text>-</xsl:text><xsl:value-of select="/tutorial/id/title-code"/><xsl:text>/Graphics/</xsl:text>
				</xsl:when>
				<xsl:when test="name(/child::node()[1])='laboratory'">
					<xsl:text>{\labroot/</xsl:text><xsl:value-of select="/laboratory/id/number-code"/><xsl:text>-</xsl:text><xsl:value-of select="/laboratory/id/title-code"/><xsl:text>/Graphics/</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>{images/</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="basename"/>
			<xsl:text>.</xsl:text>
			<xsl:choose>
				<xsl:when test="count(format) != 0">
					<xsl:value-of select="format"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$image-format"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text>}</xsl:text>
		</latex>
	</template>
	
	<template match="image[count(provide-large-version) = 0]">
		<latex><xsl:apply-templates select="." mode="latex"/></latex>
		<html>
			<IMG BORDER="0">
				<xsl:attribute name="SRC">
					<xsl:text>images/</xsl:text>
					<xsl:value-of select="basename"/>
					<xsl:text>.</xsl:text>
					<xsl:choose>
						<xsl:when test="count(format) != 0">
							<xsl:value-of select="format"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>png</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:attribute name="ALT"><xsl:apply-templates select="description"/></xsl:attribute>
			</IMG>
		</html>
	</template>

	<template match="image[count(provide-large-version) != 0]">
		<latex><xsl:apply-templates select="." mode="latex"/></latex>
		<html>
			<A>
				<xsl:attribute name="HREF">
					<xsl:text>images/</xsl:text>
					<xsl:value-of select="basename"/>
					<xsl:text>-zoom.</xsl:text>
					<xsl:choose>
						<xsl:when test="count(format) != 0">
							<xsl:value-of select="format"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>png</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<IMG BORDER="0">
					<xsl:attribute name="SRC">
						<xsl:text>images/</xsl:text>
						<xsl:value-of select="basename"/>
						<xsl:text>.</xsl:text>
						<xsl:choose>
							<xsl:when test="count(format) != 0">
								<xsl:value-of select="format"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>png</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
					<xsl:attribute name="ALT">
						<xsl:apply-templates select="description"/>
					</xsl:attribute>
				</IMG>
				<BR CLEAR="right"/>
				<xsl:text>(Larger version)</xsl:text>
			</A>
		</html>
	</template>
	
	
	<!--
		Include another XML document into the current one. Including
		LaTeX should be done using <latex-input>. Nothing for HTML
		includes at present, although this may not actually be necessary
		anyway given that HTML doesn't natively support this in the
		first place. You're probably better off just generating a
		separate document and linking to it.
		
		It's difficult to produce HTML fragments because the XSLT
		processor seems to always generate a DOCTYPE when producing
		HTML, whether you want it or not. Maybe you can turn this off?
		
		@file: Name of file to include. REQUIRED.
	-->
	<template name="include-xml" match="include-document">
		<common>
			<xsl:apply-templates select="document(@file)"/>
		</common>
	</template>
	
	
	<!--
		Environment setup. This enables you to load environment settings
		(e.g., required LaTeX packages, etc.) from an external
		configuration file. The master style sheet automatically loads a
		file called "environment.xml"; you don't need to do anything for
		this file to be loaded. Multiple configuration files may be
		loaded, as long as they all have different names.
		
		You can also define environment entries inline by omitting
		the file attribute.
		
		@file: Name of environment file. OPTIONAL.
	-->
	<template name="environment" match="environment[@file]">
		<xsl:apply-templates select="document(@file)/environment/*"/>
	</template>
	
	<template name="latex-package" match="latex-packages/package">
		<latex>\usepackage<xsl:if test="@options">[<xsl:value-of select="@options"/>]</xsl:if>{<xsl:value-of select="."/>}</latex>
	</template>
		
	<template name="latex-command" match="latex-packages/command">
		<latex><xsl:value-of select="."/></latex>
	</template>
	
	
	<!-- Sometimes we might need to embed raw code to handle something tricky. -->
	<template name="raw-latex" match="raw-code[@format = 'latex']">
		<latex><xsl:apply-templates/></latex>
	</template>
		
	<template name="raw-html" match="raw-code[@format = 'html']">
		<html><xsl:copy-of select="*"/></html>
	</template>
		
	<!-- Generate a LaTeX \input{} macro. -->
	<template name="latex-input" match="latex-input">
		<latex>\input{<xsl:value-of select="."/>}</latex>
	</template>

	
	<!--
		Conditional processing depending on the format.
		
		@format: the format under which this XML is to be processed.
	-->
	<template name="process-when-latex" match="process-when[@format = 'latex']">
		<latex><xsl:apply-templates/></latex>
	</template>
	
	<template name="process-when-html" match="process-when[@format = 'html']">
		<html><xsl:apply-templates/></html>
	</template>
	
	
	<!--
		Basic maths stuff. Anything more complicated is... complicated.
		We should probably be using MathML, but we still have to mangle
		it into HTML regardless.
		
		A <math> inside a <paragraph> is assumed to be inline, while a
		<math> on its own is assumed to be displayed in its own paragraph.
	-->
	<template name="math-inline" match="paragraph//math|para//math|p//math">
		<latex>\(<xsl:apply-templates/>\)</latex>
		<html><xsl:apply-templates/></html>
	</template>
	
	<template name="math-display" match="math">
		<latex>\[<xsl:apply-templates/>\]</latex>
		<html><P ALIGN="center"><xsl:apply-templates/></P></html>
	</template>
	
	<!-- Super- and subscript. -->
	<template name="superscript" match="math//superscript">
		<latex>^{<xsl:apply-templates/>}</latex>
		<html><SUP><xsl:apply-templates/></SUP></html>
	</template>
	
	<template name="subscript" match="math//subscript">
		<latex>_{<xsl:apply-templates/>}</latex>
		<html><SUB><xsl:apply-templates/></SUB></html>
	</template>
	
	<!-- Basic mathematical operators. -->
	<template name="plus-operator" match="math//plus">
		<common>+</common>
	</template>
	
	<template name="minus-operator" match="math//minus">
		<common>-</common>
	</template>
	
	<template name="times-operator" match="math//times">
		<latex>\times</latex>
		<html><xsl:text disable-output-escaping="yes"> &amp;#215; </xsl:text></html>
	</template>
	
	<template name="divide-operator" match="math//divide">
		<common>/</common>
	</template>
	
	<template name="equals-operator" match="math//equals">
		<common> = </common>
	</template>
	
	<!--
		We'll assume the names used by LaTeX here, as they work out correct
		anyway. There's actually nothing to stop you using any name you like,
		except that it'll probably die horribly in LaTeX :)
	-->
	<template name="log-like-function" match="math//function">
		<latex>\<xsl:value-of select="@name"/></latex>
		<html><xsl:value-of select="@name"/></html>
	</template>
	
	<!-- Any mathematical variable names. -->
	<template name="math-variable" match="math//variable">
		<latex><xsl:apply-templates/></latex>
		<html><I><xsl:apply-templates/></I></html>
	</template>
	
	<!-- A displayed fraction. Is there a nicer way of doing this in HTML? -->
	<template name="math-fraction" match="math//fraction">
		<latex>\frac{<xsl:apply-templates select="numerator"/>}{<xsl:apply-templates select="denominator"/>}</latex>
		<html><xsl:apply-templates select="numerator"/><BIG> / </BIG><xsl:apply-templates select="denominator"/></html>
	</template>
	

</stylesheet>