<?xml version="1.0" encoding="UTF-8"?>

<!--
　他人様の foaf の内容をゲットして我が物とするエグい XSL
　SHA1 計算と現在時刻の取得に Xalan 拡張 (JavaScript) を使ってるので注意。 ( s: 接頭辞で始まる関数)
 -->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:s="http://www.remus.dti.ne.jp/~a-satomi/"
	xmlns:js="http://www.remus.dti.ne.jp/~a-satomi/jsfunc"
	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:foaf="http://xmlns.com/foaf/0.1/"
	xmlns:xalan="http://xml.apache.org/xalan"
	exclude-result-prefixes="s js xalan">

<xsl:import href="jsfunc.xsl"/>

<xsl:output method="xml"
	encoding="UTF-8"
	omit-xml-declaration="no"
	indent="yes"
	xalan:indent-amount="2"
	media-type="application/xml"/>

<!-- ================ メイン ================  -->

<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">
		<xsl:text>href="../../common/xsl/foaf2html.xsl" type="text/xsl"</xsl:text>
	</xsl:processing-instruction>

	<rdf:RDF>
		<xsl:copy-of select="rdf:RDF/@xml:lang"/>
		<foaf:Person>
			<xsl:apply-templates select="rdf:RDF/foaf:Person/foaf:*"/>
			<dc:date><xsl:value-of select="js:getTime()"/></dc:date>
		</foaf:Person>
	</rdf:RDF>
</xsl:template>

<!-- ========== 他人様 foaf:mbox は出さないで sha1 値だけにする ========== -->

<xsl:template match="foaf:mbox[not(contains(@rdf:resource, 'a-satomi@remus.dti.ne.jp'))]">
	<xsl:if test="not(../foaf:mbox_sha1sum)">
		<foaf:mbox_sha1sum>
			<xsl:value-of select="js:calcSHA1(string(@rdf:resource))"/>
		</foaf:mbox_sha1sum>
	</xsl:if>
</xsl:template>

<!-- ============== 自力記述の foaf:knows ==============  -->

<xsl:template match="foaf:knows[foaf:Person]">
	<foaf:knows>
		<xsl:apply-templates/>
	</foaf:knows>
</xsl:template>

<!-- ==== 他人様 の foaf の URL リストを展開して内容を取り込む =====  -->

<xsl:template match="foaf:knows[rdf:Bag]">
	<xsl:apply-templates select="rdf:Bag/rdf:li"/>
</xsl:template>

<xsl:template match="rdf:li">
	<xsl:param name="person" select="document(@rdf:resource)/rdf:RDF/foaf:Person"/>
	<xsl:choose>
		<xsl:when test="not($person)">
			<xsl:message>
				<xsl:value-of select="concat('&#x0A;[Nof FOAF DATA] ', @rdf:resource)"/>
			</xsl:message>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message>
				<xsl:value-of select="concat('&#x0A;[Prediating] ', @rdf:resource)"/>
			</xsl:message>
			<foaf:knows>
				<foaf:Person>
					<xsl:apply-templates select="$person/foaf:*[local-name() != 'knows']"/>
					<rdfs:seeAlso rdf:resource="{@rdf:resource}"/>
					<dc:description>
						<xsl:value-of select="@dc:description"/>
					</dc:description>
				</foaf:Person>
			</foaf:knows>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ===========  ビルドインテンプレ変更 (要素自体を出力) ===========  -->

<xsl:template match="foaf:*" priority="-5.0">
	<xsl:element name="foaf:{local-name()}">
		<xsl:apply-templates select="node() | @*"/>
	</xsl:element>
</xsl:template>

<xsl:template match="rdfs:*" priority="-5.0">
	<xsl:element name="rdfs:{local-name()}">
		<xsl:apply-templates select="node() | @*"/>
	</xsl:element>
</xsl:template>

<xsl:template match="dc:*" priority="-5.0">
	<xsl:element name="dc:{local-name()}">
		<xsl:apply-templates select="node() | @*"/>
	</xsl:element>
</xsl:template>


<!-- ただし rdf:resource, dc:title, xml:lang 属性だけをコピー -->

<xsl:template match="@rdf:resource | @dc:title | @xml:lang" priority="-5.0">
	<xsl:copy/>
</xsl:template>

<xsl:template match="@*" priority="-10.0"/>

<!-- ============ インデント制御 =============  -->

<xsl:strip-space elements="*"/>

</xsl:stylesheet>


