<?xml version="1.0" encoding="UTF-8" ?>
<!-- postprocess -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="no" omit-xml-declaration="no"/>

	<!--	All this does is traverse a string of numbers and emit them
			as a sequence of elements with a single attribute	-->
	<xsl:template name="emitNumbers">
		<xsl:param name="NumberString"/>
		<xsl:variable name="DelimiterFound" select="contains($NumberString, ',')"/>
		<xsl:variable name="NumberValue">
			<xsl:choose>
				<xsl:when test="$DelimiterFound">
					<xsl:value-of select="number(substring-before($NumberString, ','))"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="number($NumberString)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="ElementName">
			<xsl:choose>
				<xsl:when test="floor($NumberValue) = $NumberValue">INT</xsl:when>
				<xsl:otherwise>FIXED</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="{$ElementName}">
			<xsl:attribute name="VAL">
				<xsl:value-of select="$NumberValue"/>
			</xsl:attribute>
		</xsl:element>
		<xsl:if test="$DelimiterFound">
			<xsl:call-template name="emitNumbers">
				<xsl:with-param name="NumberString"><xsl:value-of select="substring-after($NumberString, ',')"/></xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!--	All this does is traverse a string of filters and emit them
			as a sequence of elements with a single attribute	-->
	<xsl:template name="emitFilters">
		<xsl:param name="FilterList"/>
		<xsl:variable name="DelimiterFound" select="contains($FilterList, ',')"/>
		<xsl:variable name="FilterValue">
			<xsl:choose>
				<xsl:when test="$DelimiterFound">
					<xsl:value-of select="substring-before($FilterList, ',')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$FilterList"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="NAME">
			<xsl:attribute name="VAL">
				<xsl:value-of select="$FilterValue"/>
			</xsl:attribute>
		</xsl:element>
		<xsl:if test="$DelimiterFound">
			<xsl:call-template name="emitFilters">
				<xsl:with-param name="FilterList">
					<xsl:value-of select="substring-after($FilterList, ',')"/>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- convert a hex digit into a decimal value -->
	<xsl:template name="convertHexDigit">
		<xsl:param name="HexChar"/>
		<xsl:choose>
			<xsl:when test="$HexChar='A'">10</xsl:when>
			<xsl:when test="$HexChar='B'">11</xsl:when>
			<xsl:when test="$HexChar='C'">12</xsl:when>
			<xsl:when test="$HexChar='D'">13</xsl:when>
			<xsl:when test="$HexChar='E'">14</xsl:when>
			<xsl:when test="$HexChar='F'">15</xsl:when>
			<xsl:otherwise><xsl:value-of select="$HexChar"/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- generate a sequence of FIXED/INT nodes for each RGB value -->
	<xsl:template name="convertHexColorToFloat">
		<xsl:param name="HexString"/>
		<xsl:if test="$HexString != ''">
			<xsl:variable name="firstdigit">
				<xsl:call-template name="convertHexDigit">
					<xsl:with-param name="HexChar"><xsl:value-of select="substring($HexString, 1, 1)"/></xsl:with-param>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="seconddigit">
				<xsl:call-template name="convertHexDigit">
					<xsl:with-param name="HexChar"><xsl:value-of select="substring($HexString, 2, 1)"/></xsl:with-param>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="NumberValue">

				<xsl:value-of select="(number($firstdigit) * 16 + number($seconddigit)) div 255"/>
			</xsl:variable>
			<xsl:variable name="ElementName">
				<xsl:choose>
					<xsl:when test="floor($NumberValue) = $NumberValue">INT</xsl:when>
					<xsl:otherwise>FIXED</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:element name="{$ElementName}">
				<xsl:attribute name="VAL">
					<xsl:choose>
						<xsl:when test="$ElementName = 'FIXED'">
							<!-- only put out color to six decimal places -->
							<xsl:value-of select="format-number($NumberValue, '0.0#####')"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$NumberValue"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:element>
			<xsl:call-template name="convertHexColorToFloat">
				<xsl:with-param name="HexString">
					<xsl:value-of select="substring($HexString, 3)"/>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<!-- map the flags to their corresponding integer values -->
	<xsl:template name="mapFlags">
		<xsl:param name="FlagValue"/>
		<xsl:choose>
			<xsl:when test="$FlagValue = 'invisible'">1</xsl:when>
			<xsl:when test="$FlagValue = 'hidden'">2</xsl:when>
			<xsl:when test="$FlagValue = 'print'">4</xsl:when>
			<xsl:when test="$FlagValue = 'nozoom'">8</xsl:when>
			<xsl:when test="$FlagValue = 'norotate'">16</xsl:when>
			<xsl:when test="$FlagValue = 'noview'">32</xsl:when>
			<xsl:when test="$FlagValue = 'readonly'">64</xsl:when>
			<xsl:when test="$FlagValue = 'locked'">128</xsl:when>
			<xsl:when test="$FlagValue = 'togglenoview'">256</xsl:when>
		</xsl:choose>
	</xsl:template>

	<!--	convert the string values of the flags
			attribute into a bitfield value -->
	<xsl:template name="convertFlags">
		<xsl:param name="FlagString"/>
		<xsl:variable name="DelimiterFound" select="contains($FlagString, ',')"/>
		<xsl:variable name="FlagValue">
			<xsl:choose>
				<xsl:when test="$DelimiterFound">
					<xsl:value-of select="substring-before($FlagString, ',')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$FlagString"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="CurrentValue">
			<xsl:call-template name="mapFlags">
				<xsl:with-param name="FlagValue"><xsl:value-of select="$FlagValue"/></xsl:with-param>
			</xsl:call-template>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="$DelimiterFound">
				<!-- recurse on the rest of the flags,
					 summing the result on return to get the
					 bitfield values -->
				<xsl:variable name="RestValue">
					<xsl:call-template name="convertFlags">
						<xsl:with-param name="FlagString" select="substring-after($FlagString, ',')"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:value-of select="number($CurrentValue) + $RestValue"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$CurrentValue"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--	dump the data node into the stream! -->
	<xsl:template name="emitData">
		<xsl:param name="StreamData"/>

		<xsl:for-each select="$StreamData">
			<xsl:element name="DATA">
				<xsl:attribute name="MODE">
					<xsl:value-of select="translate (@mode, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
				</xsl:attribute>
				<xsl:attribute name="ENCODING">
					<xsl:value-of select="translate (@encoding, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
				</xsl:attribute>
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="emitStream">
		<xsl:param name="StreamData"/>
		<xsl:param name="FilterData"/>
		<xsl:param name="AnnotationType"/>

		<xsl:choose>
			<xsl:when test="$AnnotationType = 'sound'">
				<xsl:element name="NAME">
					<xsl:attribute name="KEY">Type</xsl:attribute>
					<xsl:attribute name="VAL">Sound</xsl:attribute>
				</xsl:element>
				<xsl:element name="STREAM">
					<xsl:attribute name="KEY">Sound</xsl:attribute>
					<!--	fill in the content of the
							annotation define id later -->
					<xsl:attribute name="DEFINE"/>
					<xsl:apply-templates select="@length|../@encoding|../@rate|../@bits|../@channels|$FilterData"/>
					<xsl:call-template name="emitData">
						<xsl:with-param name="StreamData" select="$StreamData"/>
					</xsl:call-template>
				</xsl:element>
			</xsl:when>

			<xsl:when test="$AnnotationType = 'fileattachment'">
				<xsl:choose>
					<xsl:when test="name() = 'data'">
						<xsl:element name="DICT">
							<xsl:attribute name="KEY">FS</xsl:attribute>
							<xsl:apply-templates select="../@file"/>
							<xsl:element name="NAME">
								<xsl:attribute name="KEY">Type</xsl:attribute>
								<xsl:attribute name="VAL">Filespec</xsl:attribute>
							</xsl:element>
							<xsl:element name="DICT">
								<xsl:attribute name="KEY">EF</xsl:attribute>
								<xsl:element name="STREAM">
									<xsl:attribute name="KEY">F</xsl:attribute>
									<!--	fill in the content of the
											annotation define id later -->
									<xsl:attribute name="DEFINE"/>
									<xsl:apply-templates select="@length|../@mimetype|$FilterData"/>
									<xsl:for-each select="..">
										<xsl:if test="@creation|@modification|@checksum|@size|*[name()='resource']">
											<xsl:element name="DICT">
												<xsl:attribute name="KEY">Params</xsl:attribute>
												<!-- using resource here will cause recursion -->
												<xsl:apply-templates select="@creation|@modification|@checksum|@size|*[name()='resource']"/>
											</xsl:element>
										</xsl:if>
									</xsl:for-each>
									<xsl:call-template name="emitData">
										<xsl:with-param name="StreamData" select="$StreamData"/>
									</xsl:call-template>
								</xsl:element>
							</xsl:element>
						</xsl:element>
					</xsl:when>

					<xsl:when test="name() = 'resource'">
						<xsl:element name="DICT">
							<xsl:attribute name="KEY">Mac</xsl:attribute>
							<xsl:apply-templates select="@subtype|@creator"/>
							<xsl:element name="STREAM">
								<xsl:attribute name="KEY">ResFork</xsl:attribute>
								<!--	fill in the content of the
										annotation define id later -->
								<xsl:attribute name="DEFINE"/>
									<xsl:apply-templates select="@length|$FilterData"/>
									<xsl:call-template name="emitData">
										<xsl:with-param name="StreamData" select="$StreamData"/>
									</xsl:call-template>
							</xsl:element>
						</xsl:element>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="/">
		<xsl:element name="PDF">
			<xsl:element name="CATALOG">
				<xsl:element name="DICT">
					<xsl:attribute name="DEFINE">1/0</xsl:attribute>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:element>
			<!-- just dropped this in -->
			<INFO><NULL/></INFO>
		</xsl:element>
	</xsl:template>
  		
	<xsl:template match="*">
		<xsl:choose>
			<xsl:when test="name() = 'xfdf'">
				<xsl:element name="DICT">
					<xsl:attribute name="KEY">FDF</xsl:attribute>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'appearance'">
				<xsl:element name="APPEARANCE">
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:when>

			<!-- beginning of field handling -->

			<xsl:when test="name() = 'fields'">
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">Fields</xsl:attribute>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'field'">
				<xsl:element name="DICT">
					<xsl:element name="STRING">
						<xsl:attribute name="KEY">T</xsl:attribute>
						<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
						<xsl:value-of select="@name"/>
					</xsl:element>
					<xsl:choose>
						<!--	for some reason, the commented out
								test doesn't work. the following one
								does - they should be equivalent -->
						<!--<xsl:when test="field">-->
						<xsl:when test="*[name() = 'field']">
							<xsl:element name="ARRAY">
								<xsl:attribute name="KEY">Kids</xsl:attribute>
								<xsl:apply-templates/>
							</xsl:element>
						</xsl:when>
						<xsl:when test="count(*[name() = 'value']) > 1">
							<xsl:element name="ARRAY">
								<xsl:attribute name="KEY">V</xsl:attribute>
								<xsl:apply-templates/>
							</xsl:element>
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'value'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">V</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'value-richtext'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">RV</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<!-- end of field handling -->

			<!-- beginning of annotation handling -->
			<xsl:when test="name() = 'annots'">
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">Annots</xsl:attribute>
					<xsl:for-each select="*">
						<xsl:apply-templates select="."/>
						<xsl:for-each select="*[name() = 'popup']">
							<!--	fill in the content of the
									popup reference later -->
							<xsl:element name="DICT">
								<xsl:attribute name="REF"/>
							</xsl:element>
						</xsl:for-each>
					</xsl:for-each>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'f'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">F</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="@href"/>
				</xsl:element>
				<xsl:if test="not(../*[name()='ids'])">
					<!-- just dropped this in -->
					<ARRAY KEY="ID"><STRING/><STRING/></ARRAY>
				</xsl:if>
			</xsl:when>

			<xsl:when test="name() = 'ids'">
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">ID</xsl:attribute>
					<xsl:element name="STRING">
						<xsl:attribute name="ENCODING">HEX</xsl:attribute>
						<xsl:value-of select="@original"/>
					</xsl:element>
					<xsl:element name="STRING">
						<xsl:attribute name="ENCODING">HEX</xsl:attribute>
						<xsl:value-of select="@modified"/>
					</xsl:element>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'inklist'">
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">InkList</xsl:attribute>
					<!--	don't process gesture by recursion here as
							this could blow the stack -->
					<xsl:for-each select="*">
						<xsl:element name="gesture">
							<xsl:value-of select="."/>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>
			</xsl:when>

			<!--	special case for freetext (on a round trip,
					this will appear as an attribute of the annotation,
					to be consistent with other annotations -->
			<xsl:when test="name() = 'border'">
				<!--	this array is used by annotations
						using a legacy layout of border
						styles -->
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">Border</xsl:attribute>
					<xsl:element name="INT">
						<xsl:attribute name="VAL">0</xsl:attribute>
					</xsl:element>
					<xsl:element name="INT">
						<xsl:attribute name="VAL">0</xsl:attribute>
					</xsl:element>
					<xsl:element name="INT">
						<xsl:attribute name="VAL">
							<xsl:value-of select="."/>
						</xsl:attribute>
					</xsl:element>
				</xsl:element>
				<xsl:element name="DICT">
					<xsl:attribute name="KEY">BS</xsl:attribute>
					<xsl:element name="INT">
						<xsl:attribute name="KEY">W</xsl:attribute>
						<xsl:attribute name="VAL">
							<xsl:value-of select="@width"/>
						</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'vertices'">
				<!--	don't process vertices by recursion here as
						this could blow the stack -->
				<xsl:element name="vertices">
					<xsl:value-of select="translate(current(), ';', ',')"/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'contents-richtext'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">RC</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'contents'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">Contents</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'defaultappearance'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">DA</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="name() = 'defaultstyle'">
				<xsl:element name="STRING">
					<xsl:attribute name="KEY">DS</xsl:attribute>
					<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:when>

			<!-- data from sound, attachments -->
			<xsl:when test="name() = 'data' or name() = 'resource'">
				<xsl:call-template name="emitStream">
					<xsl:with-param name="StreamData" select="."/>
					<xsl:with-param name="FilterData" select="@filter"/>
					<xsl:with-param name="AnnotationType" select="name(..)"/>
				</xsl:call-template>
			</xsl:when>

			<xsl:otherwise>
				<xsl:variable name="ParentNotAnnots" select="name(..) != 'annots'"/>
				<xsl:variable name="ElementName">
					<xsl:choose>
						<!--	most annotation names only need
								the first character of the name
								converted to upper case -->
						<!--	put in exceptions to rule here -->
						<xsl:when test="name() = 'strikeout'">
							<xsl:text>StrikeOut</xsl:text>
						</xsl:when>
						<xsl:when test="name() = 'fileattachment'">
							<xsl:text>FileAttachment</xsl:text>
						</xsl:when>
						<xsl:when test="name() = 'freetext'">
							<xsl:text>FreeText</xsl:text>
						</xsl:when>
						<xsl:when test="name() = 'polyline'">
							<xsl:text>PolyLine</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:variable name="StartAnnotChar">
								<xsl:value-of select="translate (substring(name(), 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
							</xsl:variable>
							<xsl:value-of select="concat($StartAnnotChar, substring(name(), 2))"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:element name="DICT">
					<xsl:if test="$ParentNotAnnots">
						<xsl:attribute name="KEY">
							<xsl:value-of select="$ElementName"/>
						</xsl:attribute>
					</xsl:if>
					<!--	fill in the content of the
							annotation define id later -->
					<xsl:attribute name="DEFINE"/>
					<xsl:element name="NAME">
						<xsl:attribute name="KEY">Type</xsl:attribute>
						<xsl:attribute name="VAL">Annot</xsl:attribute>
					</xsl:element>
					<xsl:element name="NAME">
						<xsl:attribute name="KEY">Subtype</xsl:attribute>
						<xsl:attribute name="VAL">
							<xsl:value-of select="$ElementName"/>
						</xsl:attribute>
					</xsl:element>
					<xsl:if test="$ParentNotAnnots">
						<xsl:element name="DICT">
							<xsl:attribute name="KEY">Parent</xsl:attribute>
							<!--	fill in the content of the
									parent reference later -->
							<xsl:attribute name="REF"/>
						</xsl:element>
					</xsl:if>

					<!--	handle width, style, intensity, dashes
							start, end, head, tail attributes separately
							from other attributes and nodes, as they
							need to be wrapped within other nodes -->
					<xsl:variable name="BorderEffect" select="@intensity|@style[. = 'cloudy']"/>
					<xsl:variable name="BorderStyle" select="@width|@dashes|@style[not(. = 'cloudy')]"/>
					<xsl:if test="string($BorderEffect) != ''">
						<xsl:element name="DICT">
							<xsl:attribute name="KEY">BE</xsl:attribute>
							<xsl:apply-templates select="$BorderEffect"/>
						</xsl:element>
					</xsl:if>
					<xsl:if test="string($BorderStyle) != ''">
						<!--	this array is used by annotations
								using a legacy layout of border
								styles -->
						<xsl:element name="ARRAY">
							<xsl:attribute name="KEY">Border</xsl:attribute>
							<xsl:call-template name="emitNumbers">
								<xsl:with-param name="NumberString">
									<!-- use radii of zero -->
									<xsl:text>0,0,</xsl:text>
									<xsl:choose>
										<xsl:when test="@width">
											<xsl:value-of select="@width"/>
										</xsl:when>
										<!-- default width of one -->
										<xsl:otherwise>1</xsl:otherwise>
									</xsl:choose>
									<xsl:if test="@dashes">
										<xsl:text>,</xsl:text>
										<xsl:value-of select="@dashes"/>
									</xsl:if>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:element>
						<xsl:element name="DICT">
							<xsl:attribute name="KEY">BS</xsl:attribute>
							<xsl:apply-templates select="$BorderStyle"/>
						</xsl:element>
					</xsl:if>
					<xsl:if test="@start|@end">
						<xsl:element name="ARRAY">
							<xsl:attribute name="KEY">L</xsl:attribute>
							<xsl:call-template name="emitNumbers">
								<xsl:with-param name="NumberString">
									<xsl:value-of select="@start"/>
									<xsl:text>,</xsl:text>
									<xsl:value-of select="@end"/>
							</xsl:with-param>
						</xsl:call-template>
						</xsl:element>
					</xsl:if>
					<xsl:if test="@head|@tail">
						<xsl:element name="ARRAY">
							<xsl:attribute name="KEY">LE</xsl:attribute>
							<!--	naming @head, @tail explicitly
									ensures correct order -->
							<xsl:element name="NAME">
								<xsl:attribute name="VAL">
									<xsl:value-of select="@head"/>
								</xsl:attribute>
							</xsl:element>
							<xsl:element name="NAME">
								<xsl:attribute name="VAL">
									<xsl:value-of select="@tail"/>
								</xsl:attribute>
							</xsl:element>
						</xsl:element>
					</xsl:if>
					<xsl:apply-templates select="*[not(name()='resource')]|@*[not(
								name() = 'width' or
								name() = 'style' or
								name() = 'dashes' or
								name() = 'intensity' or
								name() = 'start' or
								name() = 'end' or
								name() = 'head' or
								name() = 'tail' or
								name() = 'file' or
								name() = 'length' or
								name() = 'encoding' or
								name() = 'rate' or
								name() = 'bits' or
								name() = 'channels' or
								name() = 'creation' or
								name() = 'modification' or
								name() = 'checksum' or
								name() = 'size' or
								name() = 'mimetype')]"/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
  		
	<xsl:template match="@rotation">
		<xsl:variable name="ElementName">
			<xsl:choose>
				<xsl:when test="floor(number(.)) = number(.)">INT</xsl:when>
				<xsl:otherwise>FIXED</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="{$ElementName}">
			<xsl:attribute name="KEY">Rotate</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@rect">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">Rect</xsl:attribute>
			<xsl:call-template name="emitNumbers">
				<xsl:with-param name="NumberString" select="."/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@inlinetextrect">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">InlineTextRect</xsl:attribute>
			<xsl:call-template name="emitNumbers">
				<xsl:with-param name="NumberString" select="."/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@caretrect">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">CaretRect</xsl:attribute>
			<xsl:call-template name="emitNumbers">
				<xsl:with-param name="NumberString" select="."/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@fringe">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">RD</xsl:attribute>
			<xsl:call-template name="emitNumbers">
				<xsl:with-param name="NumberString" select="."/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@color">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">C</xsl:attribute>
			<xsl:call-template name="convertHexColorToFloat">
				<xsl:with-param name="HexString" select="substring(current(), 2)"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@interior-color">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">IC</xsl:attribute>
			<xsl:call-template name="convertHexColorToFloat">
				<xsl:with-param name="HexString" select="substring(current(), 2)"/>
			</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@page">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Page</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@encoding">
		<xsl:element name="NAME">
			<xsl:attribute name="KEY">E</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:choose>
					<xsl:when test="current() = 'raw'">Raw</xsl:when>
					<xsl:when test="current() = 'signed'">Signed</xsl:when>
					<xsl:when test="current() = 'mulaw'">muLaw</xsl:when>
					<xsl:when test="current() = 'alaw'">ALaw</xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@bits">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">B</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@rate">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">R</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@channels">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">C</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@size">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Size</xsl:attribute>
			<xsl:attribute name="VAL"><xsl:value-of select="."/></xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@length">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Length</xsl:attribute>
			<xsl:attribute name="VAL"><xsl:value-of select="."/></xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@flags">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">F</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:call-template name="convertFlags">
					<xsl:with-param name="FlagString" select="."/>
				</xsl:call-template>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@modification">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">ModDate</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@date">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">M</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@creationdate|@creation">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">CreationDate</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@title">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">T</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@name">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">NM</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@inreplyto">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">IRT</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@width">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">W</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@style">
		<xsl:element name="NAME">
			<xsl:attribute name="KEY">S</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:choose>
					<xsl:when test="current() = 'solid'">S</xsl:when>
					<xsl:when test="current() = 'dash'">D</xsl:when>
					<xsl:when test="current() = 'bevelled'">B</xsl:when>
					<xsl:when test="current() = 'inset'">I</xsl:when>
					<xsl:when test="current() = 'underline'">U</xsl:when>
					<xsl:when test="current() = 'cloudy'">C</xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@dashes">
		<xsl:element name="ARRAY">
			<xsl:attribute name="KEY">D</xsl:attribute>
				<xsl:call-template name="emitNumbers">
					<xsl:with-param name="NumberString">
						<xsl:value-of select="."/>
					</xsl:with-param>
				</xsl:call-template>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@intensity">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">I</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@subject">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">Subj</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@icon">
		<xsl:element name="NAME">
			<xsl:attribute name="KEY">Name</xsl:attribute>
			<xsl:attribute name="VAL"><xsl:value-of select="."/></xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@open">
		<xsl:element name="BOOL">
			<xsl:attribute name="KEY">Open</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:choose>
					<xsl:when test="current()='yes'">true</xsl:when>
					<xsl:otherwise>false</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@opacity">
		<xsl:element name="FIXED">
			<xsl:attribute name="KEY">CA</xsl:attribute>
			<xsl:attribute name="VAL"><xsl:value-of select="."/></xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@justification">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Q</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:choose>
					<xsl:when test="current() = 'left'">0</xsl:when>
					<xsl:when test="current() = 'centered'">1</xsl:when>
					<xsl:when test="current() = 'right'">2</xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@coords">
		<!--	don't process coords by recursion here as
				this could blow the stack -->
		<xsl:element name="quadpoints">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@state">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">State</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@statemodel">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">StateModel</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@checksum">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">CheckSum</xsl:attribute>
			<xsl:attribute name="ENCODING">HEX</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@mimetype">
		<xsl:element name="NAME">
			<xsl:attribute name="KEY">Subtype</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@file">
		<xsl:element name="STRING">
			<xsl:attribute name="KEY">F</xsl:attribute>
			<xsl:attribute name="ENCODING">ASCII</xsl:attribute>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@symbol">
		<xsl:element name="NAME">
			<xsl:attribute name="KEY">Sy</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:choose>
					<xsl:when test=". = 'none'"></xsl:when>
					<xsl:when test=". = 'paragraph'">P</xsl:when>
					<xsl:when test=". = 'space'">S</xsl:when>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@filter">
		<xsl:choose>

			<!-- more than one filter -->
			<xsl:when test="contains(., ',')">
				<xsl:element name="ARRAY">
					<xsl:attribute name="KEY">Filter</xsl:attribute>
					<xsl:call-template name="emitFilters">
						<xsl:with-param name="FilterList">
							<xsl:value-of select="."/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:element>
			</xsl:when>

			<!-- just one filter -->
			<xsl:otherwise>
				<xsl:element name="NAME">
					<xsl:attribute name="KEY">Filter</xsl:attribute>
					<xsl:attribute name="VAL">
						<xsl:value-of select="."/>
					</xsl:attribute>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- Mac specific attributes of a file attachment -->

	<xsl:template match="@subtype">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Subtype</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="@creator">
		<xsl:element name="INT">
			<xsl:attribute name="KEY">Creator</xsl:attribute>
			<xsl:attribute name="VAL">
				<xsl:value-of select="."/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<!-- end of annotation handling -->

	<!-- get rid of any extraneous text nodes and attributes -->
	<xsl:template match="text()|@*"/>

</xsl:stylesheet>
