Newer
Older
XML / xml2latex.xsl
nstanger on 10 Dec 2003 15 KB - Added XML authoring stuff.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<!-- Whether sample answers should be included in output.  This could be an xsl:param rather than an xsl:variable, maybe. -->
	<!--
	<xsl:variable name="showanswers" select="'yes'"/>
	-->	<xsl:param name="showanswers" select="'no'"/>
	<xsl:output method="text" encoding="UTF-8" media-type="text/plain"/>
	<!--
	<xsl:strip-space elements=""/>
	<xsl:preserve-space elements="paragraph"/>
	-->
	<!-- <xsl:output method="text" encoding="UTF-8" media-type="text/plain"/> -->

	<!-- Make <omit>s (and aliases) disappear. -->
	<xsl:template match="omit"></xsl:template>
	<xsl:template match="omission"/>
	<xsl:template match="excluded"/>
	<xsl:template match="exclusion"/>


	<!-- Carry comments through to the output document.  (Kind of: "It is an error if instantiating the content of xsl:comment creates nodes other than text nodes.") -->
	<xsl:template match="comment">
		\begin{comment}
			<xsl:apply-templates/>
		\end{comment}
	</xsl:template>
	
	
	<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
	<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>

	
	<xsl:template name="paper" match="paper"><xsl:apply-templates select="subject-code"/>~<xsl:apply-templates select="paper-number"/></xsl:template>
<!-- <xsl:template name="INFO212" match="INFO212"><xsl:call-template name="paper"/></xsl:template> -->
	
	<xsl:template name="PaperCode" match="PaperCode">{\PaperCode}</xsl:template>
	<xsl:template name="OracleServer" match="OracleServer">{\OracleServer}</xsl:template>
	
	
	<!-- Oracle Documentation Links: DO NOT EDIT: generated automatically from Excel -->
	
<xsl:template match="OraAdmin">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96521/toc.htm}{<xsl:call-template name="OracleServer"/> Database Administrator<xsl:call-template name="apostrophe"/>s Guide}</xsl:template>
<xsl:template match="OraAppDevOR">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/appdev.920/a96594/toc.htm}{<xsl:call-template name="OracleServer"/> Application Developer<xsl:call-template name="apostrophe"/>s Guide<xsl:call-template name="emdash"/>Object-Relational Features}</xsl:template>
<xsl:template match="OraConcepts">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96524/toc.htm}{<xsl:call-template name="OracleServer"/> Database Concepts}</xsl:template>
<xsl:template match="OraDataWarehousing">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96520/toc.htm}{<xsl:call-template name="OracleServer"/> }</xsl:template>
<xsl:template match="OraErrors">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96525/toc.htm}{<xsl:call-template name="OracleServer"/> Database Error Messages}</xsl:template>
<xsl:template match="OraSQL">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96540/toc.htm}{<xsl:call-template name="OracleServer"/> SQL Reference}</xsl:template>
<xsl:template match="OraJava">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/java.920/a96656/toc.htm}{<xsl:call-template name="OracleServer"/> Java Developer<xsl:call-template name="apostrophe"/>s Guide}</xsl:template>
<xsl:template match="OraJDBC">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/java.920/a96654/toc.htm}{<xsl:call-template name="OracleServer"/> JDBC Developer<xsl:call-template name="apostrophe"/>s Guide and Reference}</xsl:template>
<xsl:template match="OraGlossary">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/mix.920/a97290/toc.htm}{<xsl:call-template name="OracleServer"/> Database Master Glossary}</xsl:template>
<xsl:template match="OraTuning">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96533/toc.htm}{<xsl:call-template name="OracleServer"/> Database Performance Tuning Guide and Reference}</xsl:template>
<xsl:template match="OraPLSQL">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/appdev.920/a96624/toc.htm}{<xsl:call-template name="OracleServer"/> PL/SQL User<xsl:call-template name="apostrophe"/>s Guide and Reference}</xsl:template>
<xsl:template match="OraIndex">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/mix.920/a96625/toc.htm}{<xsl:call-template name="OracleServer"/> Database Master Index}</xsl:template>
<xsl:template match="OraReference">\href{http://info-nts-05.otago.ac.nz/docs/oracle9i/server.920/a96536/toc.htm}{<xsl:call-template name="OracleServer"/> Database Reference}</xsl:template>

	<!-- End of Oracle Documentation Links.  That'll save a bit o' typing. -->


	<!-- 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! -->
	<xsl:template name="newline" match="newline">\\
</xsl:template>
	<xsl:template name="non-breaking-space" match="non-breaking-space">~</xsl:template>
	<xsl:template match="nbsp"><xsl:call-template name="non-breaking-space"/></xsl:template>
	<xsl:template name="ellipsis-sign" match="ellipsis-sign">{\ldots}</xsl:template>
	<xsl:template match="etc"><xsl:call-template name="ellipsis-sign"/></xsl:template>
	<xsl:template match="ellipsis"><xsl:call-template name="ellipsis-sign"/></xsl:template>

	<!-- Mathematical symbols -->
	<xsl:template name="digit-group-separator" match="digit-group-separator"><xsl:call-template name="non-breaking-space"/></xsl:template>
	<xsl:template match="digitsep"><xsl:call-template name="digit-group-separator"/></xsl:template>
	<xsl:template name="multiplication-sign" match="multiplication-sign">\times</xsl:template>
	<xsl:template match="times"><xsl:call-template name="multiplication-sign"/></xsl:template>
	<xsl:template match="minus">-</xsl:template>
	<xsl:template match="approximately-equal-to">{\approx}</xsl:template>
	<xsl:template match="less-than-or-equal-to">{\leq}</xsl:template>
	<xsl:template match="less-than">&lt;</xsl:template>
	<xsl:template match="greater-than">&gt;</xsl:template>

	<xsl:template name="endash" match="endash">--</xsl:template>
	<xsl:template name="emdash" match="emdash">---</xsl:template>
	<xsl:template name="apostrophe" match="apostrophe">'</xsl:template>
	<xsl:template match="dollar">\$</xsl:template>
	<xsl:template name="percent-sign" match="percent-sign">\%</xsl:template>
	<xsl:template match="percent"><xsl:call-template name="percent-sign"/></xsl:template>
	<xsl:template match="ampersand">\&amp;</xsl:template>
	<xsl:template match="trademark">(TM)</xsl:template>
	<xsl:template name="degree-sign" match="degree-sign">deg</xsl:template>
	<xsl:template match="degree"><xsl:call-template name="degree-sign"/></xsl:template>
	<xsl:template match="section-sign">??</xsl:template>
	<xsl:template match="pi">{\pi}</xsl:template>

	<!-- Special styles -->
	<xsl:template match="emph">\emph{<xsl:apply-templates/>}</xsl:template>
	<xsl:template match="italic">\textit{<xsl:apply-templates/>}</xsl:template>
	<xsl:template match="quote">``<xsl:apply-templates/>''</xsl:template>
	<xsl:template match="term">\term{<xsl:apply-templates/>}</xsl:template>
	<xsl:template match="foreign">\foreign{<xsl:apply-templates/>}</xsl:template>

	<!-- More math stuff -->
	<xsl:template match="math">$<xsl:apply-templates/>$</xsl:template>
	<xsl:template match="textup">\textup{<xsl:apply-templates/>}</xsl:template>
	<xsl:template match="fraction">\frac{<xsl:apply-templates select="numerator"/>}{<xsl:apply-templates select="denominator"/>}</xsl:template>
	
	<xsl:template name="equation-array" match="equation-array">\begin{eqnarray*}<xsl:apply-templates/>\end{eqnarray*}</xsl:template>
	<!-- No, don't put in an alias, because you'd also need one for eqnarray//row.
	<xsl:template match="eqnarray"><xsl:call-template name="equation-array"/></xsl:template> -->
	<!-- Hmm, LaTeX uses & as separators: how do we know if there's another cell coming?  Ah, but eqnarray requires no more than (exactly?) three columns. -->
	<xsl:template match="equation-array/row"><xsl:apply-templates select="left"/>	&amp;	<xsl:apply-templates select="middle"/>	&amp;	<xsl:apply-templates select="right"/>	\\
		\\ </xsl:template>
	
	<xsl:template match="code">\verb#<xsl:apply-templates/>#</xsl:template>



	<!-- Basic block structures -->
	
	<xsl:template match="code-block"><!-- mathescape? -->
		\begin{lstlisting}%[language=[<xsl:value-of select="@dialect"/>]<xsl:value-of select="@language"/>]
			<xsl:apply-templates/>
		\end{lstlisting}
	</xsl:template>
	
	<!--<xsl:template match="code-block/parameter">${\langle}<xsl:apply-templates/>{\rangle}$</xsl:template>-->
	<xsl:template match="code-block//parameter">&lt;<xsl:apply-templates/>&gt;</xsl:template>
	<xsl:template match="code-block//server-output">/* SERVER OUTPUT:<xsl:apply-templates/>*/</xsl:template>
	<xsl:template match="code-block//query-plan">/* QUERY PLAN:<xsl:apply-templates/>*/</xsl:template>

	<xsl:template match="verbatim">\begin{verbatim}<xsl:apply-templates/>\end{verbatim}</xsl:template>
	
	<!-- pass-through code -->
	<xsl:template match="latex"><xsl:value-of select="."/></xsl:template>
	
	<xsl:template match="paragraph">


<xsl:apply-templates/>


</xsl:template>
	

	<xsl:template match="para">

<xsl:apply-templates/>

</xsl:template>
	
	
	<xsl:template match="p">

<xsl:apply-templates/>

</xsl:template>
	

	<xsl:template match="page-break">\clearpage</xsl:template>
	
	<!-- Figures and included graphics -->

	<xsl:template match="figure">
	
	<!-- 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="image"/>
		\caption{<xsl:apply-templates select="caption"/>}
		\label{fig:<xsl:apply-templates select="id"/><xsl:apply-templates select="label"/>}
	\end{figure}
	
	</xsl:template>

	<!-- Now assuming that all graphics will be placed in info321/Handbook/Graphics/ -->
	<xsl:template match="image">
		<xsl:if test="name(/child::node()[1])='tutorial'">\includegraphics[width=<xsl:value-of select="width"/>\textwidth]{\tutroot/<xsl:value-of select="/tutorial/id/number-code"/>-<xsl:value-of select="/tutorial/id/title-code"/>/Graphics/<xsl:value-of select="basename"/>.eps}</xsl:if>
		<xsl:if test="name(/child::node()[1])='laboratory'">\includegraphics[width=<xsl:value-of select="width"/>\textwidth]{\labroot/<xsl:value-of select="/laboratory/id/number-code"/>-<xsl:value-of select="/laboratory/id/title-code"/>/Graphics/<xsl:value-of select="basename"/>.eps}</xsl:if>
	</xsl:template>



	<xsl:template match="eqnarray">
		\begin{eqnarray}
			<xsl:apply-templates/>
		\end{eqnarray}
	</xsl:template>


	<!-- Table element handling -->

	<xsl:template match="table">
	</xsl:template>



	<!-- Top-level match: <tutorial><title>... -->
	<!-- <xsl:template match="tutorial/title"><xsl:message>showanswers=<xsl:value-of select="$showanswers"/>: Sample answers will be displayed.</xsl:message> -->
	
	<xsl:template match="/tutorial">\tutorial{<xsl:apply-templates select="title"/>}
		<xsl:apply-templates select="/tutorial/introduction"/>
		<xsl:for-each select="/tutorial/section">
			<xsl:apply-templates/>
		</xsl:for-each>

\vfill {\scriptsize \hfill \verb+<xsl:apply-templates select="//id/cvs-id"/>+}
	</xsl:template>


	<xsl:template match="/laboratory">\lab{<xsl:apply-templates select="title"/>}
		<xsl:apply-templates select="/laboratory/introduction"/>
		<xsl:for-each select="/laboratory/section">
			<xsl:apply-templates/>
		</xsl:for-each>

\vfill {\scriptsize \hfill \verb+<xsl:apply-templates select="//id/cvs-id"/>+}
	</xsl:template>


	<!-- Top-level sections -->

	<xsl:template match="/tutorial/section/title">
\section{<xsl:apply-templates/>}

</xsl:template>

	<xsl:template match="/laboratory/section/title">
\section{<xsl:apply-templates/>}

</xsl:template>

	<!-- Subsections -->
	
	<xsl:template match="/tutorial/section/section/title">
\subsection{<xsl:apply-templates/>}

</xsl:template>

	<xsl:template match="/laboratory/section/section/title">
\subsection{<xsl:apply-templates/>}

</xsl:template>


	<!-- Itemised, unordered, bullet points -->
	<xsl:template name="itemised-list" match="itemised-list">

\begin{itemize}
<xsl:apply-templates/>
\end{itemize}

</xsl:template>

	<!-- aliases -->
	<xsl:template match="itemize"><xsl:call-template name="itemised-list"/></xsl:template>
	<xsl:template match="unordered-list"><xsl:call-template name="itemised-list"/></xsl:template>
	<xsl:template match="bulleted-list"><xsl:call-template name="itemised-list"/></xsl:template>
	<xsl:template match="bullet-list"><xsl:call-template name="itemised-list"/></xsl:template>
	<xsl:template match="bullet-points"><xsl:call-template name="itemised-list"/></xsl:template>
	


	<!-- Enumerated, ordered, numbered points -->
	<xsl:template name="enumerated-list" match="enumerated-list">

\begin{enumerate}
<xsl:apply-templates/>
\end{enumerate}

</xsl:template>

	<!-- aliases -->
	<xsl:template match="enumerate"><xsl:call-template name="enumerated-list"/></xsl:template>
	<xsl:template match="ordered-list"><xsl:call-template name="enumerated-list"/></xsl:template>
	<xsl:template match="numbered-list"><xsl:call-template name="enumerated-list"/></xsl:template>



	<!--	<xsl:template match="itemised-list/item">\item <xsl:apply-templates/></xsl:template> -->
	
	<xsl:template match="questions"><question-list><xsl:apply-templates/></question-list></xsl:template>
	
	<xsl:template match="question-list">\begin{enumerate}
<xsl:apply-templates/>
\end{enumerate}</xsl:template>
<!--	<xsl:template match="question-list/item">\item <xsl:apply-templates/></xsl:template> -->

	<xsl:template match="item">\item <xsl:apply-templates/></xsl:template>

	<xsl:template match="definition-list">\begin{description}<xsl:apply-templates/>\end{description}</xsl:template>
	<xsl:template match="definition-list/item">\item <xsl:apply-templates/></xsl:template>

	<!-- <keyword> and <term> are synonymous in the context definition-list/item -->

	<xsl:template match="definition-list/item/keyword">[<xsl:apply-templates/>]</xsl:template>
	<xsl:template match="definition-list/item/term">[<xsl:apply-templates/>]</xsl:template>

	
	<!--
	Processing answer sections for tutorials and labs.  If the document says that answers should be displayed, then do, otherwise do not.
	We could (should?) get gnarly and make it so you can turn answers on/off for each tutorial and lab.
	It's nice to weed out the sample answer markup at the XSLT processing stage to make LaTeX run faster.
	\showanswers still needs to be called so that LaTeX can format chapter headings like "Answers for ..."
	-->
	<xsl:template match="answer">
		<xsl:if test="$showanswers='yes'">
			\begin{answer}
				<xsl:apply-templates/>
			\end{answer}
		</xsl:if>
	</xsl:template>


	<!-- Cross-references.  Might need to kludgify these to handle references to different kinds of label, e.g. fig:*, ... -->
	<xsl:template name="reference" match="reference">
		<xsl:if test="type='figure'">Figure~\ref{fig:<xsl:value-of select="target"/>}</xsl:if>
	</xsl:template>

	<!-- alias -->
	<xsl:template match="ref"><xsl:call-template name="reference"/></xsl:template>


	<!-- Links to URLs. -->
	<xsl:template match="netlink">\href{<xsl:value-of select="target"/>}{<xsl:apply-templates select="label"/>}</xsl:template>

	<!-- URLs that should appear verbatim in the output. -->
	<xsl:template match="url">
		\hrefa{<xsl:value-of select="."/>}
	</xsl:template>

	



	<!-- Menu items and paths -->
	<xsl:template match="menu/item">\menuitem{<xsl:apply-templates/>}</xsl:template>
	
	<xsl:template name="menu-separator" match="menu/separator">{\menusep}</xsl:template>
	<xsl:template match="menusep"><xsl:call-template name="menu-separator"/></xsl:template>



	<!-- suppress ID codes -->
	<xsl:template match="tutorial/id"/>
	<xsl:template match="laboratory/id"/>

</xsl:stylesheet>