<?xml version="1.0" encoding="UTF-8" ?>
<!-- preprocess -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://ns.adobe.com/xfdf/">
<xsl:output method="xml" encoding="UTF-8" indent="no" omit-xml-declaration="no"/>

	<xsl:variable name="HexArray">0123456789ABCDEF</xsl:variable>

	<!--	Convert color value from a floating number between
			zero and one to a hexadecimal value between 00 and FF -->
	<xsl:template name="convertColor">
		<xsl:param name="Color"/>
		<xsl:variable name="ColorAsInt"><xsl:value-of select="number($Color)"/></xsl:variable>
		<xsl:choose>
			<xsl:when test="$ColorAsInt = 0">
				<xsl:text>00</xsl:text>
			</xsl:when>
			<xsl:when test="$ColorAsInt = 1">
				<xsl:text>FF</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="RangedColor"><xsl:value-of select="round($ColorAsInt * 255)"/></xsl:variable>
				<xsl:variable name="MSDigit"><xsl:value-of select="floor($RangedColor div 16)"/></xsl:variable>
				<xsl:variable name="LSDigit"><xsl:value-of select="$RangedColor mod 16"/></xsl:variable>
				<xsl:value-of select="substring ($HexArray, $MSDigit + 1, 1)"/><xsl:value-of select="substring ($HexArray, $LSDigit + 1, 1)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--	Put out the parameters associated with a stream generic to
			the annotation -->
	<xsl:template name="emitParams">
		<xsl:apply-templates select="../*[not(self::DICT[@KEY='Mac'] or self::DATA or self::NAME[@KEY='Filter'] or self::ARRAY[@KEY='Filter'] or self::INT[@KEY='Length'])]"/>
	</xsl:template>

	<!--	Put out the stream parameters specific to
			the data of the annotation -->
	<xsl:template name="emitRestofParams">
		<xsl:apply-templates select="../INT[@KEY='Length']|../../INT[@KEY='Subtype' or @KEY='Creator']"/>
		<xsl:attribute name="filter">
			<xsl:for-each select="../NAME[@KEY='Filter']/@VAL|../ARRAY[@KEY='Filter']/NAME/@VAL">
				<xsl:value-of select="."/>
				<xsl:if test="position() != last()">,</xsl:if>
			</xsl:for-each>
		</xsl:attribute>
	</xsl:template>

	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
  		
	<xsl:template match="APPEARANCE">
		<xsl:element name="appearance">
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="ARRAY[@KEY]">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>

		<xsl:choose>

			<!--	ID values (not annotations or fields, but should
					be round tripped, to facilitate finding a PDF) -->
			<xsl:when test="$keyvalue = 'ID'">
				<xsl:element name="ids">
					<xsl:attribute name="original">
						<xsl:value-of select="STRING[1]"/>
					</xsl:attribute>
					<xsl:attribute name="modified">
						<xsl:value-of select="STRING[2]"/>
					</xsl:attribute>
				</xsl:element>
			</xsl:when>

			<!-- Start of the annotations -->
			<xsl:when test="$keyvalue = 'Annots'">
				<xsl:element name="annots">
					<xsl:apply-templates select="DICT[@DEFINE]"/>
				</xsl:element>
			</xsl:when>

			<!-- Start of the fields -->
			<xsl:when test="$keyvalue = 'Fields'">
				<xsl:element name="fields">
					<xsl:apply-templates select="DICT[not(@*)]"/>
				</xsl:element>
			</xsl:when>

			<!-- Line coordinates -->
			<xsl:when test="$keyvalue = 'L'">
				<xsl:attribute name="start">
					<xsl:value-of select="(FIXED|INT)[1]/@VAL"/>
					<xsl:text>,</xsl:text>
					<xsl:value-of select="(FIXED|INT)[2]/@VAL"/>
				</xsl:attribute>
				<xsl:attribute name="end">
					<xsl:value-of select="(FIXED|INT)[3]/@VAL"/>
					<xsl:text>,</xsl:text>
					<xsl:value-of select="(FIXED|INT)[4]/@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Line end style -->
			<xsl:when test="$keyvalue = 'LE'">
				<xsl:attribute name="head">
					<xsl:value-of select="NAME[1]/@VAL"/>
				</xsl:attribute>
				<xsl:attribute name="tail">
					<xsl:value-of select="NAME[2]/@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Rectangle coordinates -->
			<xsl:when test="$keyvalue = 'Rect'">
				<xsl:attribute name="rect">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="format-number(@VAL,'0.000000')"/><xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!-- Inline Text Rectangle coordinates -->
			<xsl:when test="$keyvalue = 'InlineTextRect'">
				<xsl:attribute name="inlinetextrect">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="format-number(@VAL,'0.000000')"/><xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!-- Caret Rectangle coordinates -->
			<xsl:when test="$keyvalue = 'CaretRect'">
				<xsl:attribute name="caretrect">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="format-number(@VAL,'0.000000')"/><xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!--	fringe rectangle (around circles and
					squares with cloud border effect) -->
			<xsl:when test="$keyvalue = 'RD'">
				<xsl:attribute name="fringe">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="format-number(@VAL,'0.000000')"/><xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!-- inklist -->
			<xsl:when test="$keyvalue = 'InkList'">
				<xsl:element name="inklist">
					<xsl:for-each select="ARRAY">
						<xsl:element name="gesture">
							<xsl:for-each select="FIXED|INT">
								<xsl:value-of select="@VAL"/>
								<xsl:choose>
									<xsl:when test="position() = last()"/>
									<xsl:when test="position() mod 2 = 1">,</xsl:when>
									<xsl:otherwise>;</xsl:otherwise>
								</xsl:choose>
							</xsl:for-each>
						</xsl:element>
					</xsl:for-each>
				</xsl:element>
			</xsl:when>

			<!-- polygon vertices -->
			<xsl:when test="$keyvalue = 'Vertices' or $keyvalue = 'vertices'">
				<xsl:element name="vertices">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="@VAL"/>
						<xsl:choose>
							<xsl:when test="position() = last()"/>
							<xsl:when test="position() mod 2 = 1">,</xsl:when>
							<xsl:otherwise>;</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:element>
			</xsl:when>

			<!-- Border Dashes -->
			<xsl:when test="$keyvalue = 'D'">
				<xsl:variable name="Dashes">
					<xsl:for-each select="(FIXED|INT)/@VAL">
						<xsl:value-of select="."/><xsl:text>,</xsl:text>
					</xsl:for-each>
				</xsl:variable>
				<!-- trim off last comma -->
				<xsl:attribute name="dashes">
					<xsl:value-of select="substring($Dashes, 1, string-length($Dashes) - 1)"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Border -->
			<xsl:when test="$keyvalue = 'Border'">
				<!--	only use the Border array if no
						border style settings are available -->
				<xsl:if test="not(../DICT[@KEY='BS'])">
					<xsl:variable name="DashesPresent" select="count(FIXED|INT) > 3"/>
					<xsl:attribute name="style">
						<xsl:choose>
							<xsl:when test="$DashesPresent">
								<xsl:text>dash</xsl:text>
							</xsl:when>
							<xsl:otherwise>
								<xsl:text>solid</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
					<xsl:attribute name="width">
						<xsl:value-of select="(FIXED|INT)[3]/@VAL"/>
					</xsl:attribute>
					<xsl:if test="$DashesPresent">
						<xsl:attribute name="dashes">
							<xsl:for-each select="(FIXED|INT)[position() > 3]">
								<xsl:value-of select="@VAL"/>
								<xsl:if test="position() != last()">
									<xsl:text>,</xsl:text>
								</xsl:if>
							</xsl:for-each>
						</xsl:attribute>
					</xsl:if>
				</xsl:if>
			</xsl:when>

			<!-- Color -->
			<xsl:when test="$keyvalue = 'C'">
				<xsl:attribute name="color">
					<xsl:text>#</xsl:text>
					<xsl:for-each select="FIXED|INT">
						<xsl:call-template name="convertColor">
							<xsl:with-param name="Color" select="@VAL"/>
						</xsl:call-template>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!-- Interior Color -->
			<!--	trivial case: don't put out interior-color
					if there are no color values -->
			<xsl:when test="$keyvalue = 'IC' and (FIXED|INT)">
				<xsl:attribute name="interior-color">
					<xsl:text>#</xsl:text>
					<xsl:for-each select="FIXED|INT">
						<xsl:call-template name="convertColor">
							<xsl:with-param name="Color" select="@VAL"/>
						</xsl:call-template>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

			<!-- QuadPoints -->
			<xsl:when test="$keyvalue = 'QuadPoints'">
				<xsl:attribute name="coords">
					<xsl:for-each select="FIXED|INT">
						<xsl:value-of select="@VAL"/>
						<xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each>
				</xsl:attribute>
			</xsl:when>

		</xsl:choose>
	</xsl:template>

	<xsl:template match="BOOL[@KEY = 'Open']">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>
		<xsl:attribute name="open">
			<xsl:choose>
				<xsl:when test="@VAL='true'">yes</xsl:when>
				<xsl:otherwise>no</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
	</xsl:template>

	<!--	rather than check the ancestor to see if we are handling
			a field or an annot, just use a different mode for fields -->
	<xsl:template match="STRING[@KEY = 'T']" mode="fields">
		<xsl:element name="field">
			<xsl:attribute name="name">
				<xsl:value-of select="text()"/>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="../ARRAY[@KEY = 'Kids']">
					<xsl:apply-templates select="../ARRAY[@KEY = 'Kids']/DICT"/>
				</xsl:when>
				<xsl:when test="../ARRAY[@KEY = 'V']">
					<xsl:for-each select="../ARRAY[@KEY = 'V']/STRING">
						<xsl:element name="value">
							<xsl:value-of select="text()"/>
						</xsl:element>
					</xsl:for-each>
				</xsl:when>
				<!--	ordering here is significant: 
						when both <STRING KEY="V"> and
						<STRING KEY="RV"> are present, use
						<STRING KEY="RV"> -->
				<xsl:when test="../STRING[@KEY = 'RV']">
					<xsl:element name="value-richtext">
						<xsl:value-of select="../STRING[@KEY = 'RV']/text()"/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="../STRING[@KEY = 'V']">
					<xsl:element name="value">
						<xsl:value-of select="../STRING[@KEY = 'V']/text()"/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="../NAME[@KEY = 'V']">
					<xsl:element name="value">
						<xsl:value-of select="../NAME[@KEY = 'V']/@VAL"/>
					</xsl:element>
				</xsl:when>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<!-- get rid of any extraneous text nodes and attributes -->
	<xsl:template match="text()|@*" mode="fields"/>

	<xsl:template match="STRING[@KEY]">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>
		<xsl:choose>

			<!-- File href -->
			<xsl:when test="$keyvalue = 'F'">
				<xsl:choose>
					<xsl:when test="../@KEY = 'FDF'">
						<xsl:element name="f">
							<xsl:attribute name="href">
								<xsl:value-of select="text()"/>
							</xsl:attribute>
						</xsl:element>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="file">
							<xsl:value-of select="text()"/>
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>

			<!-- start Stream Parameters -->

			<!-- Creation Date -->
			<xsl:when test="$keyvalue = 'CreationDate'">
				<xsl:choose>
					<xsl:when test="ancestor::STREAM">
						<xsl:attribute name="creation">
							<xsl:value-of select="text()"/>
						</xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="creationdate">
							<xsl:value-of select="text()"/>
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>

			<!-- File Modification Date -->
			<xsl:when test="$keyvalue = 'ModDate'">
				<xsl:choose>
					<xsl:when test="ancestor::STREAM">
						<xsl:attribute name="modification">
							<xsl:value-of select="text()"/>
						</xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="date">
							<xsl:value-of select="text()"/>
						</xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>

			<!-- File Checksum -->
			<xsl:when test="$keyvalue = 'CheckSum'">
				<xsl:attribute name="checksum">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- end STREAM PARAMS -->

			<!-- Name Identifier -->
			<xsl:when test="$keyvalue = 'NM'">
				<xsl:attribute name="name">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- In reply to Identifier -->
			<xsl:when test="$keyvalue = 'IRT'">
				<xsl:attribute name="inreplyto">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Subject -->
			<xsl:when test="$keyvalue = 'Subj'">
				<xsl:attribute name="subject">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Title -->
			<xsl:when test="$keyvalue = 'T'">
				<xsl:attribute name="title">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Modification Date -->
			<xsl:when test="$keyvalue = 'M'">
				<xsl:attribute name="date">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Contents -->
			<xsl:when test="$keyvalue = 'Contents'">
				<xsl:element name="contents">
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<!-- Rich Text Contents -->
			<xsl:when test="$keyvalue = 'RC'">
				<xsl:element name="contents-richtext">
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="$keyvalue = 'State'">
				<xsl:attribute name="state">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<xsl:when test="$keyvalue = 'StateModel'">
				<xsl:attribute name="statemodel">
					<xsl:value-of select="text()"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Default appearance -->
			<xsl:when test="$keyvalue = 'DA'">
				<xsl:element name="defaultappearance">
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

			<!-- Default style -->
			<xsl:when test="$keyvalue = 'DS'">
				<xsl:element name="defaultstyle">
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:when>

		</xsl:choose>
	</xsl:template>

	<xsl:template match="INT[@KEY]">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>
		<xsl:choose>

			<!-- Rotation -->
			<xsl:when test="$keyvalue = 'Rotate'">
				<xsl:attribute name="rotation">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Flags -->
			<xsl:when test="$keyvalue = 'F'">
				<xsl:variable name="BitValue" select="number(@VAL)"/>
				<!-- test individual bits -->
				<xsl:variable name="BitString">
					<xsl:if test="($BitValue mod 2)	>= 1">invisible,</xsl:if>
					<xsl:if test="($BitValue mod 4) >= 2">hidden,</xsl:if>
					<xsl:if test="($BitValue mod 8) >= 4">print,</xsl:if>
					<xsl:if test="($BitValue mod 16) >= 8">nozoom,</xsl:if>
					<xsl:if test="($BitValue mod 32) >= 16">norotate,</xsl:if>
					<xsl:if test="($BitValue mod 64) >= 32">noview,</xsl:if>
					<xsl:if test="($BitValue mod 128) >= 64">readonly,</xsl:if>
					<xsl:if test="($BitValue mod 256) >= 128">locked,</xsl:if>
					<xsl:if test="($BitValue mod 512) >= 256">togglenoview,</xsl:if>
				</xsl:variable>
				<!-- trim off last comma -->
				<xsl:attribute name="flags">
					<xsl:value-of select="substring($BitString, 1, string-length($BitString) - 1)"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Page -->
			<xsl:when test="$keyvalue = 'Page'">
				<xsl:attribute name="page">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- sound clip specific flags -->

			<!-- Bits -->
			<xsl:when test="$keyvalue = 'B'">
				<xsl:attribute name="bits">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<xsl:when test="$keyvalue = 'R'">
				<xsl:attribute name="rate">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<xsl:when test="$keyvalue = 'C'">
				<xsl:attribute name="channels">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- end of sound clip specific flags -->

			<xsl:when test="$keyvalue = 'Size'">
				<xsl:attribute name="size">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<xsl:when test="$keyvalue = 'Length'">
				<xsl:attribute name="length">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Mac only stream settings -->

			<xsl:when test="$keyvalue = 'Subtype'">
				<xsl:attribute name="subtype">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<xsl:when test="$keyvalue = 'Creator'">
				<xsl:attribute name="creator">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- end of Mac only stream settings -->

			<!-- Intensity -->
			<xsl:when test="$keyvalue = 'I'">
				<xsl:attribute name="intensity">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Opacity (really should come through FIXED[@KEY]) -->
			<xsl:when test="$keyvalue = 'CA'">
				<xsl:attribute name="opacity">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Quadding -->
			<xsl:when test="$keyvalue = 'Q'">
				<xsl:attribute name="justification">
					<xsl:choose>
						<xsl:when test="@VAL = 0">left</xsl:when>
						<xsl:when test="@VAL = 1">centered</xsl:when>
						<xsl:when test="@VAL = 2">right</xsl:when>
					</xsl:choose>
				</xsl:attribute>
			</xsl:when>

		</xsl:choose>
	</xsl:template>

	<xsl:template match="FIXED[@KEY]">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>
		<xsl:choose>

			<!-- Rotation (really should come through INT[@KEY]) -->
			<xsl:when test="$keyvalue = 'Rotate'">
				<xsl:attribute name="rotation">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Intensity (really should come through INT[@KEY]) -->
			<xsl:when test="$keyvalue = 'I'">
				<xsl:attribute name="intensity">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Opacity -->
			<xsl:when test="$keyvalue = 'CA'">
				<xsl:attribute name="opacity">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

		</xsl:choose>
	</xsl:template>

	<xsl:template match="NAME[@KEY]">
		<xsl:variable name="keyvalue"><xsl:value-of select="@KEY"/></xsl:variable>
		<xsl:choose>

			<!-- encoding (sound clip specific) -->
			<xsl:when test="$keyvalue = 'E'">
				<xsl:attribute name="encoding">
					<xsl:value-of select="translate (@VAL, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Icon -->
			<xsl:when test="$keyvalue = 'Name'">
				<xsl:attribute name="icon">
					<xsl:value-of select="@VAL"/>
				</xsl:attribute>
			</xsl:when>

			<!-- Border Style -->
			<xsl:when test="$keyvalue = 'S'">
				<xsl:variable name="Style" select="@VAL"/>
				<xsl:attribute name="style">
					<xsl:choose>
						<xsl:when test="$Style = 'S'">solid</xsl:when>
						<xsl:when test="$Style = 'D'">dash</xsl:when>
						<xsl:when test="$Style = 'B'">bevelled</xsl:when>
						<xsl:when test="$Style = 'I'">inset</xsl:when>
						<xsl:when test="$Style = 'U'">underline</xsl:when>
						<xsl:when test="$Style = 'C'">cloudy</xsl:when>
					</xsl:choose>
				</xsl:attribute>
			</xsl:when>

			<!-- Caret Symbol -->
			<xsl:when test="$keyvalue = 'Sy'">
				<xsl:attribute name="symbol">
					<xsl:choose>
						<xsl:when test="@VAL = 'None' or @VAL = ''">none</xsl:when>
						<xsl:when test="@VAL = 'P'">paragraph</xsl:when>
						<xsl:when test="@VAL = 'S'">space</xsl:when>
					</xsl:choose>
				</xsl:attribute>
			</xsl:when>

			<!--	Subtype (not a direct child of DICT,
					hence not annotation type) -->
			<xsl:when test="$keyvalue = 'Subtype'">
				<xsl:if test="ancestor::STREAM">
					<xsl:attribute name="mimetype">
						<xsl:value-of select="@VAL"/>
					</xsl:attribute>
				</xsl:if>
			</xsl:when>

		</xsl:choose>
	</xsl:template>

	<xsl:template match="DICT[not(@*)]">
		<!-- 	a DICT element without attributes is part of
				field definitions -->
		<xsl:apply-templates select="STRING[@KEY = 'T']" mode="fields"/>
	</xsl:template>

	<xsl:template match="DICT[@DEFINE or @KEY]">
		<xsl:choose>
			<xsl:when test="@KEY = 'FDF'">
				<xsl:element name="xfdf">
					<xsl:attribute name="xml:space">preserve</xsl:attribute>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>

			<xsl:when test="@KEY='BS'">
				<xsl:if test="(FIXED|INT)[@KEY='W']">
					<xsl:attribute name="width">
						<xsl:value-of select="(FIXED|INT)[@KEY='W']/@VAL"/>
					</xsl:attribute>
				</xsl:if>
				<!-- now handle dashes and style -->
				<xsl:apply-templates/>
			</xsl:when>

			<!-- this blocks processing of MacIntosh resource forks
				 until after the main data stream has been processed -->
			<xsl:when test="@KEY='Mac'"/>

			<xsl:when test="@KEY='FS' or @KEY='EF' or @KEY='BE'">
				<xsl:apply-templates/>
			</xsl:when>
			
			<!--	put this case here to allow only elements
					with DEFINE attribute beyond this point -->
			<xsl:when test="not(@DEFINE)">
				<xsl:apply-templates/>
			</xsl:when>

			<xsl:when test="@DEFINE = '1/0'">
				<xsl:apply-templates/>
			</xsl:when>

			<xsl:otherwise>
				<xsl:choose>
					<!--	most times when DICT[@DEFINE] is encountered
							this indicates a valid annotation, but
							not always -->
					<xsl:when test="NAME[@KEY = 'Subtype']">
						<!-- annotation type -->
						<!--	convert mixed cased annotation name to a lower cased
								element name -->
						<xsl:variable name="AnnotType">
							<xsl:value-of select="translate (NAME[@KEY = 'Subtype']/@VAL, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
						</xsl:variable>
						<xsl:element name="{$AnnotType}">
							<!--	make sure that embedded annotations
									get processed last, so
									that attributes can be added to
									current annotation -->
							<!--	contents, richtext and
									defaultappearance contents should
									be processed before handling
									embedded annotations -->
							<xsl:apply-templates select="(*[not(self::STRING[@KEY='RC' or @KEY='Contents' or @KEY='DA'] or self::DICT[@DEFINE or @KEY] or self::STREAM or self::APPEARANCE)])|DICT[@KEY='BS' or @KEY='BE']"/>
							<xsl:apply-templates select="DICT[@KEY='EF' or @KEY='FS']|STREAM|APPEARANCE"/>
							<!--	if both RC and Contents are there
									only use RC -->
							<xsl:choose>
								<xsl:when test="STRING[@KEY='Contents'] and STRING[@KEY='RC']">
									<xsl:apply-templates select="STRING[@KEY='RC']"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates select="STRING[@KEY='Contents' or @KEY='RC']"/>
								</xsl:otherwise>
							</xsl:choose>
							<xsl:apply-templates select="STRING[@KEY='DA']"/>
							<xsl:apply-templates select="DICT[@DEFINE][not(@KEY='BS' or @KEY='BE' or @KEY='FS' or @KEY='EF')]"/>
						</xsl:element>
					</xsl:when>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="STREAM">
		<!-- do the main part -->
		<xsl:apply-templates select="DATA"/>
		<!-- do the resource fork, if it exists -->
		<xsl:apply-templates select="DICT[@KEY='Params']/DICT[@KEY='Mac']/STREAM[@KEY='ResFork']/DATA"/>
	</xsl:template>

	<xsl:template match="DATA">
		<xsl:call-template name="emitParams"/>
		<xsl:variable name="NodeName">
		<xsl:choose>
			<xsl:when test="ancestor::DICT[@KEY='Mac']">resource</xsl:when>
			<xsl:otherwise>data</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>
		<xsl:element name="{$NodeName}">
			<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:call-template name="emitRestofParams"/>
			<xsl:value-of select="."/>
		</xsl:element>
	</xsl:template>

	<!-- get rid of any extraneous text nodes and attributes -->
	<xsl:template match="text()|@*"/>

</xsl:stylesheet>
