<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
			exclude-result-prefixes="user" xmlns:user="urn:extra-functions"
>
<!--
Author:	Conor Ryan, XML Workshop Ltd.
Date Created:	20020225
Date modified:	20020305
Description:	XSLT stylesheet to generate multiple HTML files from single Word document.
-->

<msxsl:script xmlns:msxsl="urn:schemas-microsoft-com:xslt" language="JScript" implements-prefix="user">
<![CDATA[
	function write(nodes, outputFile)
	{
		var objFSO = new ActiveXObject("Scripting.FileSystemObject");
		var objFile = objFSO.CreateTextFile(outputFile, true);
		var tmpstr  = objFSO.getabsolutepathname(outputFile);
		//print("tmpstr");
		try
		{
			for (var i = 0; i < nodes.length; i++)
			{
				objFile.Write(nodes.item(i).xml);
				//objFile.Write("&#xD;");
			}
		}
		finally
		{
			objFile.Close();
			delete objFSO;
			delete objFile;
		}
		return("");
	}
]]>
</msxsl:script>

<!--	Settings	-->
<xsl:output indent="yes" method="html" />

<!--	Top Level Parameters	X:\Pearsons\20020311\0102\-->
<xsl:variable name="dcSource" select="/article/articleinfo/keywordset/keyword[@role='DC.Source']" />
<xsl:variable name="fieldStr" select="'* FILENAME \p  \* MERGEFORMAT *'" />
<xsl:variable name="outputpath">
	<xsl:variable name="lenFilename">
		<xsl:call-template name="processTitle">
			<xsl:with-param name="titleStr" select="$dcSource" />
		</xsl:call-template>
	</xsl:variable>
	<xsl:value-of select="substring($dcSource, string-length($fieldStr) + 1, (string-length($dcSource) - number($lenFilename) - string-length($fieldStr) - 1))" />
</xsl:variable>
<xsl:param name="filename" select="substring-before(concat(article/articleinfo/title, '.'), '.')" />

<!--	Top Level Variables	-->
<xsl:variable name="htmltemplate" select="document('C:\Program Files\yawc\JournalStructure\yawcWebDirTemplate.html')" />
<xsl:variable name="data" select="/" />
<xsl:variable name="lastpage" select="count(/article/section)" />
<xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<!--	Template Matches	-->
<xsl:template match="/article">
	<xsl:apply-templates select="section/section/section/section" />
</xsl:template>

<!--	Processing-Instructions Processing	-->
<xsl:template match="processing-instruction('yawc')[.= 'insert-title']">
	<xsl:apply-templates select="$data/article/title/node()" />
</xsl:template>

<xsl:template match="processing-instruction('yawc')[.= 'insert-content']">
	<xsl:param name="currId" />
	<xsl:apply-templates select="$data/article/section/section/section/section[generate-id(.) = $currId]/*" />
</xsl:template>

<xsl:template match="processing-instruction('yawc')[.= 'insert-ToC']">
	<xsl:param name="currId" />
	<xsl:variable name="thisType" select="$data/article/section/section[section/section[generate-id(.) = $currId]]/title" />

</xsl:template>

<!--	Temp XML Processing	-->
<xsl:template match="section">
	<xsl:variable name="thispos" select="count(preceding-sibling::section) + 1" />
	<xsl:variable name="file" select="translate(concat(string($outputpath), ../../title, '/', ../title, $thispos, '.htm'), $ucase, $lcase)" />
	<xsl:variable name="dataChunk">
		<xsl:apply-templates select="$htmltemplate/*">
			<xsl:with-param name="currId" select="generate-id(.)" />
		</xsl:apply-templates>
	</xsl:variable>
	<xsl:value-of select="user:write($dataChunk, $file)"/>
</xsl:template>

<xsl:template match="section/title">
	<h1><xsl:apply-templates /></h1>
</xsl:template>

<xsl:template match="ulink">
	<a href="{@url}"><xsl:value-of select="."/></a>
</xsl:template>
<xsl:template match="para">
	<p>
		<xsl:choose>
			<xsl:when test="@class = 'Indented text'"><i><xsl:apply-templates /></i></xsl:when>
			<xsl:when test="@role='Byline'"><i><xsl:apply-templates /></i></xsl:when>
			<xsl:otherwise><xsl:apply-templates /></xsl:otherwise>
		</xsl:choose>
	</p>
</xsl:template>

<xsl:template match="itemizedlist">
	<ul>
		<xsl:apply-templates />
	</ul>
</xsl:template>

<xsl:template match="listitem">
	<li>
		<xsl:apply-templates />
	</li>
</xsl:template>

<xsl:template match="emphasis[@role='strong']">
	<b>
		<xsl:apply-templates />
	</b>
</xsl:template>

<xsl:template name="processTitle">
	<xsl:param name="titleStr" />
	<xsl:choose>
		<xsl:when test="contains($titleStr, $fieldStr)">
			<xsl:call-template name="processTitle">
				<xsl:with-param name="titleStr" select="substring($titleStr, string-length($fieldStr) + 1, (string-length($titleStr) - string-length($fieldStr) - 1))" />
			</xsl:call-template>
		</xsl:when>
		<xsl:when test="contains($titleStr, '\')">
			<xsl:call-template name="processTitle">
				<xsl:with-param name="titleStr" select="substring-after($titleStr, '\')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise><xsl:value-of select="string-length($titleStr)" /></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- CSV Stuff	-->

<!-- End of CSV Stuff	-->
<xsl:template match="script">
	<script language="{@language}" src="{@src}" type="{@type}">&#160;</script>
</xsl:template>

<!--	Identity Transformation	-->
<xsl:template match="* | @*">
	<xsl:param name="currId" />
	<xsl:copy>
		<xsl:apply-templates select="node()|@*">
			<xsl:with-param name="currId" select="$currId" />
		</xsl:apply-templates>
	</xsl:copy>
</xsl:template>
</xsl:stylesheet>