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:text>{\PaperCode}</xsl:text></latex>
		<html>INFO&#160;321</html>
	</template>

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


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


</xsl:text><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="non-breaking-space" match="non-breaking-space"><latex>~</latex><html>&#160;</html></template>	
<alias source="nbsp" target="non-breaking-space"/>

	<template name="ellipsis-sign" match="ellipsis-sign">	<latex><xsl:text>{\ldots}</xsl:text></latex>	<html><xsl:text>&#8230;</xsl:text></html></template>
	
	<alias source="etc" target="ellipsis-sign"/>
	<alias source="ellipsis" target="ellipsis-sign"/>

	<!-- Misc typographic symbols -->
	<template name="endash" match="endash">				<latex><xsl:text>--</xsl:text></latex>			<html><xsl:text>&#8211;</xsl:text></html></template>
	<alias source="en-dash" target="endash"/>
	<template name="emdash" match="emdash">				<latex><xsl:text>---</xsl:text></latex>			<html><xsl:text>&#8212;</xsl:text></html></template>
	<alias source="em-dash" target="emdash"/>
	<template name="apostrophe" match="apostrophe">		<latex><xsl:text>'</xsl:text></latex>			<html><xsl:text>&#8217;</xsl:text></html></template>
	<template name="dollar" match="dollar">				<latex><xsl:text>\$</xsl:text></latex>			<html><xsl:text>$</xsl:text></html></template>
	<template name="percent-sign" match="percent-sign">	<latex><xsl:text>\%</xsl:text></latex>			<html><xsl:text>%</xsl:text></html></template>
	<template name="ampersand" match="ampersand">		<latex><xsl:text>\&amp;</xsl:text></latex>		<html><xsl:text>&amp;</xsl:text></html></template>
	<template name="trademark-sign" match="trademark-sign">		<latex><xsl:text>(TM)</xsl:text></latex>		<html><xsl:text>&#8482;</xsl:text></html></template>
	<alias source="trademark" target="trademark-sign"/>
	<template name="copyright-sign" match="copyright-sign">	<latex><xsl:text>\copyright</xsl:text></latex>		<html><xsl:text>&#169;</xsl:text></html></template>
	<!--	<alias source="copyright" target="copyright-sign"/> -->
	<template name="degree-sign" match="degree-sign">	<latex><xsl:text>deg</xsl:text></latex>			<html><xsl:text>&#176;</xsl:text></html></template>
	<template name="section-sign" match="section-sign">	<latex><xsl:text>{\S}</xsl:text></latex>		<html><xsl:text>&#167;</xsl:text></html></template>
	<template name="paragraph-sign" match="paragraph-sign">	<latex><xsl:text>{\P}</xsl:text></latex>	<html><xsl:text>&#182;</xsl:text></html></template>
	<template name="pi" match="pi">						<latex><xsl:text>{\pi}</xsl:text></latex>		<html><xsl:text>&#960;</xsl:text></html></template>
	<template name="LaTeX" match="LaTeX">				<latex><xsl:text>{\latex}</xsl:text></latex>	<html><xsl:text>LaTeX</xsl:text></html></template>
	<alias source="latex" target="LaTeX"/>
	<!-- Could use \triangleright in LaTeX, but is there an equivalent in HTML? -->
	<template name="menu-separator" match="menu-separator"><latex>$\rightarrow$</latex><html>&#8594;</html></template>

	<!-- Special styles -->
	<template name="emph" match="emph"><latex><xsl:text>\emph{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text></latex><html><EM><xsl:apply-templates/></EM></html></template>
	<template name="italic" match="italic"><latex><xsl:text>\textit{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text></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><xsl:text>\foreign{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text></latex><html><I CLASS="foreign"><xsl:apply-templates/></I></html></template>
	<template name="code" match="code">
		<latex><xsl:text>\code{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text></latex>
		<html><CODE><xsl:apply-templates/></CODE></html>
	</template>

	<!-- Quoted text. Handles nested quotes correctly. -->
	<template name="single-quote" match="quote[@single='yes']">
		<latex>`{<xsl:apply-templates/>}'</latex>
		<html>&#8216;<xsl:apply-templates/>&#8217;</html>
	</template>
	<template name="double-quote" match="quote">
		<latex>``{<xsl:apply-templates/>}''</latex>
		<html>&#8220;<xsl:apply-templates/>&#8221;</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"/>

	<template name="code-block" match="code-block">
		<latex>
			<xsl:text>\begin{verbatim}</xsl:text>
				<xsl:apply-templates/>
			<xsl:text>\end{verbatim}</xsl:text>
		</latex>
		<html>
			<PRE CLASS="code"><xsl:apply-templates/></PRE>
		</html>
	</template>
	
	<!-- 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><xsl:text>
			\begin{enumerate}
				<xsl:apply-templates/>
			\end{enumerate}
		</xsl:text></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><xsl:text>
			\begin{description}
				<xsl:apply-templates/>
			\end{description}
		</xsl:text></latex>
		<html>
			<DL>
				<xsl:apply-templates/>
			</DL>
		</html>
	</template>
	
	<alias source="description-list" target="definition-list"/>


	<!-- List elements -->
	<template name="item" match="item">
		<latex><xsl:text>\item </xsl:text><xsl:apply-templates/></latex>
		<html><LI><xsl:apply-templates/></LI></html>
	</template>

	<template name="definition-item" match="definition-list/item">
		<latex><xsl:text>\item[</xsl:text><xsl:apply-templates select="keyword"/><xsl:text>]</xsl:text> <xsl:apply-templates select="definition"/></latex>
		<html>
			<DT><xsl:apply-templates select="keyword"/></DT>
			<DD><xsl:apply-templates select="definition"/></DD>
		</html>
	</template>
	
	<template name="keyword" match="keyword">
		<latex><xsl:apply-templates/></latex>
		<html><STRONG><xsl:apply-templates/></STRONG></html>
	</template>
	
	<template name="definition" match="definition">
		<latex><xsl:apply-templates/></latex>
		<html><xsl:apply-templates/></html>
	</template>
	
	<alias source="topic" target="keyword"/>
	<alias source="description" target="definition"/>
	<alias source="discourse" target="definition"/>
	
	
	<!-- Sections, subsections, subsubsections, ... -->
	<template name="section" match="section">
		<latex>
			<xsl:apply-templates/>
		</latex>
		<html>
			<xsl:if test="@label"><A NAME="{@label}"></A></xsl:if>
			<xsl:apply-templates/>
		</html>
	</template>

	<!--
		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">0</xsl:param>
			<xsl:if test="$depth &gt; 0">
				<xsl:text>sub</xsl:text>
				<xsl:call-template select="generate-subs">
					<xsl:with-param><xsl:value-of select="$depth - 1"/></xsl:with-param>
				</xsl:call-template>
			</xsl:if>
		</latex>
	</template>
	
	<template name="window-title" match="document/title" mode="window-title">
		<html>
			<xsl:apply-templates/>
		</html>
	</template>

	<template name="document-title" match="document/title">
	<!-- Need to do something sensible for LaTeX here. -->
<!-- 		<latex><xsl:apply-templates/></latex> -->
		<html>
			<H1><xsl:apply-templates/></H1>
		</html>
	</template>

	<template name="section-title" match="section/title">
		<latex>
			<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; 2">2</xsl:if>
				</xsl:with-param>
			</xsl:call-template>
			<xsl:text>section</xsl:text>
			<xsl:apply-templates/><xsl:text>}

</xsl:text></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"/>
	
	
	<!-- Tables (equivalent to a tabular in LaTeX terms). -->
	<template name="table" match="table">
		<latex></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>
				<xsl:apply-templates/>
			</TABLE>
		</html>
	</template>
	
	<template name="table-body" match="table-body">
		<latex></latex>
		<html>
			<TBODY>
				<xsl:apply-templates/>
			</TBODY>
		</html>
	</template>

	<template name="row" match="row">
		<latex></latex>
		<html>
			<TR>
				<xsl:apply-templates/>
			</TR>
		</html>
	</template>

	<template name="cell" match="cell">
		<latex></latex>
		<html>
			<xsl:variable name="column-no">
				<xsl:number/>
			</xsl:variable>
			<TD>
				<xsl:attribute name="ALIGN">
					<xsl:value-of select="ancestor::table/columns/column[position()=$column-no]/@align"/>
					<xsl:if test="not(ancestor::table/columns/column[position()=$column-no]/@align)">left</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:value-of select="@url"/>}</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">
		<latex></latex>
		<html>
			<xsl:apply-templates select="//*[@label = current()/@label]" mode="reference"/>
		</html>
	</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>Section&#160;<xsl:number count="section" 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>Figure&#160;<xsl:number count="figure" level="multiple" format="1"/></html>
	</template>
	
	
	<!-- Figures. -->
	<template name="figure" match="figure">
		<latex>
			<!-- This should really be more generic, so it can handle figures without graphics.  That'll mean more templates...  Or perhaps do separate XML tags for different kinds of figure? -->
			<!-- select="id" and select = "label" so it'll work with either -->
			\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="caption" match="caption">
		<latex><xsl:apply-templates/></latex>
		<html>
			<STRONG>Figure <xsl:number count="figure" level="multiple" format="1"/>. </STRONG><xsl:apply-templates/>
		</html>
	</template>
	
	
	<!-- Images. -->
	<template match="image[not(@print-version)]">
		<latex>
			\includegraphics[width=<xsl:value-of select="width"/>\textwidth]{images/<xsl:value-of select="basename"/>.<xsl:value-of select="$latex-image-format"/>}
		</latex>
		<html>
			<IMG BORDER="1">
				<xsl:attribute name="SRC">images/<xsl:value-of select="basename"/>.png</xsl:attribute>
				<xsl:attribute name="ALT"><xsl:apply-templates select="description"/></xsl:attribute>
			</IMG>
		</html>
	</template>

	<template match="image[@print-version = 'yes']">
		<latex>
			\includegraphics[width=<xsl:value-of select="width"/>\textwidth]{images/<xsl:value-of select="basename"/>.<xsl:value-of select="$latex-image-format"/>}
		</latex>
		<html>
			<A>
				<xsl:attribute name="HREF">images/<xsl:value-of select="basename"/>-print.png</xsl:attribute>
				<IMG BORDER="1">
					<xsl:attribute name="SRC">images/<xsl:value-of select="basename"/>.png</xsl:attribute>
					<xsl:attribute name="ALT"><xsl:apply-templates select="description"/></xsl:attribute>
				</IMG>
			</A>
		</html>
	</template>


</stylesheet>