<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" 
	xmlns		= "http://www.w3.org/1999/xhtml"
	xmlns:h		= "http://www.w3.org/1999/xhtml"
	xmlns:xsl	= "http://www.w3.org/1999/XSL/Transform"
	xmlns:owl	= "http://www.w3.org/2002/07/owl"
	xmlns:xsi	= "http://www.w3.org/2001/XMLSchema-instance"
	xmlns:rdf	= "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs	= "http://www.w3.org/2000/01/rdf-schema#"
	xmlns:rss	= "http://purl.org/rss/1.0/"
	xmlns:cc	= "http://web.resource.org/cc/"
	xmlns:dc	= "http://purl.org/dc/elements/1.0/"
	xmlns:dcterms	= "http://purl.org/dc/terms/"
	xmlns:dcq	= "http://purl.org/metadata/dublin_core_qualifiers#"
	xmlns:hr	= "http://www.w3.org/2000/08/w3c-synd/#"
	xmlns:date	= "http://exslt.org/dates-and-times"
	xmlns:foaf	= "http://xmlns.com/foaf/0.1/"
	xmlns:vcard	= "http://www.w3.org/2001/vcard-rdf/3.0#"
	xmlns:rel	= "http://purl.org/vocab/relationship"
	xmlns:admin	= "http://webns.net/mvcb/"
	xmlns:svg	= "http://www.w3.org/2000/svg"
	xmlns:xlink	= "http://www.w3.org/1999/xlink"
	exclude-result-prefixes="xsl h">

<!-- meta Extractor -->

<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:variable name="nl">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:param name="PAGE" select="''"/>

<!-- sample from w3c:
<rdf:RDF
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
   <rdf:Description rdf:about="">
     <dc:subject>ADAM; Simple Search; Index+; prototype</dc:subject>
   </rdf:Description>
</rdf:RDF>
-->
<xsl:template match="/">
	<rdf:RDF> <!-- using literal elements copies all of the namespace declarations from stylesheet -->
		<xsl:element name="rdf:Description">
			<xsl:attribute name="rdf:about">
				<xsl:value-of select="$PAGE"/>
			</xsl:attribute>
			<xsl:apply-templates select="h:html"/>
		</xsl:element>
	</rdf:RDF>
</xsl:template>

<xsl:template match="h:html">
	<xsl:apply-templates select="@*"/>
<!--	<xsl:apply-templates select="h:head"/>-->
	<xsl:apply-templates select="h:body"/>
</xsl:template>

<xsl:template match="@xml:lang">
	<xsl:element name="dc:language">
		<xsl:value-of select="."/>
	</xsl:element>
</xsl:template>

<!-- ============================================================================================== -->
<!-- head                                                                                           -->
<!-- ============================================================================================== -->

<!-- ============================================================================================== -->
<!-- body                                                                                           -->
<!-- ============================================================================================== -->
<xsl:template match="h:*[contains(@class,'vcard ')]|h:*[contains(@class,' vcard')]|h:*[@class='vcard']">
<!--	<xsl:comment>VCARD</xsl:comment>-->
	<xsl:element name="foaf:Person">
		<xsl:attribute name="rdf:ID">
			<xsl:choose>
				<xsl:when test="@id">
					<xsl:value-of select="@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="contains(@class,' ')">
							<xsl:choose>
								<xsl:when test="contains(@class,'fn ') or contains(@class,' fn')">
									<xsl:value-of select="generate-id(.)"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="translate(normalize-space(substring-before(@class,'vcard')),' ','_')"/>
									<xsl:value-of select="translate(normalize-space(substring-after(@class,'vcard')),' ','_')"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="generate-id(.)"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:apply-templates select="current()" mode="PERSON">
			<xsl:with-param name="CLASS" select="@class"/>
		</xsl:apply-templates>
<!-- check if N present -->
		<xsl:variable name="N">
			<xsl:apply-templates mode="SEARCH">
				<xsl:with-param name="CLASS" select="''"/>
				<xsl:with-param name="STR" select="'n'"/>
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:if test="$N = ''">		<!-- no 'n' specified -->
			<xsl:variable name="N_SUB">
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'family-name'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'given-name'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'additional-name'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'honorific-prefix'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'honorific-suffix'"/>
				</xsl:apply-templates>
			</xsl:variable>
			<xsl:if test="not($N_SUB = '')">
				<xsl:element name="vcard:N">
					<xsl:attribute name="rdf:parseType">
						<xsl:text>Resource</xsl:text>
					</xsl:attribute>
					<xsl:apply-templates select="h:*" mode="N"/>
				</xsl:element>
			</xsl:if>
		</xsl:if>
<!-- check if ADR present -->
		<xsl:variable name="ADR">
			<xsl:apply-templates mode="SEARCH">
				<xsl:with-param name="CLASS" select="''"/>
				<xsl:with-param name="STR" select="'adr'"/>
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:if test="$ADR = ''">		<!-- no 'adr' specified -->
			<xsl:variable name="ADR_SUB">
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'post-office-box'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'extended-address'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'street-address'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'locality'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'region'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'postal-code'"/>
				</xsl:apply-templates>
				<xsl:apply-templates mode="SEARCH">
					<xsl:with-param name="CLASS" select="''"/>
					<xsl:with-param name="STR" select="'country-name'"/>
				</xsl:apply-templates>
			</xsl:variable>
			<xsl:if test="not($ADR_SUB = '')">
				<xsl:element name="vcard:ADR">
					<xsl:attribute name="rdf:parseType">
						<xsl:text>Resource</xsl:text>
					</xsl:attribute>
					<xsl:apply-templates select="h:*" mode="ADR"/>
				</xsl:element>
			</xsl:if>
		</xsl:if>
<!-- TODO -->
	</xsl:element>
</xsl:template>
<!--
<xsl:template match="h:*[@class='vcard']" mode="PERSON">
	<xsl:apply-templates mode="PERSON"/>
</xsl:template>
-->
<xsl:template match="h:*[@class]" mode="PERSON">
	<xsl:param name="CLASS"/>
<!--
	<xsl:comment>
		<xsl:text>Element: </xsl:text><xsl:value-of select="name()"/>
		<xsl:text>.</xsl:text>
		<xsl:value-of select="@class"/>
		<xsl:text> (</xsl:text>
		<xsl:value-of select="$CLASS"/>
		<xsl:text>)</xsl:text>
	</xsl:comment>
-->
	<xsl:choose>
		<xsl:when test="$CLASS = ''">
			<xsl:apply-templates select="current()" mode="PERSON">
				<xsl:with-param name="CLASS" select="@class"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="contains($CLASS,' ')">
			<xsl:apply-templates select="current()" mode="PERSON">
				<xsl:with-param name="CLASS" select="normalize-space(substring-before($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="current()" mode="PERSON">
				<xsl:with-param name="CLASS" select="normalize-space(substring-after($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:if test="not(contains(normalize-space(substring-after($CLASS,' ')),' '))">
				<xsl:apply-templates mode="PERSON">
					<xsl:with-param name="CLASS" select="''"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$CLASS = 'vcard'"/>
<!-- simple records -->
				<xsl:when test="$CLASS = 'fn'">
					<xsl:element name="vcard:FN">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'nickname'">
					<xsl:element name="vcard:NICKNAME">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'bday'">
					<xsl:element name="vcard:BDAY">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'mailer'">
					<xsl:element name="vcard:MAILER">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'geo'">
					<xsl:element name="vcard:GEO">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'title'">
					<xsl:element name="vcard:TITLE">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'role'">
					<xsl:element name="vcard:ROLE">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'categories'">
					<xsl:element name="vcard:CATEGORIES">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'name'">
					<xsl:element name="vcard:NAME">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'source'">
					<xsl:element name="vcard:SOURCE">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'note'">
					<xsl:element name="vcard:NOTE">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'rev'">
					<xsl:element name="vcard:REV">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'sort-string'">
					<xsl:element name="vcard:SORT-STRING">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'class'">
					<xsl:element name="vcard:CLASS">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
<!-- compound records -->
				<xsl:when test="$CLASS = 'n'">
					<xsl:element name="vcard:N">
						<xsl:attribute name="rdf:parseType">
							<xsl:text>Resource</xsl:text>
						</xsl:attribute>
						<xsl:if test="contains(.,';')">
							<xsl:call-template name="MULTIVAL">
								<xsl:with-param name="LIST" select="."/>
								<xsl:with-param name="NAMES">
									<xsl:text>vcard:Family;vcard:Given;vcard:Other;vcard:Prefix;vcard:Suffix</xsl:text>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:if>
						<xsl:apply-templates select="h:*[@class]" mode="N"/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'adr'">
					<xsl:element name="vcard:ADR">
						<xsl:attribute name="rdf:parseType">
							<xsl:text>Resource</xsl:text>
						</xsl:attribute>
						<xsl:if test="contains(.,';')">
							<xsl:call-template name="MULTIVAL">
								<xsl:with-param name="LIST" select="."/>
								<xsl:with-param name="NAMES">
									<xsl:text>vcard:Pobox;vcard:Extended;vcard:Street;vcard:Locality;vcard:Region;vcard:Pcode;vcard:Country</xsl:text>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:if>
						<xsl:apply-templates select="h:*[@class]" mode="ADR"/>
					</xsl:element>
				</xsl:when>
<!--
				<xsl:otherwise>
					<xsl:comment>unknown <xsl:value-of select="$CLASS"/></xsl:comment>
				</xsl:otherwise>
-->
			</xsl:choose>
			<xsl:if test="not(contains(@class,' '))">
				<xsl:apply-templates mode="PERSON">
					<xsl:with-param name="CLASS" select="''"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="h:*[not(@class)]" mode="PERSON">
	<xsl:apply-templates mode="PERSON"/>
</xsl:template>

<!-- Semicolon-separated list of values. LIST contains the list of values, NAMES contains the list of field names. -->
<xsl:template name="MULTIVAL">
	<xsl:param name="LIST"/>
	<xsl:param name="NAMES"/>
	<xsl:choose>
		<xsl:when test="not($NAMES)"/>
		<xsl:when test="NAMES = ''"/>
		<xsl:when test="contains($NAMES,';')">
			<xsl:choose>
				<xsl:when test="$LIST = ''"/>
				<xsl:when test="contains($LIST,';')">
					<xsl:if test="not(substring-before($LIST,';') = '')">
						<xsl:element name="{substring-before($NAMES,';')}">
							<xsl:choose>
								<xsl:when test="contains($LIST,';')">
									<xsl:value-of select="substring-before($LIST,';')"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$LIST"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:element>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:element name="{substring-before($NAMES,';')}">
						<xsl:choose>
							<xsl:when test="contains($LIST,';')">
								<xsl:value-of select="substring-before($LIST,';')"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$LIST"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:element>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name="MULTIVAL">
				<xsl:with-param name="LIST">
					<xsl:choose>
						<xsl:when test="contains($LIST,';')">
							<xsl:value-of select="substring-after($LIST,';')"/>
						</xsl:when>
						<xsl:otherwise><xsl:text/></xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
				<xsl:with-param name="NAMES" select="substring-after($NAMES,';')"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="not($LIST = '')">
				<xsl:element name="{$NAMES}">
					<xsl:value-of select="$LIST"/>
				</xsl:element>
			</xsl:if>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="PERSON"/>

<xsl:template match="h:*" mode="N">
	<xsl:param name="CLASS"/>
	<xsl:choose>
		<xsl:when test="not($CLASS)">
			<xsl:apply-templates mode="N">
				<xsl:with-param name="CLASS" select="@class"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="$CLASS = ''">
			<xsl:apply-templates select="current()" mode="N">
				<xsl:with-param name="CLASS" select="@class"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="contains($CLASS,' ')">
			<xsl:apply-templates select="current()" mode="N">
				<xsl:with-param name="CLASS" select="normalize-space(substring-before($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="current()" mode="N">
				<xsl:with-param name="CLASS" select="normalize-space(substring-after($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:if test="not(contains(normalize-space(substring-after($CLASS,' ')),' '))">
				<xsl:apply-templates mode="N">
					<xsl:with-param name="CLASS" select="''"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$CLASS = 'vcard'"/>
				<xsl:when test="$CLASS = 'family-name'">
					<xsl:element name="vcard:Family">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'given-name'">
					<xsl:element name="vcard:Given">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'additional-name'">
					<xsl:element name="vcard:Other">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'honorific-prefix'">
					<xsl:element name="vcard:Prefix">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'honorific-suffix'">
					<xsl:element name="vcard:Suffix">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
			</xsl:choose>
			<xsl:apply-templates mode="N">
				<xsl:with-param name="CLASS" select="''"/>
			</xsl:apply-templates>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="N"/>

<xsl:template match="h:*" mode="ADR">
	<xsl:param name="CLASS"/>
	<xsl:choose>
		<xsl:when test="not(@class)">
			<xsl:apply-templates mode="ADR">
				<xsl:with-param name="CLASS" select="''"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="not($CLASS)">
			<xsl:apply-templates select="current()" mode="ADR">
				<xsl:with-param name="CLASS" select="@class"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="$CLASS = ''">
			<xsl:apply-templates select="current()" mode="ADR">
				<xsl:with-param name="CLASS" select="@class"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="contains($CLASS,' ')">
			<xsl:apply-templates select="current()" mode="ADR">
				<xsl:with-param name="CLASS" select="normalize-space(substring-before($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="current()" mode="ADR">
				<xsl:with-param name="CLASS" select="normalize-space(substring-after($CLASS,' '))"/>
			</xsl:apply-templates>
			<xsl:if test="not(contains(normalize-space(substring-after($CLASS,' ')),' '))">
				<xsl:apply-templates mode="ADR">
					<xsl:with-param name="CLASS" select="''"/>
				</xsl:apply-templates>
			</xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$CLASS = 'vcard'"/>
				<xsl:when test="$CLASS = 'post-office-box'">
					<xsl:element name="vcard:Pobox">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'extended-address'">
					<xsl:element name="vcard:Extadd">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'street-address'">
					<xsl:element name="vcard:Street">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'locality'">
					<xsl:element name="vcard:Locality">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'region'">
					<xsl:element name="vcard:Region">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'postal-code'">
					<xsl:element name="vcard:Pcode">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$CLASS = 'country-name'">
					<xsl:element name="vcard:Country">
						<xsl:value-of select="."/>
					</xsl:element>
				</xsl:when>
			</xsl:choose>
			<xsl:apply-templates mode="ADR">
				<xsl:with-param name="CLASS" select="''"/>
			</xsl:apply-templates>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="ADR"/>

<xsl:template match="h:*" mode="SEARCH">
	<xsl:param name="CLASS"/>
	<xsl:param name="STR"/>
	<xsl:choose>
		<xsl:when test="not(@class)">
			<xsl:apply-templates mode="SEARCH">
				<xsl:with-param name="CLASS" select="@class"/>
				<xsl:with-param name="STR" select="$STR"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="$CLASS = ''">
			<xsl:apply-templates select="current()" mode="SEARCH">
				<xsl:with-param name="CLASS" select="@class"/>
				<xsl:with-param name="STR" select="$STR"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:when test="contains($CLASS,' ')">
			<xsl:apply-templates select="current()" mode="SEARCH">
				<xsl:with-param name="CLASS" select="substring-before($CLASS,' ')"/>
				<xsl:with-param name="STR" select="$STR"/>
			</xsl:apply-templates>
			<xsl:apply-templates select="current()" mode="SEARCH">
				<xsl:with-param name="CLASS" select="substring-after($CLASS,' ')"/>
				<xsl:with-param name="STR" select="$STR"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="$CLASS = $STR">
					<xsl:text>yes</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates mode="SEARCH">
						<xsl:with-param name="CLASS" select="''"/>
						<xsl:with-param name="STR" select="$STR"/>
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="SEARCH"/>

<xsl:template match="text()"/>

</xsl:stylesheet>
