<?xml version="1.0" encoding="UTF-8"?>
<stylesheet
    xmlns  ="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsl  ="http://www.w3.org/1999/XSL/Transform"
    xmlns:h    ="http://www.w3.org/1999/xhtml"
    xmlns:rdf  ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:v  ="http://www.w3.org/2006/vcard/ns#">


<!-- hGRDDL hCard : GRDDL transformation from hCard to microformats
	 Version 0.1 by Fabien.Gandon@sophia.inria.fr                   -->


<output indent="yes" method="xml" media-type="text/html" encoding="UTF-8" omit-xml-declaration="yes"/>

<!-- templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

  <template match="/">
   <copy>
  	  <apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
  </template>  

  <!-- declare namespaces -->
  <template match="h:html">
  	  <html xmlns="http://www.w3.org/1999/xhtml"
  	  	 xmlns:rdf  ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  	  	 xmlns:v  ="http://www.w3.org/2006/vcard/ns#">
        <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
      </html>
  </template>  

 <!-- update transform -->
 <template match="h:link[attribute::rel='transformation' and attribute::href='hCard2RDFa.xsl']">
   <link rel="transformation" href="RDFa2RDFXML.xsl" xmlns="http://www.w3.org/1999/xhtml"/>
 </template>
 
 
 <!-- VCard templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <!-- v:Card -->
 <template match="*[attribute::class='vcard']">
   <copy>
    <apply-templates select="@*" /> 
    <if test="not(attribute::about) and attribute::id"><attribute name="about">#<value-of select="attribute::id"/></attribute></if>
    <if test="not(attribute::about) and not(attribute::id)"><attribute name="about">#<value-of select="generate-id()"/></attribute></if>
    <apply-templates select="text()[string-length(normalize-space(.))=0][position()=1]" /> 
    <link xmlns="http://www.w3.org/1999/xhtml" rel="rdf:type" href="v:VCard"/>
   	<apply-templates select="*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>

 <!-- v:photo -->
 <template match="h:img[attribute::class='photo']">
   <copy><xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /></copy>
   <apply-templates select="../text()[string-length(normalize-space(.))=0][position()=1]" /> 
   <span xmlns="http://www.w3.org/1999/xhtml" rel="v:photo" href="{@src}"/>
 </template>
 
 <!-- v:url -->
 <template match="h:a">
   <copy>
   	<if test="ancestor::*[attribute::class='vcard'] and (contains(attribute::class,' url ') or starts-with(attribute::class,'url ') or attribute::class='url')">
   		<attribute name="rel"><value-of select="normalize-space(concat('v:url ',attribute::rel))"/></attribute>
   	</if>
   	<if test="ancestor::*[attribute::class='vcard'] and (contains(attribute::class,' email ') or starts-with(attribute::class,'email ') or attribute::class='email')">
   		<attribute name="property"><value-of select="normalize-space(concat('v:email ',attribute::property))"/></attribute>
   	</if>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- v:given-name -->
 <template match="*[attribute::class='given-name' and ancestor::*/attribute::class='vcard']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:given-name ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- v:family-name -->
 <template match="*[attribute::class='family-name' and ancestor::*/attribute::class='vcard']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:family-name ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
  <!-- v:additional-name -->
 <template match="*[attribute::class='additional-name' and ancestor::*/attribute::class='vcard']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:additional-name ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
  
   <!-- v:org -->
 <template match="*[attribute::class='org' and ancestor::*/attribute::class='vcard']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:org ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
 
    <!-- v:tel -->
 <template match="*[attribute::class='tel' and ancestor::*/attribute::class='vcard']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:tel ',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
 
 
 <!-- copy everything -->
 <template match="@*|*|processing-instruction()|comment()">
  <copy><apply-templates select="*|@*|text()|processing-instruction()|comment()" /></copy>
 </template>

 <!-- Adresse templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <!-- v:Card -->
 <template match="*[attribute::class='adr']">
   <copy>
    <apply-templates select="@*" /> 
    <if test="not(attribute::about) and attribute::id"><attribute name="about">#<value-of select="attribute::id"/></attribute></if>
    <if test="not(attribute::about) and not(attribute::id)"><attribute name="about">#<value-of select="generate-id()"/></attribute></if>
    <apply-templates select="text()[string-length(normalize-space(.))=0][position()=1]" /> 
    <link xmlns="http://www.w3.org/1999/xhtml" rel="rdf:type" href="v:Address"/>
    <apply-templates select="text()[string-length(normalize-space(.))=0][position()=1]" />
    <for-each select="ancestor::*[attribute::class='vcard'][position()=1]">
      <if test="not(attribute::about) and attribute::id"><link xmlns="http://www.w3.org/1999/xhtml" rev="v:adr" href="#{attribute::id}"/></if>
      <if test="not(attribute::about) and not(attribute::id)"><link xmlns="http://www.w3.org/1999/xhtml" rev="v:adr" href="#{generate-id()}"/></if>
    </for-each>
   	<apply-templates select="*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template>
 
 <!-- v:street-address -->
 <template match="*[attribute::class='street-address' and ancestor::*/attribute::class='adr']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:street-address',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
 
 <!-- v:region -->
 <template match="*[attribute::class='region' and ancestor::*/attribute::class='adr']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:region',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
 
 <!-- v:locality -->
 <template match="*[attribute::class='locality' and ancestor::*/attribute::class='adr']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:locality',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 

  <!-- v:postal-code -->
 <template match="*[attribute::class='postal-code' and ancestor::*/attribute::class='adr']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:postal-code',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 

  <!-- v:country-name -->
 <template match="*[attribute::class='country-name' and ancestor::*/attribute::class='adr']">
   <copy>
    <attribute name="property"><value-of select="normalize-space(concat('v:country-name',attribute::property))"/></attribute>
    <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" /> 
   </copy>
 </template> 
</stylesheet>
