<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:dcterms="http://purl.org/dc/terms/"
	xmlns:foaf="http://xmlns.com/foaf/0.1/"
	xmlns="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="rdf rdfs dc dcterms foaf"
>
	<xsl:output method="xml" indent="yes" encoding="utf-8" media-type="application/xhtml+xml" doctype-public="-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" doctype-system="http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd"/>
	
	<xsl:template match="/">
		<xsl:apply-templates select="rdf:RDF"/>
	</xsl:template>
	
	<xsl:template match="rdf:RDF">
		<html>
			<xsl:call-template name="lang"/>
			<head>
				<title>
					<xsl:choose>
						<xsl:when test="foaf:PersonalProfileDocument/dc:title">
							<xsl:value-of select="foaf:PersonalProfileDocument/dc:title"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="xml:lang">
								<xsl:text>en</xsl:text>
							</xsl:attribute>
							<xsl:text>FOAF</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</title>
				<xsl:if test="foaf:PersonalProfileDocument/dc:description">
					<meta name="description">
						<xsl:attribute name="content">
							<xsl:value-of select="foaf:PersonalProfileDocument/dc:description"/>
						</xsl:attribute>
					</meta>
				</xsl:if>
				<xsl:choose>
					<xsl:when test="*[@rdf:about = /rdf:RDF/foaf:PersonalProfileDocument/foaf:maker/@rdf:resource]/foaf:name">
						<meta name="author">
							<xsl:attribute name="content">
								<xsl:value-of select="*[@rdf:about = /rdf:RDF/foaf:PersonalProfileDocument/foaf:maker/@rdf:resource]/foaf:name"/>
							</xsl:attribute>
						</meta>
					</xsl:when>
					<xsl:when test="*[@rdf:about = /rdf:RDF/foaf:PersonalProfileDocument/foaf:maker/@rdf:resource]/foaf:nick">
						<meta name="author">
							<xsl:attribute name="content">
								<xsl:value-of select="*[@rdf:about = /rdf:RDF/foaf:PersonalProfileDocument/foaf:maker/@rdf:resource]/foaf:nick"/>
							</xsl:attribute>
						</meta>
					</xsl:when>
					<xsl:when test="foaf:PersonalProfileDocument/dc:creator/foaf:Person/foaf:name">
						<meta name="author">
							<xsl:attribute name="content">
								<xsl:value-of select="foaf:PersonalProfileDocument/dc:creator/foaf:Person/foaf:name"/>
							</xsl:attribute>
						</meta>
					</xsl:when>
					<xsl:when test="foaf:PersonalProfileDocument/dc:creator">
						<meta name="author">
							<xsl:attribute name="content">
								<xsl:value-of select="foaf:PersonalProfileDocument/dc:creator"/>
							</xsl:attribute>
						</meta>
					</xsl:when>
				</xsl:choose>
			</head>
			<body>
				<h1>
					<xsl:choose>
						<xsl:when test="foaf:PersonalProfileDocument/dc:title">
							<xsl:value-of select="foaf:PersonalProfileDocument/dc:title"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:attribute name="xml:lang">
								<xsl:text>en</xsl:text>
							</xsl:attribute>
							<abbr title="Friend of a Friend">
								<xsl:text>FOAF</xsl:text>
							</abbr>
						</xsl:otherwise>
					</xsl:choose>
				</h1>
				<xsl:apply-templates select="foaf:Person"/>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="foaf:Person">
		<dl>
			<xsl:call-template name="lang"/>
			<xsl:apply-templates select="*[local-name() != 'knows']"/>
		</dl>
		<xsl:if test="foaf:knows">
			<h2>and I know ...</h2>
			<ul>
				<xsl:call-template name="lang"/>
				<xsl:apply-templates select="foaf:knows"/>
			</ul>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="foaf:knows">
		<li>
			<xsl:call-template name="lang"/>
			<xsl:apply-templates select="foaf:Person"/>
		</li>
	</xsl:template>
	
	<xsl:template match="*">
		<dt>
			<code>
				<xsl:value-of select="local-name()"/>
			</code>
		</dt>
		<dd>
			<xsl:choose>
				<xsl:when test="attribute::rdf:resource">
					<a href="{attribute::rdf:resource}">
						<xsl:choose>
							<xsl:when test="attribute::dc:title">
								<xsl:value-of select="attribute::dc:title"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="attribute::rdf:resource"/>
							</xsl:otherwise>
						</xsl:choose>
					</a>
					<xsl:if test="attribute::dc:language">
						<xsl:value-of select="concat(' (', attribute::dc:language, ')')"/>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="lang"/>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</dd>
	</xsl:template>
	
	<xsl:template name="lang">
		<xsl:if test="attribute::xml:lang">
			<xsl:attribute name="xml:lang">
				<xsl:value-of select="attribute::xml:lang"/>
			</xsl:attribute>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>

