<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    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/">

<xsl:param name="surname-first" select="'false'"/>
<xsl:param name="sha1sum"/>
<xsl:param name="mbox"/>

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

  <xsl:template match="xhtml:html">
    <xsl:variable name="nick">
        <xsl:value-of select="substring-after(//xhtml:h1, 'Personal info for ')"/>
    </xsl:variable>
    <xsl:variable name="home">
        <xsl:value-of select="//xhtml:p[text() = 'Homepage: ']/xhtml:a"/>
    </xsl:variable>
    <xsl:variable name="name">
        <xsl:value-of select="substring-after(//xhtml:p, 'Name: ')"/>
    </xsl:variable>
    <rdf:RDF>
        <rdf:Description rdf:about="">
           <dc:title>FOAF for <xsl:value-of select="$name"/></dc:title>
           <dc:description>Friend-of-a-Friend description for <xsl:value-of select="$name"/></dc:description>
           <dc:creator rdf:resource="#{$nick}" />
        </rdf:Description>

        <foaf:Person rdf:ID="{$nick}">
            <foaf:name><xsl:value-of select="$name"/></foaf:name>
            <xsl:choose>
                <xsl:when test="$surname-first = 'false'">
                    <foaf:firstName><xsl:value-of select="substring-before($name, ' ')"/></foaf:firstName>
                    <foaf:surname><xsl:value-of select="substring-after($name, ' ')"/></foaf:surname>
                </xsl:when>
                <xsl:otherwise>
                    <foaf:surname><xsl:value-of select="substring-before($name, ' ')"/></foaf:surname>
                    <foaf:firstName><xsl:value-of select="substring-after($name, ' ')"/></foaf:firstName>
                </xsl:otherwise>
            </xsl:choose>

            <foaf:nick><xsl:value-of select="$nick"/></foaf:nick>

            <xsl:if test="$sha1sum">
                <foaf:mbox_sha1sum><xsl:value-of select="$sha1sum"/></foaf:mbox_sha1sum>
            </xsl:if>

            <xsl:if test="$mbox">
                <foaf:mbox rdf:resource="mailto:{$mbox}"/>
            </xsl:if>

            <foaf:weblog rdf:resource="http://www.advogato.org/person/{$nick}/diary.html"/>
            <foaf:homepage rdf:resource="{$home}"/>

            <xsl:for-each select="//xhtml:p[text() = 'This person is:']/following-sibling::xhtml:ul[1]/xhtml:li/xhtml:a">
               <xsl:variable name="proj-home">
                  <xsl:value-of select="document(concat('http://cgi.w3.org/cgi-bin/tidy?docAddr=http://www.advogato.org',@href))//xhtml:p[text() = 'Homepage: ']/xhtml:a"/>                  
               </xsl:variable>
                <foaf:currentProject rdf:resource="{$proj-home}"/>
            </xsl:for-each>

           </foaf:Person>

            <foaf:Document rdf:about="http://www.advogato.org/person/{$nick}">
                <dc:title>Personal info for <xsl:value-of select="$nick"/></dc:title>
                <dc:description>Advogato profile page for <xsl:value-of select="$name"/></dc:description>
                <foaf:topic rdf:resource="#{$nick}"/>
            </foaf:Document>

            <xsl:for-each select="//xhtml:p[text() = 'This person is:']/following-sibling::xhtml:ul[1]/xhtml:li/xhtml:a">
               <xsl:variable name="proj-home">
                  <xsl:value-of select="document(concat('http://cgi.w3.org/cgi-bin/tidy?docAddr=http://www.advogato.org',@href))//xhtml:p[text() = 'Homepage: ']/xhtml:a"/>                  
               </xsl:variable>
                <foaf:Project rdf:about="{$proj-home}">
                    <dc:title><xsl:value-of select="."/></dc:title>
                </foaf:Project>
                <foaf:Document rdf:about="http://www.advogao.org/proj{@href}">
                  <dc:title>Project info for <xsl:value-of select="."/></dc:title>
                  <dc:description>Advogato project description for <xsl:value-of select="."/></dc:description>
                  <foaf:topic rdf:resource="{$proj-home}"/>
                </foaf:Document>
            </xsl:for-each>
    </rdf:RDF>
  </xsl:template>

</xsl:stylesheet>