<?xml version="1.0" encoding="Shift_JIS"?>
<!--
	Ver.0.4.4
		各章毎に脚注を付ける機能を追加。
	Ver.0.4.3
		要素 span をやむなく追加。なるべく使わないように…。
	Ver.0.4.2
		要素 flex を追加。よく使う flex.gif のこと。
	Ver.0.4.1
		table のオプションをサポート。
	Ver.0.4.0
		import 要素を追加。xt を独自拡張する。
	Ver.0.3.2
		参考文献の埋め込み形式の改良。
	Ver.0.3.1
		普通の IMG が line-height により、
		ネスケでレイアウトが乱れるバグへの対処。
	Ver.0.3.0
		参考文献の埋め込みに対応。
	Ver.0.2.1
		連番自動生成に対応。
	Ver.0.2.0
		文頭への目次自動付加機能を追加。
	Ver.0.1.0
		ドラフト初版。
-->
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0"
	xmlns:xslext="http://www.jclark.com/xt/java/jp.ac.hokudai.sci.ep.gonta.XSLTExtension.ImportDocument"
	exclude-result-prefixes="xslext">

<xsl:output method="html" encoding="Shift_JIS"
	doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>

<xsl:template match="/">
<HTML>
<xsl:comment>
この HTML は XML を XSLT で変換したものです。
ソースが汚く見えるのはそのためです。
</xsl:comment>
<HEAD>
<TITLE>
	<xsl:value-of select="document/info/title" />
</TITLE>
<xsl:element name="LINK">
	<xsl:attribute name="REL">stylesheet</xsl:attribute>
	<xsl:attribute name="TYPE">text/css</xsl:attribute>
	<xsl:attribute name="HREF">
		<xsl:value-of select="document/info/css" />
	</xsl:attribute>
</xsl:element>
</HEAD>
<BODY>
	<H1>
		<xsl:element name="A">
			<xsl:attribute name="HREF">
				<xsl:value-of select="document/info/backto" />
			</xsl:attribute>
			<xsl:attribute name="CLASS">H1</xsl:attribute>
			<xsl:value-of select="document/info/title" />
		</xsl:element>
	</H1>
	<xsl:call-template name="contents"/>
	<DIV CLASS="MAIN">
		<xsl:apply-templates select="document/body" />
	</DIV>
	<BR/><BR/>
	<TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR>
	<TD CLASS="DATE">最終更新日：
		<xsl:value-of select="document/info/lastmodify/year" />/<xsl:value-of
		select="document/info/lastmodify/month" />/<xsl:value-of
		select="document/info/lastmodify/day" />
	</TD>
	<TD CLASS="COPYRIGHT">
		Copyright
		<xsl:text disable-output-escaping="yes">&amp;copy;&amp;nbsp;</xsl:text>
		<xsl:value-of select="document/info/lastmodify/year" />
		<xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text>
		<xsl:element name="A">
			<xsl:attribute name="HREF">
				<xsl:text>mailto:</xsl:text>
				<xsl:value-of select="document/info/author/@mail" />
			</xsl:attribute>
			<xsl:value-of select="document/info/author" />
		</xsl:element>
	</TD>
	</TR></TABLE>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="chapter">
	<H2>
		<xsl:element name="A">
			<xsl:attribute name="NAME">
				<xsl:text>chapter_</xsl:text>
				<xsl:number level="any" count="chapter" from="body" />
			</xsl:attribute>
			<xsl:number level="any" count="chapter" from="body" />
			<xsl:text>. </xsl:text>
			<xsl:value-of select="@title" />
		</xsl:element>
  </H2>
  <xsl:apply-templates/>
	<xsl:if test="current()//fn">
		<BR/>
		<HR NOSHADE="" SIZE="1" WIDTH="40%" ALIGN="left"/>
		<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" STYLE="margin:0px">
			<xsl:for-each select="current()//fn">
				<TR VALIGN="top">
					<TD>
						<xsl:element name="A">
							<xsl:attribute name="CLASS">
								<xsl:text>CFLIST</xsl:text>
							</xsl:attribute>
							<xsl:attribute name="NAME">
								<xsl:text>fn_</xsl:text>
								<xsl:number level="any" count="fn" from="body" />
							</xsl:attribute>
							<xsl:variable name="fnnum">
								<xsl:number level="any" count="fn" from="chapter" />
							</xsl:variable>
							<xsl:variable name="fnchar">
								<xsl:choose>
									<xsl:when test="$fnnum = 1">
										<xsl:text>&#x2020;</xsl:text>
									</xsl:when>
									<xsl:when test="$fnnum = 2">
										<xsl:text>&#x2021;</xsl:text>
									</xsl:when>
									<xsl:otherwise>
										<xsl:text>#</xsl:text>
										<xsl:value-of select="$fnnum" />
									</xsl:otherwise>
								</xsl:choose>
							</xsl:variable>
							<xsl:value-of select="$fnchar" />
						</xsl:element>
					</TD>
					<TD><P><SMALL>
						<xsl:apply-templates/>
					</SMALL></P></TD>
				</TR>
			</xsl:for-each>
		</TABLE>
	</xsl:if>
</xsl:template>

<xsl:template match="fn">
	<SUP>
		<A CLASS="CF">
			<xsl:attribute name="HREF">
				<xsl:text>#fn_</xsl:text>
				<xsl:number level="any" count="fn" from="body" />
			</xsl:attribute>
			<xsl:variable name="fnnum">
				<xsl:number level="any" count="fn" from="chapter" />
			</xsl:variable>
			<xsl:if test="$fnnum &gt; 2">
				<xsl:message>warn: too many footnotes.</xsl:message>
			</xsl:if>
			<xsl:variable name="fnchar">
				<xsl:choose>
					<xsl:when test="$fnnum = 1">
						<xsl:text>&#x2020;</xsl:text>
					</xsl:when>
					<xsl:when test="$fnnum = 2">
						<xsl:text>&#x2021;</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>#</xsl:text>
						<xsl:value-of select="$fnnum" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:value-of select="$fnchar" />
		</A>
	</SUP>
</xsl:template>

<xsl:template match="section">
	<H3>
		<xsl:element name="A">
			<xsl:attribute name="NAME">
				<xsl:text>section_</xsl:text>
				<xsl:number level="any" count="section" from="body" />
			</xsl:attribute>
			<xsl:text>(</xsl:text>
			<xsl:number level="multiple" count="chapter|section" />
			<xsl:text>) </xsl:text>
			<xsl:value-of select="@title" />
		</xsl:element>
	</H3>
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="subsection">
	<H4>
		<xsl:text>■ </xsl:text>
		<xsl:value-of select="@title" />
	</H4>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="p|P">
	<P>
		<xsl:apply-templates/>
	</P>
</xsl:template>

<xsl:template match="s|STRONG">
	<STRONG><xsl:apply-templates/></STRONG>
</xsl:template>

<xsl:template match="ul|UL">
	<UL>
		<xsl:apply-templates/>
	</UL>
</xsl:template>

<xsl:template match="ol|OL">
	<OL>
		<xsl:apply-templates/>
	</OL>
</xsl:template>

<xsl:template match="li|LI">
	<LI><xsl:apply-templates/></LI>
</xsl:template>

<xsl:template match="sample|BLOCKQUOTE[@CLASS='SAMPLE']">
	<BLOCKQUOTE CLASS="SAMPLE"><TT>
		<xsl:apply-templates/>
	</TT></BLOCKQUOTE>
</xsl:template>

<xsl:template match="bq|BLOCKQUOTE">
	<BLOCKQUOTE>
		<xsl:apply-templates/>
	</BLOCKQUOTE>
</xsl:template>

<xsl:template match="br|BR">
	<xsl:copy/>
</xsl:template>

<xsl:template match="u|U">
	<U><xsl:apply-templates/></U>
</xsl:template>

<xsl:template match="i|I">
	<I><xsl:apply-templates/></I>
</xsl:template>

<xsl:template match="sup">
	<SUP><xsl:apply-templates/></SUP>
</xsl:template>

<xsl:template match="command">
	<TABLE CLASS="TT" WIDTH="80%"><TR><TD NOWRAP=""><TT>
		<xsl:apply-templates/>
	</TT></TD></TR></TABLE>
</xsl:template>

<xsl:template match="k|KBD">
	<KBD><xsl:apply-templates/></KBD>
</xsl:template>

<xsl:template match="v|VAR">
	<VAR><xsl:apply-templates/></VAR>
</xsl:template>

<xsl:template match="ret">
	<xsl:element name="IMG">
		<xsl:attribute name="SRC">return.gif</xsl:attribute>
		<xsl:attribute name="WIDTH">13</xsl:attribute>
		<xsl:attribute name="HEIGHT">11</xsl:attribute>
		<xsl:attribute name="ALT">[ENTER]</xsl:attribute>
	</xsl:element>
</xsl:template>

<xsl:template match="flex">
	<xsl:element name="IMG">
		<xsl:attribute name="SRC">flex.gif</xsl:attribute>
		<xsl:if test="@w">
			<xsl:attribute name="WIDTH">
				<xsl:value-of select="@w" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@w)">
			<xsl:attribute name="WIDTH">1</xsl:attribute>
		</xsl:if>
		<xsl:if test="@h">
			<xsl:attribute name="HEIGHT">
				<xsl:value-of select="@h" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@h)">
			<xsl:attribute name="HEIGHT">1</xsl:attribute>
		</xsl:if>
	</xsl:element>
</xsl:template>

<xsl:template match="table">
	<xsl:element name="TABLE">
		<xsl:if test="@border">
			<xsl:attribute name="BORDER">
				<xsl:value-of select="@border" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@border)">
			<xsl:attribute name="BORDER">
				<xsl:text>1</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@cellspacing">
			<xsl:attribute name="CELLSPACING">
				<xsl:value-of select="@cellspacing" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@cellspacing)">
			<xsl:attribute name="CELLSPACING">
				<xsl:text>0</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@cellpadding">
			<xsl:attribute name="CELLPADDING">
				<xsl:value-of select="@cellpadding" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@cellpadding)">
			<xsl:attribute name="CELLPADDING">
				<xsl:text>3</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@width != 'none'">
			<xsl:attribute name="WIDTH">
				<xsl:value-of select="@width" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="not(@width)">
			<xsl:attribute name="WIDTH">
				<xsl:text>80%</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@style">
			<xsl:attribute name="STYLE">
				<xsl:value-of select="@style" />
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<xsl:template match="caption">
	<CAPTION ALIGN="top">
		<FONT SIZE="-1">
			<xsl:text>&lt;</xsl:text>
			<xsl:apply-templates/>
			<xsl:text>&gt;</xsl:text>
		</FONT>
	</CAPTION>
</xsl:template>

<xsl:template match="table/tr">
	<TR>
		<xsl:apply-templates/>
	</TR>
</xsl:template>

<xsl:template match="table/tr/th">
	<TH NOWRAP="">
		<xsl:apply-templates/>
	</TH>
</xsl:template>

<xsl:template match="table/tr/td|table/tr/tdn">
	<xsl:element name="TD">
		<xsl:if test="local-name() = 'tdn'">
			<xsl:attribute name="NOWRAP" />
		</xsl:if>
		<xsl:if test="@bgc">
			<xsl:attribute name="BGCOLOR">
				<xsl:value-of select="@bgc" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@align">
			<xsl:attribute name="ALIGN">
				<xsl:value-of select="@align" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@valign">
			<xsl:attribute name="VALIGN">
				<xsl:value-of select="@valign" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@col">
			<xsl:attribute name="COLSPAN">
				<xsl:value-of select="@col" />
			</xsl:attribute>
		</xsl:if>
		<xsl:if test="@row">
			<xsl:attribute name="ROWSPAN">
				<xsl:value-of select="@row" />
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<xsl:template match="a|A">
	<xsl:element name="A">
		<xsl:attribute name="HREF">
			<xsl:value-of select="@href|@HREF" />
		</xsl:attribute>
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<xsl:template match="img|IMG">
	<CENTER><TABLE>
		<TR><TD ALIGN="center">
			<xsl:element name="IMG">
				<xsl:attribute name="SRC">
					<xsl:value-of select="@src|@SRC" />
				</xsl:attribute>
				<xsl:attribute name="WIDTH">
					<xsl:value-of select="@w|@WIDTH" />
				</xsl:attribute>
				<xsl:attribute name="HEIGHT">
					<xsl:value-of select="@h|@HEIGHT" />
				</xsl:attribute>
				<xsl:attribute name="ALT">
					<xsl:value-of select="@src|@SRC" />
				</xsl:attribute>
			</xsl:element>
		</TD></TR>
		<TR><TD ALIGN="left" VALIGN="top">
			<HR NOSHADE="" SIZE="1" WIDTH="100%" ALIGN="left"/>
			<FONT SIZE="-1"><xsl:value-of select="@a|@ALT" /></FONT>
		</TD></TR>
	</TABLE></CENTER>
</xsl:template>

<xsl:template name="contents">
	<xsl:if test="//chapter">
		<DIV CLASS="CONTENTS"><OL>
			<xsl:for-each select="//chapter">
				<LI>
					<xsl:element name="A">
						<xsl:attribute name="HREF">
							<xsl:text>#chapter_</xsl:text>
							<xsl:number level="any" count="chapter" from="body" />
						</xsl:attribute>
						<xsl:value-of select="@title" />
					</xsl:element>
					<xsl:if test="./section">
						<xsl:call-template name="listsection" />
					</xsl:if>
				</LI>
			</xsl:for-each>
		</OL></DIV>
	</xsl:if>
</xsl:template>

<xsl:template name="listsection">
	<UL>
		<xsl:for-each select="section">
			<LI>
				<xsl:element name="A">
					<xsl:attribute name="HREF">
						<xsl:text>#section_</xsl:text>
						<xsl:number level="any" count="section" from="body" />
					</xsl:attribute>
					<xsl:value-of select="@title" />
				</xsl:element>
			</LI>
		</xsl:for-each>
	</UL>
</xsl:template>

<xsl:template match="cf">
	<SUP>
		<A CLASS="CF">
			<xsl:attribute name="HREF">
				<xsl:text>#cf_</xsl:text>
				<xsl:value-of select="@id" />
			</xsl:attribute>
			<xsl:text>*</xsl:text>
			<xsl:value-of select="@id" />
		</A>
	</SUP>
</xsl:template>

<xsl:template match="cfol">
	<OL>
		<xsl:for-each select="cfli">
			<xsl:sort select="@id" data-type="number" />
			<LI>
				<xsl:element name="A">
					<xsl:attribute name="CLASS">
						<xsl:text>CFLIST</xsl:text>
					</xsl:attribute>
					<xsl:attribute name="NAME">
						<xsl:text>cf_</xsl:text>
						<xsl:value-of select="@id" />
					</xsl:attribute>
					<xsl:apply-templates/>
				</xsl:element>
			</LI>
		</xsl:for-each>
	</OL>
</xsl:template>

<xsl:template match="import">
	<xsl:variable name="path" select="@href" />
	<xsl:if test="@lines">
		<xsl:variable name="lines" select="@lines" />
		<xsl:if test="@tabwidth">
			<xsl:variable name="tabwidth" select="@tabwidth" />
			<xsl:value-of select="xslext:importDoc($path, $lines, $tabwidth)"
										disable-output-escaping="yes" />
		</xsl:if>
		<xsl:if test="not(@tabwidth)">
			<xsl:value-of select="xslext:importDoc($path, $lines)"
										disable-output-escaping="yes" />
		</xsl:if>
	</xsl:if>
	<xsl:if test="not(@lines)">
		<xsl:value-of select="xslext:importDoc($path)"
									disable-output-escaping="yes" />
	</xsl:if>
</xsl:template>

<xsl:template match="span">
	<xsl:element name="SPAN">
		<xsl:attribute name="STYLE">
			<xsl:value-of select="@style" />
		</xsl:attribute>
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

</xsl:stylesheet>