<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: rfc2sharef.xsl 282 2006-04-27 09:01:17Z dret $ -->
<!-- This stylesheet is designed to import RFC XML files into ShaRef. It is based on the rfc-index.xsd schema dated 2003-11-05 (no version number available, available at ftp://ftp.isi.edu/in-notes/rfc-index.xsd), and produces a ShaRef 1.0 document. -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rfc="http://www.rfc-editor.org/rfc-index" xmlns:sharef="http://dret.net/xmlns/sharef" xmlns="http://dret.net/xmlns/sharef" exclude-result-prefixes="rfc">
	<xsl:include href="common_import.xsl"/>
	<xsl:include href="message.xsl"/>
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:key name="doc-key" match="/rfc:rfc-index/*" use="rfc:doc-id/text()"/>
	<xsl:variable name="entries" select="max((count(/rfc:rfc-index/(rfc:std-entry | rfc:rfc-entry )),1))"/>
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:output indent="yes" method="xml" exclude-result-prefixes="rfc"/>
	<xsl:template match="/">
		<bibliography version="1.0">
			<associationdef id="obsoletes">
				<association>Obsoletes</association>
				<forward>obsoletes</forward>
				<reverse>is obsoleted by</reverse>
			</associationdef>
			<associationdef id="updates">
				<association>Updates</association>
				<forward>updates</forward>
				<reverse>is updated by</reverse>
			</associationdef>
			<associationdef id="isalso">
				<association>Is Also</association>
				<forward>is also</forward>
				<reverse>"is also" by</reverse>
			</associationdef>
			<associationdef id="seealso">
				<association>See Also</association>
				<forward>see also</forward>
				<reverse>"see also" by</reverse>
			</associationdef>
			<xsl:for-each-group select="//rfc:kw[string-length(replace(lower-case(text()), '[^a-zA-Z0-9]', '')) gt 0]" group-by="replace(lower-case(text()), '[^a-zA-Z0-9]', '')">
				<xsl:sort select="normalize-space(current-group()[1]/text())"/>
				<keyworddef id="{replace(lower-case(current-group()[1]/text()), '[^a-zA-Z0-9]', '')}">
					<keyword>
						<xsl:value-of select="normalize-space(current-group()[1]/text())"/>
					</keyword>
				</keyworddef>
			</xsl:for-each-group>
			<xsl:for-each select="rfc:rfc-index/( rfc:std-entry | rfc:rfc-entry )">
				<xsl:if test="ceiling(99 * (position() div $entries)) != ceiling(99 * ((position()-1) div $entries))">
					<xsl:call-template name="message">
						<xsl:with-param name="level" select="'progress'"/>
						<xsl:with-param name="text" select="ceiling(99 * (position() div $entries))"/>
					</xsl:call-template>
				</xsl:if>
				<reference id="{rfc:doc-id/text()}" name="{rfc:doc-id/text()}" type="bibtex:misc">
					<howpublished>
						<xsl:text>Internet </xsl:text>
						<xsl:if test="exists(rfc:publication-status) and (rfc:publication-status ne 'UNKNOWN') and (rfc:publication-status ne 'FOR YOUR INFORMATION')">
							<xsl:value-of select="concat(normalize-space(lower-case(rfc:publication-status/text())), ' ')"/>
						</xsl:if>
						<xsl:value-of select="replace(rfc:doc-id/text(), '(\p{L}+)(\d+)', '$1 $2')"/>
					</howpublished>
					<xsl:if test="not(self::rfc:std-entry)">
						<identifier type="sharef:uri" resourceType="text/plain">
							<xsl:text>http://tools.ietf.org/html/</xsl:text>
							<xsl:choose>
								<xsl:when test="self::rfc:fyi-entry">
									<xsl:value-of select="concat('fyi', string(number(substring(rfc:doc-id/text(), 4))))"/>
								</xsl:when>
								<xsl:when test="self::rfc:rfc-entry">
									<xsl:value-of select="concat('rfc', string(number(substring(rfc:doc-id/text(), 4))))"/>
								</xsl:when>
								<xsl:when test="self::rfc:bcp-entry">
									<xsl:value-of select="concat('bcp', string(number(substring(rfc:doc-id/text(), 4))))"/>
								</xsl:when>
							</xsl:choose>
						</identifier>
					</xsl:if>
					<xsl:for-each select="*">
						<xsl:choose>
							<xsl:when test="local-name() = 'abstract'">
								<abstract>
									<richtext>
										<p>
											<xsl:value-of select="normalize-space(text())"/>
										</p>
									</richtext>
								</abstract>
							</xsl:when>
							<xsl:when test="local-name() = 'title'">
								<title type="sharef:primaryTitle">
									<xsl:value-of select="normalize-space(text())"/>
								</title>
							</xsl:when>
							<xsl:when test="local-name() = 'date'">
								<xsl:variable name="month">
									<!-- only english month names will be recognized as content of the month field -->
									<xsl:choose>
										<xsl:when test="rfc:month = 'January'">01</xsl:when>
										<xsl:when test="rfc:month = 'February'">02</xsl:when>
										<xsl:when test="rfc:month = 'March'">03</xsl:when>
										<xsl:when test="rfc:month = 'April'">04</xsl:when>
										<xsl:when test="rfc:month = 'May'">05</xsl:when>
										<xsl:when test="rfc:month = 'June'">06</xsl:when>
										<xsl:when test="rfc:month = 'July'">07</xsl:when>
										<xsl:when test="rfc:month = 'August'">08</xsl:when>
										<xsl:when test="rfc:month = 'September'">09</xsl:when>
										<xsl:when test="rfc:month = 'October'">10</xsl:when>
										<xsl:when test="rfc:month = 'November'">11</xsl:when>
										<xsl:when test="rfc:month = 'December'">12</xsl:when>
										<xsl:otherwise>00</xsl:otherwise>
									</xsl:choose>
								</xsl:variable>
								<xsl:call-template name="process_date">
									<xsl:with-param name="year" select="rfc:year"/>
									<xsl:with-param name="month" select="$month"/>
									<xsl:with-param name="day" select="rfc:day"/>
								</xsl:call-template>
							</xsl:when>
						</xsl:choose>
					</xsl:for-each>
					<xsl:if test="not(exists(rfc:title)) and (count(rfc:is-also/rfc:doc-id) eq 1)">
						<title type="sharef:primaryTitle">
							<xsl:value-of select="normalize-space(key('doc-key', rfc:is-also/rfc:doc-id/text())/rfc:title/text())"/>
						</title>
					</xsl:if>
					<xsl:if test="exists(rfc:author)">
						<names type="sharef:author">
							<xsl:for-each select="rfc:author/rfc:name">
								<!-- the following xsl:choose has been copied from endnote2sharef.xsl (except for the replace and the following xsl:if, which remove some special unwanted cases from the name processing) -->
								<xsl:variable name="name" select="replace(normalize-space(text()), 'et al\.| ?3rd|\(deceased\)', '')"/>
								<xsl:if test="string-length($name) gt 0">
									<xsl:variable name="parse_names" select="'yes'"/>
									<xsl:choose>
										<xsl:when test="contains($name, '(') or not(contains($name, ' ')) or (not(contains($name, ',')) and $parse_names = 'no') or ((substring(normalize-space($name), string-length($name), 1) = ',') and (not(contains(substring(normalize-space($name), 1, string-length($name)-1), ','))))">
											<!-- if there is a parenthesis, or there is no comma, or a comma as the only and last character, it is not a person -->
											<name>
												<xsl:choose>
													<!-- copy text if no comma, remove trailing comma if there was one -->
													<xsl:when test="not(contains($name, ','))">
														<xsl:value-of select="normalize-space($name)"/>
													</xsl:when>
													<xsl:otherwise>
														<xsl:value-of select="substring(normalize-space($name), 1, string-length($name)-1)"/>
													</xsl:otherwise>
												</xsl:choose>
											</name>
										</xsl:when>
										<xsl:when test="not(contains($name, ',')) and $parse_names='yes'">
											<!-- there is no comma and name parsing is enabled -->
											<xsl:variable name="name_parser" select="'^(.*?)\s+((\p{Ll}.*)\s+)?(\p{L}\S*)$'"/>
											<person>
												<givenname>
													<xsl:value-of select="normalize-space(replace($name, $name_parser, '$1'))"/>
												</givenname>
												<surname>
													<xsl:if test="string-length(replace($name, $name_parser, '$3')) gt 0">
														<xsl:attribute name="link" select="normalize-space(replace($name, $name_parser, '$3'))"/>
													</xsl:if>
													<xsl:value-of select="normalize-space(replace($name, $name_parser, '$4'))"/>
												</surname>
											</person>
										</xsl:when>
										<xsl:otherwise>
											<!-- it is a person (i.e., there was a comma), now map the name -->
											<person>
												<givenname>
													<xsl:value-of select="normalize-space(substring-after($name, ','))"/>
												</givenname>
												<surname>
													<xsl:value-of select="normalize-space(substring-before($name, ','))"/>
												</surname>
											</person>
										</xsl:otherwise>
									</xsl:choose>
								</xsl:if>
							</xsl:for-each>
						</names>
					</xsl:if>
					<xsl:if test="exists(rfc:keywords/rfc:kw[string-length(replace(lower-case(text()), '[^a-zA-Z0-9]', '')) gt 0])">
						<annotation type="sharef:keywords">
							<richtext>
								<p>
									<xsl:text>Keywords: </xsl:text>
									<xsl:for-each select="rfc:keywords/rfc:kw[string-length(replace(lower-case(text()), '[^a-zA-Z0-9]', '')) gt 0]">
										<keywordref type="{replace(lower-case(text()), '[^a-zA-Z0-9]', '')}"/>
										<xsl:if test="position() ne last()">
											<xsl:text>; </xsl:text>
										</xsl:if>
									</xsl:for-each>
								</p>
							</richtext>
						</annotation>
					</xsl:if>
					<xsl:if test="exists(self::rfc:rfc-entry/( rfc:obsoletes | rfc:updates | rfc:is-also | rfc:see-also ))">
						<associations>
							<xsl:for-each select="self::rfc:rfc-entry/( rfc:obsoletes | rfc:updates | rfc:is-also | rfc:see-also )/*">
								<xsl:if test="not(parent::rfc:is-also/rfc:doc-id[starts-with(text(), 'BCP') or starts-with(text(), 'FYI')])">
									<!-- do not copy is-also entries pointing to bcp or fyi entries (these are represented as annotations) -->
									<xref type="{translate(local-name(..), '-', '')}" target="{text()}"/>
								</xsl:if>
							</xsl:for-each>
						</associations>
					</xsl:if>
					<xsl:if test="exists(self::rfc:rfc-entry/rfc:is-also/rfc:doc-id[starts-with(text(), 'BCP') or starts-with(text(), 'FYI')])">
						<annotation>
							<richtext>
								<p>This RFC is also known as <xsl:value-of select="self::rfc:rfc-entry/rfc:is-also/rfc:doc-id"/>.</p>
							</richtext>
						</annotation>
					</xsl:if>
				</reference>
			</xsl:for-each>
		</bibliography>
		<xsl:call-template name="message">
			<xsl:with-param name="level" select="'progress'"/>
			<xsl:with-param name="text" select="'100'"/>
		</xsl:call-template>
	</xsl:template>
</xsl:stylesheet>
