<?xml version="1.0" encoding="utf-8"?>
<!--RSS生成用-->
<xsl:stylesheet version="1.0"
 xmlns:xh="http://www.w3.org/1999/xhtml"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:dct="http://purl.org/dc/terms/"
 xmlns:cc="http://web.resource.org/cc/"
 xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#"
 xmlns:foaf="http://xmlns.com/foaf/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:r="http://www.purl.org/stuff/rev#"
 exclude-result-prefixes="xsl xh xhv rdfs">
<xsl:output method="xml"
 indent="yes"/>
<xsl:param name="xmlfile"/>
<xsl:variable name="baseURI">
	<xsl:choose>
		<xsl:when test="/*[@xml:base]">
			<xsl:value-of select="/*[@xml:base]"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$xmlfile"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>


<xsl:template match="/">
	<xsl:apply-templates select="xh:html"/>
</xsl:template>

<xsl:template match="xh:html">
	<rdf:RDF xml:lang="ja" xml:base="{$xmlfile}">
    <foaf:Document rdf:about="">
      <dc:title>
        <xsl:value-of select="xh:head/xh:title"/>
      </dc:title>
      <xsl:for-each select="xh:body">
        <xsl:for-each select="descendant::xh:*[contains(@class,'abstract')]">
          <dct:alternative>
            <xsl:value-of select="text()"/>
          </dct:alternative>
        </xsl:for-each>
        <xsl:for-each select="xh:div[@class='section']">
          <dc:description>
            <xsl:for-each select="xh:p">
              <xsl:value-of select="normalize-space(.)"/>
            </xsl:for-each>
          </dc:description>
          <xsl:for-each select="xh:p/descendant::xh:a">
            <dct:references dc:title="{text()}" rdf:resource="{@href}"/>
          </xsl:for-each>
          <xsl:for-each select="xh:ol[@class='content']/xh:li/xh:a">
            <dct:hasPart dc:title="{text()}" rdf:resource="{@href}"/>
          </xsl:for-each>
          <xsl:for-each select="xh:ul[@class='references']/xh:li/xh:a">
            <dct:references dc:title="{text()}" rdf:resource="{@href}"/>
          </xsl:for-each>
        </xsl:for-each>
        <xsl:for-each select="xh:div[@class != 'section'][contains(@class,'section')][xh:h2[count(@id) !=0]]">
          <dct:hasPart rdf:resource="#{xh:h2/@id}"/>
        </xsl:for-each>
        <xsl:for-each select="/xh:html/xh:body/xh:div[contains(@class,'footer')]/descendant::xh:a[contains(@rel,'license')]">
          <xsl:for-each select="document(@href)/rdf:RDF/cc:Work/cc:license">
          <cc:license rdf:resource="{@rdf:resource}"/>
        </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:for-each select="xh:head">
        <xsl:for-each select="xh:link[contains(@rel,'prev')] | xh:link[contains(@rel,'next')] | xh:link[contains(@rel,'home')] | xh:link[contains(@rel,'contents')] | xh:link[contains(@rel,'index')]">
          <dct:isReferencedBy dc:title="{@title}" rdf:resource="{@href}"/>
        </xsl:for-each>
      </xsl:for-each>
    </foaf:Document>
		
    <xsl:apply-templates select="xh:body"/>
	
  </rdf:RDF>
</xsl:template>

<xsl:template match="xh:body">
	<xsl:for-each select="xh:div[contains(@class,'diary')]|xh:div[contains(@class,'impression')]">
		<xsl:call-template name="entry"/>
	</xsl:for-each>
  <xsl:for-each select="xh:div[contains(@class,'hreview')]">
    <xsl:call-template name="hreview">
      <xsl:with-param name="num">
        <xsl:value-of select="position()"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:for-each>
  

  <xsl:for-each select="xh:div[contains(@class,'vcard')]">
		<xsl:call-template name="maker"/>
	</xsl:for-each>
</xsl:template>

<xsl:template name="entry">
  <xsl:variable name="reviewID" select="xh:h2/@id"/>
	<foaf:Document rdf:ID="{$reviewID}">
		<dc:title>
			<xsl:value-of select="xh:h2"/>
		</dc:title>
<!--		<foaf:maker rdf:resource="#author"/>-->
    <dct:created>
      <dct:W3CDTF>
        <rdf:value>
          <xsl:call-template name="ID2date">
            <xsl:with-param name="targetID" select="$reviewID"/>
          </xsl:call-template>
        </rdf:value>
      </dct:W3CDTF>
    </dct:created>
    <dc:description>
      <xsl:value-of select="normalize-space(xh:div[@class='description'])"/>
    </dc:description>

    <xsl:for-each select="xh:div[contains(@class,'description')]/descendant::xh:dfn">
      <dc:subject>
        <xsl:value-of select="."/>
      </dc:subject>
    </xsl:for-each>
    <xsl:for-each select="xh:div[contains(@class,'description')]/descendant::xh:a">
      <dct:references dc:title="{.}" rdf:resource="{@href}"/>
    </xsl:for-each>
    <xsl:for-each select="xh:ul[contains(@class,'references')]/xh:li/xh:a">
      <dct:references dc:title="{.}" rdf:resource="{@href}"/>
    </xsl:for-each>
    <xsl:for-each select="xh:dl[contains(@class,'about')]/xh:dd/xh:a[contains(@class,'url')]">
      <dct:references dc:title="{.}" rdf:resource="{@href}"/>
    </xsl:for-each>
    <xsl:for-each select="/xh:html/xh:body/xh:div[contains(@class,'footer')]/descendant::xh:a[contains(@rel,'license')]">
      <xsl:for-each select="document(@href)/rdf:RDF/cc:Work/cc:license">
        <cc:license rdf:resource="{@rdf:resource}"/>
      </xsl:for-each>
    </xsl:for-each>

  </foaf:Document>
</xsl:template>

<xsl:template name="hreview">
  <xsl:param name="num"/>
  <xsl:variable name="reviewID" select="xh:h2/@id"/>

  <r:Review rdf:ID="{$reviewID}">
    <dc:title>
      <xsl:value-of select="xh:h2"/>
    </dc:title>
    <r:createdOn>
      <dct:W3CDTF>
        <rdf:value>
          <xsl:call-template name="ID2date">
            <xsl:with-param name="targetID" select="$reviewID"/>
          </xsl:call-template>
        </rdf:value>
      </dct:W3CDTF>
    </r:createdOn>
    <r:text>
      <xsl:value-of select="normalize-space(descendant::xh:div[contains(@class,'description')])"/>
    </r:text>
    <xsl:for-each select="/xh:html/xh:body/xh:div[contains(@class,'footer')]/descendant::xh:a[contains(@rel,'license')]">
      <xsl:for-each select="document(@href)/rdf:RDF/cc:Work/cc:license">
        <cc:license rdf:resource="{@rdf:resource}"/>
      </xsl:for-each>
    </xsl:for-each>
	<xsl:for-each select="/xh:html/xh:body/xh:div[contains(@class,'vcard')]">
	<r:reviewer>
		<foaf:Person>
			<foaf:mbox rdf:resource="{xh:address/xh:a[contains(@rev,'made')]/@href}"/>
		</foaf:Person>
	</r:reviewer>
	</xsl:for-each>
  </r:Review>
  <xsl:for-each select="descendant::xh:dl[contains(@class,'about')]">
    <rdf:Description rdf:ID="review-item{$num}">
      <r:hasReview rdf:resource="#{$reviewID}"/>
      <dc:title>
        <xsl:value-of select="xh:dd[contains(@class,'fn')]"/>
      </dc:title>
      <foaf:page rdf:resource="{xh:dd/xh:a[contains(@class,'url')]/@href}"/>
    </rdf:Description>
  </xsl:for-each>
</xsl:template>


<xsl:template name="maker">
	<foaf:Person>
		<foaf:nick xml:lang="en">
			<xsl:value-of select="xh:address/xh:a[contains(@rev,'made')]"/>
		</foaf:nick>
		<foaf:mbox rdf:resource="{xh:address/xh:a[contains(@rev,'made')]/@href}"/>
		<foaf:isPrimaryTopicOf rdf:resource="{xh:address/xh:a[contains(@rel,'author')][@type='application/rdf+xml']/@href}"/>
		<xsl:for-each select="/xh:html/xh:body">
			<xsl:for-each select="xh:div[contains(@class,'diary')] | xh:div[contains(@class,'impression')] | xh:div[contains(@class,'hreview')]">
				<foaf:made rdf:resource="#{xh:h2/@id}" />
			</xsl:for-each>
		</xsl:for-each>
	</foaf:Person>
 	<foaf:Group>
		<foaf:name xml:lang="en">
			<xsl:value-of select="descendant::xh:*[contains(@class,'org')]"/>
		</foaf:name>
		<foaf:homepage rdf:resource="{descendant::xh:*[contains(@class,'org')]/@href}"/>
		<foaf:member>
			<foaf:Person>
				<foaf:mbox rdf:resource="{xh:address/xh:a[contains(@rev,'made')]/@href}"/>
			</foaf:Person>
		</foaf:member>
	</foaf:Group>
</xsl:template>

<xsl:template name="ID2date">
  <xsl:param name="targetID"/>
    <xsl:value-of select="substring($targetID,2,4)"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="substring($targetID,6,2)"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="substring($targetID,8,2)"/>
    <xsl:text>T</xsl:text>
    <xsl:value-of select="substring($targetID,10,2)"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of select="substring($targetID,12,2)"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of select="substring($targetID,14,2)"/>
    <xsl:text>+09:00</xsl:text>
</xsl:template>


</xsl:stylesheet>

