<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fm="http://www.itp-x.co.uk/xslt" xmlns:xlink="http://www.w3.org/1999/xlink">

<!--
****************************************************************
* ADOBE SYSTEMS INCORPORATED                                   *
* Copyright 2006 © 2006 Adobe Systems Incorporated             *
* All Rights Reserved                                          *
*                                                              *
* NOTICE:  Adobe permits you to use, modify, and distribute    *
* this file in accordance with the terms of the Adobe license  *
* agreement accompanying it.  If you have received this file   *
* from a source other than Adobe, then your use, modification, *
* or distribution of it requires the prior written permission  *
* of Adobe.                                                    *
* _____________________________________________________________*
* Adobe FrameMaker 7.2 Application Pack for S1000D             *
*                                                              *
* Figure export transform                                      *
* Version   Date         By                                    *
* 0.2b      2006-08-21   Ian Proudfoot, iTP-X                  *
*                                                              *
****************************************************************
-->

	<!--This template reinstates the unparsed entity declarations-->
	<xsl:template match="/">
		<xsl:text disable-output-escaping="yes">&lt;!DOCTYPE dmodule [</xsl:text>
		<!--<xsl:if test="$notation = 'pdf' or $notation = 'PDF'">PDF</xsl:if>
			<xsl:if test="$notation = 'cgm' or $notation = 'CGM'">CGM</xsl:if>
			<xsl:if test="$notation = 'gif' or $notation = 'GIF'">GIF</xsl:if>
			<xsl:if test="$notation = 'png' or $notation = 'PNG'">PNG</xsl:if>
			<xsl:if test="$notation = 'fax' or $notation = 'FAX' or $notation = 'cg4' or $notation = 'CG4'">FAX</xsl:if>
			<xsl:if test="$notation = 'jpg' or $notation = 'JPG' or $notation = 'jpeg' or $notation = 'JPEG'">JPG</xsl:if>-->

		<xsl:text disable-output-escaping="yes">
&lt;!NOTATION CGM PUBLIC "-//USA-DOD//NOTATION Computer Graphics Metafile//EN" ""&gt;</xsl:text>
		<xsl:for-each select="//graphic[not(@boardno = preceding::graphic/@boardno)]">
			<xsl:variable name="grfile">
				<xsl:call-template name="get-file-name">
					<xsl:with-param name="boardno" select="@boardno"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="notation" select="substring-after($grfile, '.')"/>
			<xsl:variable name="entity">
				<xsl:call-template name="get-entity-name">
					<xsl:with-param name="boardno" select="@boardno"/>
				</xsl:call-template>
			</xsl:variable>

			<xsl:text disable-output-escaping="yes">
&lt;!ENTITY </xsl:text>
			<xsl:value-of select="$entity"/>
			<xsl:text> SYSTEM "</xsl:text>
			<xsl:value-of select="$grfile"/>
			<xsl:text>" NDATA </xsl:text>
			<xsl:choose>
				<xsl:when test="$notation = 'pdf' or $notation = 'PDF'">
					<xsl:text>PDF</xsl:text>
				</xsl:when>
				<xsl:when test="$notation = 'cgm' or $notation = 'CGM'">
					<xsl:text>CGM</xsl:text>
				</xsl:when>
				<xsl:when test="$notation = 'gif' or $notation = 'GIF'">
					<xsl:text>GIF</xsl:text>
				</xsl:when>
				<xsl:when test="$notation = 'png' or $notation = 'PNG'">
					<xsl:text>PNG</xsl:text>
				</xsl:when>
				<xsl:when test="$notation = 'fax' or $notation = 'FAX'  or $notation = 'cg4' or $notation = 'CG4'">
					<xsl:text>FAX</xsl:text>
				</xsl:when>
				<xsl:when test="$notation = 'jpg' or $notation = 'JPG' or $notation = 'jpeg' or $notation = 'JPEG'">
					<xsl:text>JPEG</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$notation"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
		</xsl:for-each>
		<xsl:text disable-output-escaping="yes">
]&gt;
 </xsl:text>
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="fm:graphic"/>

	<xsl:template match="fm:graphic/graphic">
		<xsl:copy use-attribute-sets="graphic"/>
		<xsl:copy-of select="hotspot"/>
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="figure">
		<xsl:element name="figure">
			<xsl:copy-of select="applic"/>
			<xsl:copy-of select="@mark | @change | @level | @rfc | @id"/>
			<xsl:element name="title">
				<xsl:for-each select="fig-title/@class">
					<xsl:call-template name="class"/>
				</xsl:for-each>
				<xsl:for-each select="fig-title/@caveat">
					<xsl:call-template name="caveat"/>
				</xsl:for-each>
				<xsl:for-each select="fig-title/@commcls">
					<xsl:call-template name="commcls"/>
				</xsl:for-each>
				<xsl:value-of select="fig-title"/>
			</xsl:element>

			<xsl:choose>
				<xsl:when test="sheet/fm:graphic/graphic">
					<xsl:for-each select="sheet">
						<xsl:for-each select="applic">
							<xsl:copy-of select="."/>
						</xsl:for-each>
						<xsl:element name="sheet">
							<xsl:copy-of select="@mark | @id | @change | @total | @sheetno | @rfc | @level"/>
						</xsl:element>
						<xsl:for-each select="fm:graphic/graphic">
							<xsl:element name="graphic">
								<xsl:attribute name="boardno">
									<xsl:call-template name="get-entity-name">
										<xsl:with-param name="boardno" select="@boardno"/>
									</xsl:call-template>
								</xsl:attribute>
								<xsl:for-each select="following-sibling::hotspot">
									<xsl:copy-of select="."/>
								</xsl:for-each>
								<xsl:for-each select="following-sibling::rfa">
									<xsl:copy-of select="."/>
								</xsl:for-each>
							</xsl:element>
						</xsl:for-each>
					</xsl:for-each>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="fm:graphic/graphic">
						<xsl:element name="graphic">
							<xsl:copy-of select="@id | @reprowid | @reprohgt | @reproscl"/>
							<xsl:copy-of select="@xlink:actuate | @xlink:type | @xlink:title | @xlink:href | @xlink:show"/>
							<xsl:attribute name="boardno">
								<xsl:call-template name="get-entity-name">
									<xsl:with-param name="boardno" select="@boardno"/>
								</xsl:call-template>
							</xsl:attribute>
							<xsl:for-each select="following-sibling::hotspot">
								<xsl:copy-of select="."/>
							</xsl:for-each>
							<xsl:for-each select="following-sibling::rfa">
								<xsl:copy-of select="."/>
							</xsl:for-each>
						</xsl:element>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:for-each select="legend">
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>

	<xsl:template name="substring-before-last">
		<xsl:param name="input"/>
		<xsl:param name="substr"/>
		<xsl:if test="$substr and contains($input, $substr)">
			<xsl:variable name="temp" select="substring-before($input, $substr)"/>
			<xsl:value-of select="substring-before($input, $substr)"/>
			<xsl:if test="contains($temp, $substr)">
				<xsl:value-of select="$substr"/>
				<xsl:call-template name="substring-before-last">
					<xsl:with-param name="input" select="$temp"/>
					<xsl:with-param name="substr" select="$substr"/>
				</xsl:call-template>
			</xsl:if>
		</xsl:if>
	</xsl:template>

	<xsl:template name="substring-after-last">
		<xsl:param name="input"/>
		<xsl:param name="substr"/>
		<xsl:variable name="temp" select="substring-after($input, $substr)"/>
		<xsl:choose>
			<!--If it still contains the search string, then recursively process-->
			<xsl:when test="$substr and contains($temp, $substr)">
				<xsl:call-template name="substring-after-last">
					<xsl:with-param name="input" select="$temp"/>
					<xsl:with-param name="substr" select="$substr"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$temp"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--Create the entity name from the graphic file name-->
	<xsl:template name="get-entity-name">
		<xsl:param name="boardno"/>
		<xsl:variable name="file">
			<xsl:call-template name="get-file-name">
				<xsl:with-param name="boardno" select="$boardno"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:call-template name="substring-before-last">
			<xsl:with-param name="input" select="$file"/>
			<xsl:with-param name="substr" select="'.'"/>
		</xsl:call-template>
	</xsl:template>

	<!--Remove the path from the graphic file name-->
	<xsl:template name="get-file-name">
		<xsl:param name="boardno"/>
		<xsl:choose>
			<xsl:when test="contains($boardno, '/')">
				<xsl:call-template name="substring-after-last">
					<xsl:with-param name="input" select="$boardno"/>
					<xsl:with-param name="substr" select="'/'"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="contains($boardno, '\')">
				<xsl:call-template name="substring-after-last">
					<xsl:with-param name="input" select="$boardno"/>
					<xsl:with-param name="substr" select="'\'"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$boardno"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet><!-- Stylus Studio meta-information - (c) 2004-2006. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="..\apps" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->