<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://simonstl.com/foaf-xml/0.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wot="http://xmlns.com/wot/0.1/"
    xmlns:rss="http://purl.org/rss/1.0/"
    exclude-result-prefixes="xsl rdf rdfs foaf"
    >
    
<!--XSLT stylesheet for transforming FOAF RDF to FOAF XML
   Donated to the public domain by Simon St.Laurent, http://simonstl.com -->

<xsl:strip-space elements="foaf:Person" />
<xsl:output indent="yes" encoding="UTF-8"/>

<xsl:template match="/rdf:RDF" >
  <foafParts>
  <xsl:apply-templates />
  </foafParts>
</xsl:template>


<xsl:template match="foaf:Person[foaf:knows]">
<person><xsl:call-template name="hrefAtts"/><xsl:apply-templates />
<knows>
<xsl:for-each select="foaf:knows">
  <xsl:variable name="frag" select="@rdf:resource" />
<!--<xsl:message>Frag is set to <xsl:value-of select="$frag" /></xsl:message>-->
<xsl:choose>
<xsl:when test="$frag">
  <xsl:variable name="ident" select="substring-after($frag, '#')" />
<!--<xsl:message>Ident is set to <xsl:value-of select="$ident" /></xsl:message>-->

  <xsl:apply-templates select="//foaf:Person[@rdf:ID=$ident]" mode="known" />
</xsl:when>
<xsl:otherwise>
<!--<xsl:message>Otherwise</xsl:message>-->

  <xsl:apply-templates select="foaf:Person" mode="known"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</knows>
</person>
</xsl:template>





<xsl:template match="foaf:Project"><project><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></project></xsl:template>

<xsl:template match="foaf:Document"><document><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></document></xsl:template>

<xsl:template match="foaf:Organization"><organization><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></organization></xsl:template>

<xsl:template match="foaf:Image"><image><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></image></xsl:template> 


<!--FOAF pieces-->

<xsl:template match="foaf:currentProject"><currentProject><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></currentProject></xsl:template>

<xsl:template match="foaf:depiction"><depiction><xsl:call-template name="srcAtts"/><xsl:apply-templates /></depiction></xsl:template>

<xsl:template match="foaf:name"><name><xsl:apply-templates /></name></xsl:template>

<xsl:template match="foaf:mbox"><mbox><xsl:call-template name="hrefAtts"/><xsl:apply-templates/></mbox></xsl:template>

<xsl:template match="foaf:pastProject"><pastProject><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></pastProject></xsl:template>

<xsl:template match="foaf:depicts" /> <!--dunno how this works-->

<xsl:template match="foaf:firstName"><givenName><xsl:apply-templates /></givenName></xsl:template>

<xsl:template match="foaf:mbox_sha1sum"><mbox_sha1sum><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></mbox_sha1sum></xsl:template>

<xsl:template match="foaf:fundedBy"><fundedBy><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></fundedBy></xsl:template>

<xsl:template match="foaf:thumbnail"><thumbnail><xsl:call-template name="srcAtts"/><xsl:apply-templates /></thumbnail ></xsl:template>

<xsl:template match="foaf:givenname"><givenName><xsl:apply-templates /></givenName></xsl:template>

<xsl:template match="foaf:geekcode"><geekcode><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></geekcode></xsl:template>

<xsl:template match="foaf:linkedWith" /> <!--dunno how this works-->

<xsl:template match="foaf:myersBriggs"><myersBriggs><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></myersBriggs></xsl:template>

<xsl:template match="foaf:surname"><familyName><xsl:apply-templates /></familyName></xsl:template>

<xsl:template match="foaf:dnaChecksum"><dnaChecksum><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></dnaChecksum></xsl:template>
<!--support even the jokes, yes-->

<xsl:template match="foaf:logo" /> <!--dunno how this works-->

<xsl:template match="foaf:workplaceHomepage"><workplaceHomepage><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></workplaceHomepage></xsl:template>

<xsl:template match="foaf:family_name"><familyName><xsl:apply-templates /></familyName></xsl:template>

<xsl:template match="foaf:sha1" /> <!--dunno how this works-->

<xsl:template match="foaf:topic"><topic><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></topic></xsl:template>

<xsl:template match="foaf:workInfoHomepage"><workInfoHomepage><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></workInfoHomepage></xsl:template>

<xsl:template match="foaf:phone"><phone><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></phone></xsl:template>

<xsl:template match="foaf:title"><title><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></title></xsl:template>

<xsl:template match="foaf:page"><page><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></page></xsl:template>

<xsl:template match="foaf:schoolHomepage"><schoolHomepage><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></schoolHomepage></xsl:template>

<xsl:template match="foaf:homepage"><homepage><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></homepage></xsl:template>

<xsl:template match="foaf:nick"><nick><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></nick></xsl:template>

<xsl:template match="foaf:theme"><theme><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></theme></xsl:template>

<xsl:template match="foaf:knows" />
<!--knows is processed about in group-->

<xsl:template match="foaf:weblog"><weblog><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></weblog></xsl:template>

<xsl:template match="foaf:jabberID"><jabberID><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></jabberID></xsl:template>

<xsl:template match="foaf:interest"><interest><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></interest></xsl:template>

<xsl:template match="foaf:plan"><plan><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></plan></xsl:template>

<xsl:template match="foaf:aimChatID"><aimChatID><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></aimChatID></xsl:template>

<xsl:template match="foaf:topic_interest"><topicInterest><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></topicInterest></xsl:template>

<xsl:template match="foaf:made"><made><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></made></xsl:template>

<xsl:template match="foaf:msnChatID"><msnChatID><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></msnChatID></xsl:template>

<xsl:template match="foaf:publications"><publications><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></publications></xsl:template>

<xsl:template match="foaf:iChatID"><iChatID><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></iChatID></xsl:template>

<xsl:template match="foaf:img"><img><xsl:call-template name="srcAtts"/><xsl:apply-templates /></img></xsl:template>

<xsl:template match="foaf:Person" />

<xsl:template match="foaf:Person" mode="known"><knownPerson><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></knownPerson></xsl:template>

<xsl:template name="hrefAtts">
<xsl:if test="@rdf:resource"><xsl:attribute name="href"><xsl:value-of select="@rdf:resource"/></xsl:attribute></xsl:if>
<xsl:if test="@rdf:ID"><xsl:attribute name="ID"><xsl:value-of select="@rdf:ID"/></xsl:attribute></xsl:if>
</xsl:template>

<xsl:template name="srcAtts">
<xsl:if test="@rdf:resource"><xsl:attribute name="href"><xsl:value-of select="@rdf:resource"/></xsl:attribute></xsl:if>
<xsl:if test="@rdf:ID"><xsl:attribute name="ID"><xsl:value-of select="@rdf:ID"/></xsl:attribute></xsl:if>
</xsl:template>




<!--RDF bits-->

<xsl:template match="rdf:Description"><description><xsl:call-template name="rdfAtts"/><xsl:apply-templates /></description></xsl:template>

<xsl:template match="rdfs:isDefinedBy"><isDefinedBy><xsl:call-template name="rdfAtts"/><xsl:apply-templates /></isDefinedBy></xsl:template>

<xsl:template match="rdfs:seeAlso"><seeAlso><xsl:call-template name="rdfAtts"/><xsl:apply-templates /></seeAlso></xsl:template>

<xsl:template name="rdfAtts">
<xsl:if test="@rdf:resource"><xsl:attribute name="href"><xsl:value-of select="@rdf:resource"/></xsl:attribute></xsl:if>
<xsl:if test="@rdf:ID"><xsl:attribute name="ID"><xsl:value-of select="@rdf:ID"/></xsl:attribute></xsl:if>
</xsl:template>


<!--RSS bits-->

<xsl:template match="rss:channel"><rss:channel><xsl:call-template name="hrefAtts"/><xsl:apply-templates /></rss:channel></xsl:template>


<!--Dublin Core bits-->

<xsl:template match="dc:title"><dc:title><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:title></xsl:template>

<xsl:template match="dc:creator"><dc:creator><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:creator></xsl:template>

<xsl:template match="dc:subject"><dc:subject><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:subject></xsl:template>

<xsl:template match="dc:description"><dc:description><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:description></xsl:template>

<xsl:template match="dc:publisher"><dc:publisher><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:publisher></xsl:template>

<xsl:template match="dc:contributor"><dc:contributor><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:contributor></xsl:template>

<xsl:template match="dc:date"><dc:date><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:date></xsl:template>

<xsl:template match="dc:type"><dc:type><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:type></xsl:template>

<xsl:template match="dc:format"><dc:format><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:format></xsl:template>

<xsl:template match="dc:identifier"><dc:identifier><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:identifier></xsl:template>

<xsl:template match="dc:source"><dc:source><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:source></xsl:template>

<xsl:template match="dc:language"><dc:language><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:language></xsl:template>

<xsl:template match="dc:relation"><dc:relation><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:relation></xsl:template>

<xsl:template match="dc:coverage"><dc:coverage><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:coverage></xsl:template>

<xsl:template match="dc:rights"><dc:rights><xsl:call-template name="dcAtts"/><xsl:apply-templates /></dc:rights></xsl:template>

<xsl:template name="dcAtts">
<xsl:if test="@rdf:resource"><xsl:attribute name="href"><xsl:value-of select="@rdf:resource"/></xsl:attribute></xsl:if>
<xsl:if test="@rdf:ID"><xsl:attribute name="ID"><xsl:value-of select="@rdf:ID"/></xsl:attribute></xsl:if>
</xsl:template>


<!--WOT bits-->

<xsl:template match="wot:PubKey"><wot:PubKey><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:PubKey></xsl:template>

<xsl:template match="wot:User"><wot:User><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:User></xsl:template>

<xsl:template match="wot:SigEvent"><wot:SigEvent><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:SigEvent></xsl:template>

<xsl:template match="wot:Endorsement"><wot:Endorsement><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:Endorsement></xsl:template>

<xsl:template match="wot:hex_id"><wot:hex_id><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:hex_id></xsl:template>

<xsl:template match="wot:length"><wot:length><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:length></xsl:template>

<xsl:template match="wot:fingerprint"><wot:fingerprint><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:fingerprint></xsl:template>

<xsl:template match="wot:identity"><wot:identity><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:identity></xsl:template>

<xsl:template match="wot:signed"><wot:signed><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:signed></xsl:template>

<xsl:template match="wot:sigdate"><wot:sigdate><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:sigdate></xsl:template>

<xsl:template match="wot:signerLongHexID"><wot:signerLongHexID><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:signerLongHexID></xsl:template>

<xsl:template match="wot:pubkeyAddress"><wot:pubkeyAddress><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:pubkeyAddress></xsl:template>

<xsl:template match="wot:assurance"><wot:assurance><xsl:call-template name="wotAtts"/><xsl:apply-templates /></wot:assurance></xsl:template>

<xsl:template name="wotAtts">
<xsl:if test="@rdf:resource"><xsl:attribute name="href"><xsl:value-of select="@rdf:resource"/></xsl:attribute></xsl:if>
<xsl:if test="@rdf:ID"><xsl:attribute name="ID"><xsl:value-of select="@rdf:ID"/></xsl:attribute></xsl:if>
</xsl:template>






<xsl:template match="*">      <!--preserve unknown components-->    
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
    