<?xml version="1.0" encoding="UTF-8"?>

<!-- 各ページ向け専用 XSL が共用する、基本的なテンプレや変数値を集めた XSL -->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:s="http://www.remus.dti.ne.jp/~a-satomi/"
	xmlns:js="http://www.remus.dti.ne.jp/~a-satomi/jsfunc"
	xmlns:fl="http://www.remus.dti.ne.jp/~a-satomi/filelist"
	xmlns:ap="http://www.remus.dti.ne.jp/~a-satomi/ap"
	exclude-result-prefixes="h s js fl ap">

<xsl:import href="jsfunc.xsl"/>
<xsl:import href="navi.xsl"/>
<xsl:import href="section.xsl"/>

<!-- ================ 既定値・初期値いろいろ ================  -->

<!-- ルートディレクトリのフルパス -->
<xsl:param name="baseDir">/Users/satomi/Sites/__DTI</xsl:param>
<xsl:param name="baseURI">http://www.remus.dti.ne.jp/~a-satomi</xsl:param>

<!-- ファイルリスト XML ファイルパス-->
<xsl:param name="fileList" select="concat($baseDir, '/common/xml/filelist.xml')"/>

<!-- 日記ジャンルリスト XML ファイルパス -->
<xsl:param name="genreList" select="concat($baseDir, '/common/xml/nikki_genres.xml')"/>

<!-- 日記のジャンル別一覧の入るディレクトリの名前 -->
<xsl:param name="genreDirName">genres</xsl:param>

<!-- 現在のファイルの仮想フルパスとファイル名とページタイトル -->
<xsl:param name="curFilePath" select="string(/h:html/h:head/s:file/@path)"/>
<xsl:param name="curFileTitle">
	<xsl:choose>
		<xsl:when test="string(/descendant::h:h1)"><xsl:value-of select="/descendant::h:h1"/></xsl:when>
		<xsl:when test="string(/descendant::h:h1/h:img/@alt)"><xsl:value-of select="/descendant::h:h1/h:img/@alt"/></xsl:when>
		<xsl:otherwise>TITLE NOT FOUND</xsl:otherwise>
	</xsl:choose>
</xsl:param>
<xsl:param name="curFileName" select="js:replace(string($curFilePath), '/.+\/(.+)$/', '$1')"/>

<!-- 現在処理中ファイルの仮想的フルパスにおける、第一レベル階層のディレクトリ名 -->
<xsl:param name="curDirName" select="js:replace(string($curFilePath), '/^\/(.+?)\/.*$/', '$1')"/>

<!-- サイトルート -->
<xsl:param name="rootPath" select="string('/index.xml')"/>
<xsl:param name="rootURI" select="js:omitDirIndex(js:chsuffix(js:a2r($curFilePath, $rootPath), 'html'))"/>
<xsl:param name="rootLabel" select="document($fileList)/fl:dir/fl:file[@name = substring-after($rootPath, '/')]"/>
<xsl:param name="rootTitle" select="string('娘娘飯店しるきぃうぇぶ')"/>

<!-- 各種ファイル/ディレクトリURI -->
<xsl:param name="profilesDir" select="concat($rootURI, 'profiles')"></xsl:param>
<xsl:param name="aboutFile" select="concat($profilesDir, '/about_ThisSite.html')"></xsl:param>
<xsl:param name="siteSearch" select="concat($rootURI, 'siteSearch.html')"></xsl:param>
<xsl:param name="commonDir" select="concat($rootURI, 'common')"></xsl:param>
<xsl:param name="cssDir" select="concat($commonDir, '/css')"></xsl:param>
<xsl:param name="scriptDir" select="concat($commonDir, '/js')"></xsl:param>
<xsl:param name="mail">mailto:a-satomi@remus.dti.ne.jp</xsl:param>

<!-- CSS関係デフォルト値 -->
<xsl:param name="cssFile">__defaults.css</xsl:param>
<xsl:param name="cssTitle">ぴんくすとらいぷ</xsl:param>

<!-- rel : home/contents/index/prev/next/rss/foaf な物で使われる各種変数は navi.xsl にて設定される。 -->

<!-- ================ head 要素テンプレ ================  -->

<xsl:template match="h:head">
	<head>
		<xsl:call-template name="head-meta"/>
		<xsl:call-template name="head-title"/>
		<xsl:call-template name="head-naviLink"/>
		<xsl:call-template name="head-metaLink"/>
		<xsl:call-template name="head-miscLink"/>
		<xsl:call-template name="head-css"/>
		<xsl:call-template name="head-script"/>
	</head>
</xsl:template>

<!-- meta -->
<xsl:template name="head-meta">
	<xsl:param name="charset" select="string('Shift_JIS')"/>
	<meta content="text/html; charset={$charset}" http-equiv="Content-Type" />
	<meta content="text/css" http-equiv="Content-Style-Type" />
	<meta content="text/javascript" http-equiv="Content-Script-Type" />
	<meta content="mi + Xalan-Java XSLT processor" name="generator" />
	<xsl:apply-templates select="h:meta"/>
	<xsl:apply-templates select="s:chieri"/>
</xsl:template>

<!-- ちえりリスト（トップページのみ） -->

<xsl:template match="s:chieri">
	<meta content="{substring-before(translate(js:getTime(), '-T', '/ '), '+')}" name="chieri-update" />
	<meta content="{@newitem}" name="chieri-newitem" />
</xsl:template>

<!-- ページタイトル -->
<xsl:template name="head-title">
	<title><xsl:value-of select="$curFileTitle"/></title>
</xsl:template>

<!-- ナビリンク各種 -->
<xsl:template name="head-naviLink">
	<link href="{$rootURI}" title="{$rootTitle}" rel="home" />
	<xsl:if test="string($contentsPath)"><link href="{$contentsURI}" title="{$contentsLabel}" rel="contents" /></xsl:if>
	<xsl:if test="string($indexPath)"><link href="{$indexURI}" title="{$indexLabel}" rel="index" /></xsl:if>
	<xsl:if test="string($prevPath)"><link href="{$prevURI}" title="{$prevLabel}" rel="prev" /></xsl:if>
	<xsl:if test="string($nextPath)"><link href="{$nextURI}" title="{$nextLabel}" rel="next" /></xsl:if>
</xsl:template>

<!-- メタデータリンク -->
<xsl:template name="head-metaLink">
	<xsl:if test="string($rssPath)">
		<link href="{$rssURI}" title="{$rssLabel}" rel="meta" />
		<link href="{$rssURI}" title="{$rssLabel}" type="application/rss+xml" rel="alternate" />
	</xsl:if>
	<xsl:if test="string($foafPath)">
		<link href="http://purl.org/dc/elements/1.1/" rel="schema.FOAF"/> 
		<link href="{$foafURI}" title="{$foafLabel}" type="application/rdf+xml" rel="meta FOAF.MAKER"/> 
	</xsl:if>
</xsl:template>

<!-- その他リンク -->
<xsl:template name="head-miscLink">
	<link href="{$aboutFile}" title="このサイトについて" rel="help" />
	<link href="{$aboutFile}#copy" title="著作権ほか" rel="copyright" />
	<link href="{$siteSearch}" title="しるきぃうぇぶ検索ましーん" rel="search" />
	<link href="{$commonDir}/img/icon/favicon.png" type="image/png" rel="icon" />
	<link href="{$commonDir}/img/icon/favicon.ico" type="image/x-icon" rel="shortcut icon" />
	<link href="{$mail}" title="ありみかさとみメアド" rev="made" />
</xsl:template>

<!-- CSS -->
<xsl:template name="head-css">
	<!--  特に優先スタイルが明示指定されてなければ、デフォルトの優先 CSS を適用-->
	<xsl:if test="not(s:css[@preferred])">
		<link title="{$cssTitle}" href="{$cssDir}/{$cssFile}" media="screen,tv" type="text/css" rel="stylesheet" />
	</xsl:if>
	<!-- 他の追加スタイル適用 -->
	<xsl:apply-templates select="s:css"/>
	<xsl:apply-templates select="s:style"/>
</xsl:template>

<xsl:template match="s:css">
	<!--
	　追加の外部 CSS ファイル (s:css 要素)
	　・href 属性： ./ または ../ で始まる→ xml からの相対 URL, それ以外→既定の CSS dir からの相対 URL
	　・title 属性： 明示的にカラ→ title 属性付加無し, あり→この title 属性値を付加, なし→既定のタイトルを付加
	　・alternate 属性：'yes' → rel="alternate stylesheet", なし → rel="stylesheet"
	　・preferred 属性：あり→優先スタイルとして適用, なし→通常
	-->
	<xsl:variable name="rel">
		<xsl:if test="@alternate = 'yes'">alternate </xsl:if>
		<xsl:text>stylesheet</xsl:text>
	</xsl:variable>

	<xsl:variable name="href">
		<xsl:choose>
			<xsl:when test="starts-with(@href, './')"><xsl:value-of select="substring-after(@href, '/')"/></xsl:when>
			<xsl:when test="starts-with(@href, '../')"><xsl:value-of select="@href"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="concat($cssDir, '/', @href)"/></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:choose>
		<xsl:when test="@title = ''">
			<link href="{$href}" media="screen,tv" type="text/css" rel="{$rel}"/>
		</xsl:when>
		<xsl:when test="string(@title)">
			<link title="{@title}" href="{$href}" media="screen,tv" type="text/css" rel="{$rel}"/>
		</xsl:when>
		<xsl:otherwise>
			<link title="{$cssTitle}" href="{$href}" media="screen,tv" type="text/css" rel="{$rel}"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="s:style">
	<!-- 追加のリテラル style  (s:style 要素) -->
	<style title="{$cssTitle}" media="screen,tv" type="text/css">
		<xsl:apply-templates select="child::text()"/>
	</style>
</xsl:template>

<!-- スクリプト -->
<xsl:template name="head-script">
	<xsl:param name="add-script"/>
	<xsl:param name="defer" select="false()"/>

	<!-- 既定の外部 JavaScript -->
	<xsl:text disable-output-escaping="yes"><![CDATA[
    <script type="text/javascript" src=]]></xsl:text>
		<xsl:value-of select="concat('&quot;', $scriptDir, '/', 'common.js', '&quot;')"/>
		<xsl:text disable-output-escaping="yes"><![CDATA[></script>
  ]]></xsl:text>

	<!-- 追加の外部 JavaScript  (外部 JS のファイル名は引数渡し＝元 XML が無く XSL だけでファイルを生成するケースで利用) -->
	<xsl:if test="string($add-script)">
		<xsl:text disable-output-escaping="yes"><![CDATA[  <script type="text/javascript" src=]]></xsl:text>
		<xsl:value-of select="concat('&quot;', $scriptDir, '/', $add-script, '&quot;')"/>
		<xsl:if test="boolean($defer)"> defer=&quot;defer&quot;</xsl:if>
		<xsl:text disable-output-escaping="yes"><![CDATA[></script>
  ]]></xsl:text>
	</xsl:if>

	<!-- 追加の外部 JavaScript  (元 XML の s:script 要素による指定) -->
	<xsl:apply-templates select="s:script"/>

</xsl:template>

<xsl:template match="s:script">
	<!--
	　追加の外部 JavaScript (元 XML の s:script 要素による指定)
	　・src 属性あり：
	　　./ または ../ で始まる : xml からの相対 URL, それ以外 : 既定の Script dir からの相対 URL
	　・src 属性なし：リテラルのスクリプト
	-->
	<xsl:variable name="src">
		<xsl:choose>
			<xsl:when test="starts-with(@src, './')"><xsl:value-of select="substring-after(@src, '/')"/></xsl:when>
			<xsl:when test="starts-with(@src, '../')"><xsl:value-of select="@src"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="concat($scriptDir, '/', @src)"/></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:choose>
		<xsl:when test="string(@src)">
			<xsl:text disable-output-escaping="yes"><![CDATA[  <script type="text/javascript" src=]]></xsl:text>
			<xsl:value-of select="concat('&quot;', $src, '&quot;')"/>
			<xsl:if test="@defer = 'defer'"> defer=&quot;defer&quot;</xsl:if>
			<xsl:text disable-output-escaping="yes"><![CDATA[></script>
  ]]></xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>  </xsl:text>
			<script type="text/javascript">
				<xsl:if test="@defer = 'defer'"><xsl:attribute name="defer">defer</xsl:attribute></xsl:if>
				<xsl:apply-templates select="child::text()"/>
			</script>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ ナビリンクテンプレ ================  -->

<xsl:template name="navi">
	<ul class="navigation">
		<li class="home">
			<a rel="home" accesskey="T" title="{$rootTitle} [T]" href="{$rootURI}">
				<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
				<xsl:value-of select="js:ct(string($rootLabel), 12)"/>
			</a>
		</li>
		<xsl:if test="string($contentsPath)">
			<li class="contents">
				<a rel="contents" accesskey="I" title="{$contentsTitle} [I]" href="{$contentsURI}">
					<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
					<xsl:value-of select="js:ct(string($contentsLabel), 12)"/>
				</a>
			</li>
		</xsl:if>
		<xsl:if test="string($prevPath)">
			<li class="prev">
				<a rel="prev" accesskey="P" title="{$prevTitle} [P]" href="{$prevURI}">
					<img alt="←" height="15" width="15" src="{$commonDir}/img/icon/LeftArrow.png" />
					<xsl:value-of select="js:ct(string($prevLabel), 12)"/>
				</a>
			</li>
		</xsl:if>
		<xsl:if test="string($nextPath)">
			<li class="next">
				<a rel="next" accesskey="N" title="{$nextTitle} [N]" href="{$nextURI}">
					<img alt="→" height="15" width="15" src="{$commonDir}/img/icon/RightArrow.png" />
					<xsl:value-of select="js:ct(string($nextLabel), 12)"/>
				</a>
			</li>
		</xsl:if>
	</ul>
</xsl:template>

<!-- ================ フッタテンプレ ================  -->

<xsl:template name="footer">
	<!-- xsl:param name="useAccessLogger" select="true()"/ -->
	<xsl:param name="useAccessLogger" select="false()"/>
	<hr />
	<address>
		<xsl:text >Copyright &#169; 1998-2007 ALIMIKA SATOMI/NYAN-NYAN-HANTEN.</xsl:text>
		<xsl:apply-templates select="/h:html/h:head/s:file"/>
	</address>

	<ul id="shortCut">
		<li id="made"><a rev="made" title="ありみかさとみメアド" href="{$mail}">
			<img alt="" height="15" width="18" src="{$commonDir}/img/icon/MailIcon.png" />Mail to Alimika Satomi</a></li>
		<li id="search"><a rel="search" title="しるきぃうぇぶ検索ましーん" href="{$siteSearch}">
			<img alt="" height="15" width="15" src="{$commonDir}/img/icon/glass.png" />Site search</a></li>
		<li id="copyright"><a rel="copyright" title="著作権ほか" href="{$aboutFile}#copy">
			<img alt="" height="15" width="15" src="{$commonDir}/img/icon/caution.png" />About copyrights</a></li>
		<li id="dMomo"><a title="ももいろコミューン" href="{$rootURI}bunsyorou/DedicateMomo.html">
			<img alt="ももいろコミューン" height="31" width="88" src="{$commonDir}/img/banner/dedicateMomo.png" /></a></li>
	</ul>
	<xsl:if test="boolean($useAccessLogger)">
		<xsl:text disable-output-escaping="yes"><![CDATA[
    <script type="text/javascript" src=]]></xsl:text>
		<xsl:value-of select="concat('&quot;', $scriptDir, '/', 'getlog.js', '&quot;')"/>
		<xsl:text disable-output-escaping="yes"><![CDATA[></script>
  ]]></xsl:text>
	</xsl:if>
</xsl:template>

<!-- 作成日、更新日 -->
<xsl:template match="s:file[string(@created) or string(@lastmod)]">
	<span class="datetime">
		<xsl:if test="string(@created)">Created: <xsl:value-of select="js:n2DTF(string(@created))"/></xsl:if>
		<xsl:if test="string(@created) and string(@lastmod)">, </xsl:if>
		<xsl:if test="string(@lastmod)">Last-modified: <xsl:value-of select="js:n2DTF(string(@lastmod))"/></xsl:if>
	</span>
</xsl:template>

<!-- ================ h1 要素 ================  -->
<!--
　・section 構造は一つ目の h1 要素を起点として作る方針。
　・h1 要素の存在は文書中にひとつだけである事を想定。
　・元ファイルの h1 要素への class/id は結果ファイルの body 要素の class/id とする方針
-->

<xsl:template match="h:h1">
	<xsl:call-template name="section"/>
</xsl:template>

<xsl:template match="h:h1[position() != 1]"/>

<!-- ============== blockquote 要素 ================  -->
<!--
　blockquote の直後に s:from 要素があれば、その内部テキストとアンカー URI を title/cite 属性へ
　ただし blockquote 要素自身で明示されている title/cite が優先
 -->
 
<xsl:template match="h:blockquote">
	<blockquote>
		<!-- blockquote の直後の要素が s:from であり、かつそれが a を内包していたら -->
		<xsl:if test="name(following-sibling::*[1]) = 's:from' and following-sibling::s:from[1]/h:a">
			<xsl:attribute name="cite">
				<!-- s:from 内に a が複数あっても1つめのだけ -->
				<xsl:value-of select="following-sibling::s:from[1]/h:a/@href"/>
			</xsl:attribute>
		</xsl:if>

		<!-- blockquote の直後の要素が s:from であるなら -->
		<xsl:if test="name(following-sibling::*[1]) = 's:from'">
			<xsl:attribute name="title">
				<xsl:variable name="titleValue">
					<xsl:for-each select="following-sibling::s:from[1]/node()">
						<xsl:choose>
							<xsl:when test="name(self::node()) = 'a' and string(self::node()/@title)">
								<xsl:value-of select="@title"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="self::node()"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:variable>
				<xsl:value-of select="normalize-space($titleValue)"/>
				<xsl:if test="string(following-sibling::s:from[1]/h:a/@s:datetime)">
					<xsl:value-of select="concat(' (', js:n2DTF(string(following-sibling::s:from[1]/h:a/@s:datetime)), ')')"/>
				</xsl:if>
			</xsl:attribute>
		</xsl:if>

		<!-- 元からある属性値をそのまま付与 -->
		<xsl:apply-templates select="@*"/>
		<!-- 子要素生成 -->
		<xsl:call-template name="innerSection"/>
	</blockquote>
</xsl:template>

<!-- =============  blockquote/ins/del 要素の内部生成 =============  -->
<!--
　blockquote/ins/del 要素内でも見出し要素があるなら section 構造を作る。
　なければそのまま。
-->

<xsl:template match="h:ins | h:del">
	<xsl:element name="{name()}">
		<xsl:apply-templates select="@*"/>
		<xsl:call-template name="innerSection"/>
	</xsl:element>
</xsl:template>

<xsl:template name="innerSection">

	<!-- 子要素中最初の見出し要素の位置 -->
	<xsl:param name="posHx">
		<xsl:for-each select="h:*[
				starts-with(local-name(), 'h') and
				string-length(local-name()) = 2 and
				substring(local-name(), 2, 1) &gt; 0][1]">
			<xsl:number level="single" count="* | comment()"/>
		</xsl:for-each>
	</xsl:param>

	<!-- 子要素生成 -->
	<xsl:choose>
		<!-- 子要素として見出し要素を含んでいる場合 -->
		<xsl:when test="$posHx &gt; 0">
			<!-- 見出しより手前の要素を生成 -->
			<xsl:apply-templates select="*[position() &lt; $posHx]"/>
			<!-- 見出しとそれに続く部分を section 構造化 -->
			<xsl:for-each select="h:*[position() = $posHx]">
				<xsl:call-template name="section"/>
			</xsl:for-each>
		</xsl:when>
		<!-- 子要素に見出し要素が無い場合 -->
		<xsl:otherwise>
			<!-- そのまま子要素生成 -->
			<xsl:apply-templates/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ pre 要素 ================  -->
<!--
　pre 要素内で勝手に改行やインデント整形されるのを防ぐ
-->

<xsl:template match="h:pre">
	<pre>
		<xsl:apply-templates select="@*"/>
		<xsl:for-each select="child::node()">
			<xsl:choose>
				<xsl:when test="string(name()) and not(starts-with(name(), 's:'))">
					<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
					<xsl:value-of select="name()"/>
					<xsl:for-each select="@*">
						<xsl:text> </xsl:text>
						<xsl:value-of select="name()"/>
						<xsl:text>=&quot;</xsl:text>
						<xsl:value-of select="self::node()"/>
						<xsl:text>&quot;</xsl:text>
					</xsl:for-each>
					<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
					<xsl:apply-templates/>
					<xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
					<xsl:value-of select="name()"/>
					<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="self::node()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</pre>
</xsl:template>

<!-- ================ a 要素 (s:datetime 属性付き)  ================  -->

<xsl:template match="h:a">
	<a>
		<xsl:apply-templates select="node() | @*[name() != 's:datetime']"/>
		<xsl:apply-templates select="@s:datetime"/>
	</a>
</xsl:template>

<!-- ================ dfn, span 要素 (title 属性有り) ================  -->

<xsl:template match="h:dfn[string(@title)] | h:span[string(@title)]">
	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="@*"/>
		<xsl:attribute name="class">
			<xsl:text>cfTitle</xsl:text>
			<xsl:if test="string(@class)">
				<xsl:text> </xsl:text>
				<xsl:value-of select="@class"/>
			</xsl:if>
		</xsl:attribute>
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<xsl:template match="h:input[@type = 'submit' or @type = 'checkbox' or @type = 'radio']">
	<input>
		<xsl:apply-templates select="@*"/>
		<xsl:attribute name="class">
			<xsl:value-of select="@type"/>
			<xsl:if test="string(@class)">
				<xsl:text> </xsl:text>
				<xsl:value-of select="@class"/>
			</xsl:if>
		</xsl:attribute>
	</input>
</xsl:template>

<!-- ================ s:toc 要素 (table of contents) ================  -->

<xsl:template match="s:toc">
	<ul>
		<xsl:apply-templates select="@*"/>
		<xsl:for-each select="/h:html/h:h2[@id != '']">
			<li><a href="#{@id}"><xsl:value-of select="."/></a></li>
		</xsl:for-each>
	</ul>
</xsl:template>

<!-- ================ s:pictute 要素 (画像ブロック) ================  -->
<!--
　・div.picture への置換。ただし s:picture の mode 属性があれば、その属性値を要素名とする。
　・子孫要素の img の width 属性値が 400 より小さいときは class 属性値に 'small' が追加される。
　・子孫要素の img の width に関わらず、 s:picture の display 属性値が 'block' のときは 'small' は追加されない。
　・子孫要素の img の width に関わらず、 s:picture の display 属性値が 'small' のときは 'small' が追加される。
-->

<xsl:template match="s:picture">
	<xsl:param name="element">
		<xsl:choose>
			<xsl:when test="string(@mode)"><xsl:value-of select="@mode"/></xsl:when>
			<xsl:otherwise>div</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:element name="{$element}">
		<xsl:attribute name="class">
			<xsl:choose>
				<xsl:when test="@display = 'block'">picture</xsl:when>
				<xsl:when test="@display = 'small'">picture small</xsl:when>
				<xsl:when test="descendant::h:img/@width &gt; 400">picture</xsl:when>
				<xsl:otherwise>picture small</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="string(@class)">
				<xsl:value-of select="concat(' ', @class)"/>
			</xsl:if>
		</xsl:attribute>
		<xsl:apply-templates select="node() | @*[name() != 'class' and name() != 'mode' and name() != 'display']"/>
	</xsl:element>
</xsl:template>

<!-- ================ s:reflink 要素 (参考リンク) ================  -->

<!--
　<s:reflink><li>A</li><s:from>B</s:from> ... </s:reflink>
　　　→ <ul class="refLink"><li>A<cite>(B)</cite></li> ... </ul>
　　　<s:from> は無くても可
　<s:reflink><li>A</li><s:from>B</s:from><s:data>C</sdata> ... </s:reflink>
　　　→ <dl class="refLink"><dt>A<cite>(B)</cite></dt><dd>C</dd> ... </dl>
　　　<s:from> は無くても可。<li> と <s:data> はかならず一対で。
-->

<xsl:template match="s:reflink">
	<xsl:choose>
		<xsl:when test="s:data">
			<dl>
				<xsl:apply-templates select="@*[name() != 'class']"/>
				<xsl:attribute name="class">
					<xsl:text>refLink</xsl:text>
					<xsl:if test="string(@class)"><xsl:value-of select="concat(' ', @class)"/></xsl:if>
				</xsl:attribute>
				<xsl:for-each select="h:li">
					<dt>
						<xsl:apply-templates/>
						<xsl:if test="name(following-sibling::*[1]) = 's:from'">
							<cite>(<xsl:apply-templates select="following-sibling::s:from[1]/node()"/>)</cite>
						</xsl:if>
					</dt>
					<dd><xsl:apply-templates select="following-sibling::s:data[1]/node()"/></dd>
				</xsl:for-each>
			</dl>
		</xsl:when>
		<xsl:otherwise>
			<ul>
				<xsl:apply-templates select="@*[name() != 'class']"/>
				<xsl:attribute name="class">
					<xsl:text>refLink</xsl:text>
					<xsl:if test="string(@class)"><xsl:value-of select="concat(' ', @class)"/></xsl:if>
				</xsl:attribute>
				<xsl:for-each select="h:li">
					<li>
						<xsl:apply-templates/>
						<xsl:if test="name(following-sibling::*[1]) = 's:from'">
							<cite>(<xsl:apply-templates select="following-sibling::s:from[1]/node()"/>)</cite>
						</xsl:if>
					</li>
				</xsl:for-each>
			</ul>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- =========== s:from 要素 (引用元/経由元表示) ================  -->

<!-- blockquote または s:blockquote または s:reflink の直後の s:from 要素。 display 属性値が none の場合はスルー-->

<xsl:template match="
	   s:from[name(preceding-sibling::*[1]) = 'blockquote'
	or name(preceding-sibling::*[1]) = 's:blockquote'
	or name(preceding-sibling::*[1]) = 's:reflink']">
	<xsl:param name="lang">
		<xsl:for-each select="preceding-sibling::h:*[
				starts-with(local-name(), 'h') and
				string-length(local-name()) = 2 and
				substring(local-name(), 2, 1) &gt; 0][1]">
			<xsl:value-of select="@xml:lang"/>
		</xsl:for-each>
	</xsl:param>

	<xsl:param name="label">
		<xsl:choose>
			<xsl:when test="name(preceding-sibling::*[1]) = 'blockquote' or name(preceding-sibling::*[1]) = 's:blockquote'">
				<xsl:choose>
					<xsl:when test="string($lang) and $lang != 'ja'">from </xsl:when>
					<xsl:otherwise> より</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="name(preceding-sibling::*[1]) = 's:reflink'">
				<xsl:choose>
					<xsl:when test="string($lang) and $lang != 'ja'">via </xsl:when>
					<xsl:otherwise> 経由</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>
	</xsl:param>

	<xsl:if test="string(@display) != 'none'">
		<p class="source">
			<xsl:text>[ </xsl:text>
			<xsl:if test="string($lang) and $lang != 'ja'">
				<xsl:value-of select="$label"/>
			</xsl:if>
			<cite><xsl:apply-templates/></cite>
			<xsl:if test="not(string($lang)) or $lang = 'ja'">
				<xsl:value-of select="$label"/>
			</xsl:if>
			<xsl:text> ]</xsl:text>
		</p>
	</xsl:if>
</xsl:template>

<!-- ================ s:kaiwa 要素 (会話文) ================  -->

<!--
　<s:kaiwa><dt>名前</dt><dd>セリフ</dd> ... </s:kaiwa>
　　　→ <dl class="kaiwa"><dt>名前</dt><dd>セリフ</dd> ... </dl>
　<dt> と <dd> はかならず一対で。
-->

<xsl:template match="s:kaiwa">
<dl>
	<xsl:apply-templates select="@*[name() != 'class']"/>
	<xsl:attribute name="class">
		<xsl:text>kaiwa</xsl:text>
		<xsl:if test="string(@class)"><xsl:value-of select="concat(' ', @class)"/></xsl:if>
	</xsl:attribute>
	<xsl:for-each select="h:dt">
		<xsl:apply-templates select="self::node()"/>
		<dd>「<xsl:apply-templates select="following-sibling::h:dd[1]/node()"/>」</dd>
	</xsl:for-each>
</dl>
</xsl:template>

<!-- ================ s:memo 要素 (メモもしくは追記) ================  -->

<xsl:template match="s:memo">
	<xsl:param name="element">
		<xsl:choose>
			<xsl:when test="string(@mode)"><xsl:value-of select="@mode"/></xsl:when>
			<xsl:otherwise>span</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<xsl:element name="{$element}">
		<xsl:attribute name="class">
			<xsl:text>memo</xsl:text>
			<xsl:if test="@class">
				<xsl:text> </xsl:text><xsl:value-of select="@class"/>
			</xsl:if>
		</xsl:attribute>
		<xsl:apply-templates select="node() | @*[name() != 'class' and name() != 'mode']"/>
	</xsl:element>
</xsl:template>

<!-- ========== s:datetime 要素 / s:datetime 属性（日付） ==========  -->

<xsl:template match="s:datetime">
	<span class="datetime"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="@s:datetime">
	<xsl:text> </xsl:text>
	<span class="datetime">(<xsl:value-of select="js:n2DTF(string(self::node()))"/>)</span>
</xsl:template>

<!-- ================ s:rss-link 要素 (RSS へのリンク生成) ================  -->

<xsl:template match="s:rss-link">
	<a title="RSS : {$rssLabel}" href="{$rssURI}">
		<img alt="RSS : " height="16" width="16" src="{$commonDir}/img/icon/feed.png"/>
		<xsl:value-of select="$rssLabel"/>
	</a>
</xsl:template>

<!-- ========== s:a, s:area, s:q, s:blockquote 要素 (自サイト内参照) ==========  -->

<!--
　リンク先文書を解析して title 属性値を自動的に設定。
　空要素の s:a 要素にはアンカーテキストを自動設定。
　・参照先ファイルが fileList.xml に記載の無い場合は使用不能
　・参照先を示す href/cite 属性値は相対パスまたは基準ディレクトリからの仮想的フルパスで指定。
　・title 属性値が明示的にカラの場合は title 属性そのものを出力しない。
-->

<xsl:template match="s:a | s:area | s:q | s:blockquote">
	<xsl:param name="uri">
		<xsl:choose>
			<xsl:when test="name() = 's:q' or name() = 's:blockquote'">
				<xsl:value-of select="@cite"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@href"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:param name="targetPath">
		<xsl:value-of select="js:r2a(string($curFilePath), string($uri))"/>
	</xsl:param>

	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="@*[name() != 'title']"/>
		<xsl:choose>
			<xsl:when test="string(@title)">
				<xsl:apply-templates select="@title"/>
			</xsl:when>
			<xsl:when test="name() != 's:area' and (not(@title) or @title != '')">
				<xsl:attribute name="title">
					<xsl:call-template name="makeAnchorTitle">
						<xsl:with-param name="path" select="$targetPath"/>
					</xsl:call-template>
				</xsl:attribute>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>

		<xsl:if test="name() = 's:area' and not(@alt)">
			<xsl:attribute name="alt">
				<xsl:call-template name="makeAnchorText">
					<xsl:with-param name="path" select="$targetPath"/>
				</xsl:call-template>
			</xsl:attribute>
		</xsl:if>

		<xsl:choose>
			<xsl:when test="name() = 's:blockquote' and string(self::node())">
				<xsl:call-template name="innerSection"/>
			</xsl:when>
			<xsl:when test="string(self::node()) or h:img">
				<xsl:apply-templates/>
			</xsl:when>
			<xsl:when test="name() = 's:a'">
				<xsl:call-template name="makeAnchorText">
					<xsl:with-param name="path" select="$targetPath"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="(name() = 's:q' or name() = 's:blockquote') and not(string(self::node()))">
				<xsl:message terminate="yes">[Quote text is empty] </xsl:message>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>
	</xsl:element>
</xsl:template>

<xsl:template match="s:*/@href | s:*/@cite">
	<xsl:param name="targetPath" select="js:a2r(string($curFilePath), string(self::node()))"/>

	<xsl:attribute name="{local-name()}">
		<xsl:choose>
			<xsl:when test="parent::node()/@chsuffix = 'no'">
				<xsl:value-of select="$targetPath"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="js:omitDirIndex(js:chsuffix($targetPath, 'html'))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:attribute>
</xsl:template>

<!-- ================ アンカーテキストを生成 ================  -->
<!-- 自サイト内参照 (s:a, s:area, s:q, s:blockquote) やナビリンク生成テンプレ向け -->

<xsl:template name="makeAnchorText">
	<xsl:param name="path"/><!-- 基準ディレクトリからの仮想的フルパス -->
	<xsl:param name="return-value">
		<xsl:call-template name="searchFile">
			<xsl:with-param name="path" select="$path"/>
			<xsl:with-param name="data">title</xsl:with-param>
		</xsl:call-template>
	</xsl:param>

	<xsl:choose>
		<xsl:when test="string($return-value)">
			<xsl:value-of select="$return-value"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message terminate="yes">
				<xsl:text>[Anchor text may be empty] </xsl:text>
				<xsl:value-of select="$path"/>
			</xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ アンカータイトルを生成 ================  -->
<!-- 自サイト内参照 (s:a, s:area, s:q, s:blockquote) やナビリンク生成テンプレ向け -->

<xsl:template name="makeAnchorTitle">
	<!-- 基準ディレクトリからの仮想的フルパス -->
	<xsl:param name="path"/>

	<!-- サイト名やコーナー名を接尾辞として付けるか否か -->
	<xsl:param name="addAncorTitleSuffix" select="$curFilePath != concat('/', $curDirName, '/index.xml') and $curFilePath != '/index.xml'"/>

	<!-- ファイルパス中 id フラグメント -->
	<xsl:param name="id" select="substring-after($path, '#')"/>

	<!-- $path のファイル名部分 (idフラグメント部を除いたもの) -->
	<xsl:param name="path_">
		<xsl:choose>
			<xsl:when test="$id">
				<xsl:value-of select="substring-before($path, '#')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$path"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	
	<!-- リンク先ファイル中の該当 id を持つ要素の内容値 (主に見出し要素のみを想定) -->
	<xsl:param name="targetIDTitle">
		<xsl:if test="$id">
			<xsl:call-template name="searchFile">
				<xsl:with-param name="path" select="$path"/>
				<xsl:with-param name="data">title</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:param>

	<!-- リンク先ファイルのタイトル (h1要素の内容値) -->
	<xsl:param name="targetFileTitle">
		<xsl:call-template name="searchFile">
			<xsl:with-param name="path" select="$path_"/>
			<xsl:with-param name="data">title</xsl:with-param>
		</xsl:call-template>
	</xsl:param>

	<!-- リンク先ファイルが属している直接の親ページのタイトル -->
	<xsl:param name="targetFileParentTitle">
		<xsl:call-template name="searchFile">
			<xsl:with-param name="path" select="$path_"/>
			<xsl:with-param name="rel">contents</xsl:with-param>
			<xsl:with-param name="data">title</xsl:with-param>
		</xsl:call-template>
	</xsl:param>

	<!-- リンク先ファイル中の該当 id を持つ要素の s:datetime 属性値（主に日記） -->
	<xsl:param name="datetime">
		<xsl:if test="$id">
			<xsl:value-of select="document(concat($baseDir, $path))/h:html/h:*[@id = $id]/@s:datetime"/>
		</xsl:if>
	</xsl:param>

	<xsl:param name="return-value">
		<xsl:if test="$id">
			<xsl:value-of select="$targetIDTitle"/>
			<xsl:text> - </xsl:text>
		</xsl:if>

		<xsl:choose>
			<xsl:when test="starts-with($path, '/nikki')">
				<xsl:choose>
					<xsl:when test="$id">
						<xsl:text>ねこめしにっき</xsl:text>
						<xsl:if test="string($datetime)">
							<xsl:text> (</xsl:text>
							<xsl:value-of select="js:n2DTF(substring-before($datetime, 'T'))"/>
							<xsl:text>)</xsl:text>
						</xsl:if>
					</xsl:when>
					<xsl:otherwise>
						<!-- xsl:value-of select="$targetFileTitle"/ --><!-- この場合は filelist.xml の file 要素の内容 -->
						<xsl:value-of select="document(concat($baseDir, $path))/descendant::h:h1"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$targetFileTitle"/>
				<xsl:if test="$addAncorTitleSuffix">
					<xsl:if test="string($targetFileParentTitle) 
							and $targetFileParentTitle != $rootLabel 
							and $targetFileParentTitle != $targetFileTitle">
						<xsl:text> - </xsl:text><xsl:value-of select="$targetFileParentTitle"/>
					</xsl:if>
<!--
					<xsl:text> - </xsl:text><xsl:value-of select="$rootTitle"/>
-->
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:choose>
		<xsl:when test="string($return-value)">
			<xsl:value-of select="$return-value"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message terminate="yes">
				<xsl:text>[Anchor title may be empty] </xsl:text>
				<xsl:choose>
					<xsl:when test="$id">
						<xsl:value-of select="concat($path, '#', $id)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$path"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ========== s:wb 要素 (にっき WriteBack ページへのリンク生成) ==========  -->

<!--
　・空要素の s:wblink 要素にはアンカーテキストを自動設定。
　・参照先を示す href 属性値は相対パスまたは基準ディレクトリからの仮想的フルパスで指定。
　・参照先を示す href 属性値は必ずフラグメント ID の指定が必要。
　・参照先ファイルが fileList.xml に記載の無い場合、指定フラグメント ID が存在しない場合は使用不能
　・title 属性値が明示的にカラの場合は title 属性そのものを出力しない。
-->

<xsl:template match="s:wb">
	<xsl:param name="path" select="js:r2a(string($curFilePath), string(@href))"/>
	<xsl:param name="id" select="substring-after($path, '#')"/>
	<xsl:param name="wb" select="string('http://alimika.alib.jp/nikki/writeback')"/>
	<xsl:param name="tb_id" select="js:replace($path, '/.+\/(\d+)\/(\w+)\.xml#d(\d+)n(\d+)$/', '$1_$2_$3_$4')"/>
	<xsl:param name="title">
		<xsl:call-template name="searchFile">
			<xsl:with-param name="path" select="$path"/>
			<xsl:with-param name="data">title</xsl:with-param>
		</xsl:call-template>
	</xsl:param>
	<xsl:param name="text" select="concat('「', $title, '」へのご意見ご感想')"/>
	
	<xsl:element name="a">
		<xsl:apply-templates select="@*[name() != 'href' and name() != 'title']"/>
		<xsl:attribute name="href">
			<xsl:value-of select="concat($wb, '/list/', $tb_id)"/>
		</xsl:attribute>
		<xsl:if test="not(@title) or @title != ''">
			<xsl:attribute name="title">
				<xsl:choose>
					<xsl:when test="string(@title)">
						<xsl:value-of select="@title"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$text"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="string(self::node())">
				<xsl:apply-templates/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$text"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:element>
</xsl:template>

<!-- ============== 一連のファイルから最新の更新日付を取得 ==============  -->
<!--
　兄弟ページである一連のファイル群の中から、最新の更新日付 (YYYY/MM/DD) を取り出す。
　一連ファイルのうち、ファイルリスト中で最も先頭に位置する物の仮想的フルパスを引数 $pathに与える事。
　(rel="next" の関係性をたどってファイル群を走査するるユエ)
 -->

<xsl:template name="getLatestModifyDay">
	<xsl:param name="path"/><!-- 現在調査対象ファイルの仮想的フルパス。 -->
	<xsl:param name="x" select="0"/><!-- 今のところの最大値 -->

	<!-- 現在調査中ファイルのモノとそれまでの最大値で比較した、大きい方 -->
	<xsl:param name="max">
		<xsl:choose>
			<xsl:when test="translate(document(concat($baseDir, $path))/h:html/h:head/s:file/@lastmod, '/-', '') &gt; $x">
				<xsl:value-of select="translate(document(concat($baseDir, $path))/h:html/h:head/s:file/@lastmod, '/-', '')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$x"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<!-- 次の調査対象ファイルの仮想的フルパス -->
	<xsl:param name="nextFilePath">
		<xsl:call-template name="searchFile">
			<xsl:with-param name="path" select="$path"/>
			<xsl:with-param name="rel">next</xsl:with-param>
			<xsl:with-param name="data">path</xsl:with-param>
			<!-- xsl:with-param name="excludeIndex" select="false()"/ -->
		</xsl:call-template>
	</xsl:param>

	<xsl:choose>
		<!-- 次の調査対象ファイルが存在してたら -->
		<xsl:when test="string($nextFilePath)">
			<xsl:call-template name="getLatestModifyDay">
				<xsl:with-param name="path" select="$nextFilePath"/>
				<xsl:with-param name="x" select="$max"/>
			</xsl:call-template>
		</xsl:when>
		<!-- 次の調査対象ファイルが無いなら、最大値を W3C DTF (YYYY-MM-DD) 形式へ整形して返す -->
		<xsl:otherwise>
			<xsl:value-of select="concat(substring($max, 1, 4), '-', substring($max, 5, 2), '-', substring($max, 7, 2))"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================== ビルドインテンプレートの変更 ================= -->

<!-- 要素自体も出力するようにする、よくある変更（未使用） -->

<!-- xsl:template match="@*|node()" priority="-5.0" -->
	<!-- xsl:copy -->
		<!-- xsl:apply-templates select="@*|node()" / -->
	<!-- /xsl:copy -->
<!-- /xsl:template -->

<!-- XHTML 以外の名前空間が付くのを阻止しつつ XHTML 要素のみコピー -->

<xsl:template match="h:*" priority="-5.0">
	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="node() | @*"/>
	</xsl:element>
</xsl:template>

<xsl:template match="s:*" priority="-5.0"/>

<!-- XHTML 以外の属性は一部を除いて通さない -->

<xsl:template match="@xml:* | @ap:banner">
	<xsl:copy/>
</xsl:template>

<xsl:template match="@*" priority="-5.0">
	<xsl:if test="not(contains(name(), ':'))">
		<xsl:copy/>
	</xsl:if>
</xsl:template>

<!-- コメントをそのまんま通す -->

<xsl:template match="comment()">
	<xsl:copy-of select="."/>
</xsl:template>

<!-- ============ インデント制御 =============  -->

<!-- 全要素中の空白文字だけのテキストノードを消す（綺麗なインデント出力のために必要） -->

<xsl:strip-space elements="*"/>

<!-- s:no-indent 属性 : 指定した要素以下の子孫要素のインデント整形を防ぐ（ただし不完全） -->

<xsl:preserve-space elements="*[@s:no-indent] *[@s:no-indent]/*"/>

<!-- リテラル script/style 要素のインデントを自力調整 -->

<xsl:template match="s:style/text() | s:script/text()">
	<xsl:value-of select="js:deleteTab(string(self::node()))" disable-output-escaping="yes"/>
</xsl:template>

</xsl:stylesheet>
