<?xml-stylesheet href="/parts/xsltdoc.xsl" type="text/xsl" media="screen"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY classnames "concat('|',@class,'|')">
<!ENTITY localnames "concat('|',local-name(),'|')">
<!ENTITY articleclass "'|article|entry|blogentry|entryItem|'">
<!ENTITY dcwho "|creator|publisher|contributor|">
<!ENTITY dcboth "coverage|description|rights|">
<!ENTITY dcclass "'&dcwho;&dcboth;date|format|identifier|title|author|'">
<!ENTITY dcrel "'|&dcboth;relation|source|subject|'">
<!ENTITY dctrel "'|references|isPartOf|hasPart|'">
<!ENTITY coreblockelt "|p|div|dd|li|">
<!ENTITY core-and-tbl "&coreblockelt;table|">
<!ENTITY listelt "ul|ol|dl|">
<!ENTITY blockelt "'&coreblockelt;&listelt;address|blockquote|td|th|'">
<!ENTITY hblockelt "h:p|h:div|h:dd|h:ul|h:ol|h:li|h:dl|h:address|h:blockquote|h:table">
<!ENTITY inlineelt "'|a|em|strong|cite|abbr|var|span|dt|caption'">
<!ENTITY hinlineelt "h:a|h:em|h:strong|h:dfn|h:cite|h:abbr|h:var|h:span|h:dt">
<!ENTITY rdfns "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="&rdfns;"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:h="http://www.w3.org/1999/xhtml"
  xmlns:dcterms="http://purl.org/dc/terms/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  xmlns:wn="http://xmlns.com/wordnet/1.6/"
  xmlns:cc="http://web.resource.org/cc/"
  xmlns:kw="http://purl.org/net/ns/kw-concept#"
  xmlns:doas="http://purl.org/net/ns/doas#"
  exclude-result-prefixes="h doas"
>
 <xsl:variable name="_doas" select="document('')//rdf:Description[1]"/>

 <xsl:template name="_doas_description">
  <rdf:RDF xmlns="http://purl.org/net/ns/doas#">
   <rdf:Description rdf:about="">
    <title>Metadata extractor stylesheet</title>
    <description>This stylesheet is designed to extract metadata from XHTML, especially with GRDDL. Can be used with http://purl.org/net/ns/metaprof as a profile URI.</description>
    <description xml:lang="ja">XHTMLの要素型とclass, rel属性値を利用し、GRDDLによってメタデータRDF/XMLを抽出するためのスタイルシートです。head要素のprofile属性にhttp://purl.org/net/ns/metaprofを記述するだけで利用できます。</description>
    <author rdf:parthType="Resource">
     <name>Masahide Kanzaki</name>
     <mbox rdf:resource="mailto:webmaster@kanzaki.com"/>
    </author>
    <created>2004-03-03</created>
    <release rdf:parthType="Resource">
     <revision>2.2.3</revision>
     <created>2007-08-19</created>
    </release>
    <rights>(c) 2004-2007 by the author, copyleft under GPL</rights>
    <license rdf:resource="http://creativecommons.org/licenses/GPL/2.0/"/>
   </rdf:Description>
  </rdf:RDF>
 </xsl:template>

 <xsl:output method="xml" media-type="application/xml" indent="yes"/>
 <xsl:param name="xmlfile"/>
 <!-- basic variables of XML NS uris and base uri -->
 <xsl:variable name="schemas" select="/h:html/h:head/h:link[starts-with(@rel,'schema.')]"/>
 <xsl:variable name="myns" select="document('')/xsl:stylesheet/namespace::*"/>
 <xsl:variable name="baseuri">
  <xsl:choose>
   <xsl:when test="/h:html/h:head/h:base"><xsl:value-of select="/h:html/h:head/h:base/@href"/></xsl:when>
   <xsl:when test="/h:html/h:body/h:address//h:cite[@id='orguri']"><xsl:value-of select="/h:html/h:body/h:address//h:cite[@id='orguri']"/></xsl:when>
   <xsl:when test="$xmlfile"><xsl:value-of select="$xmlfile"/></xsl:when>
  </xsl:choose>
 </xsl:variable>
 <!--
 <xsl:template match="/">
  <xsl:apply-templates select="h:html"/>
 </xsl:template>
-->
 <xsl:template match="/h:html">
  <!--** Main XHTML document handler template. This stylesheet can extract metadata form predefined class, rel attributes and schema prefixed class, rel attributes.-->
  <rdf:RDF>
   <xsl:comment>This RDF is generated from XHTML via GRDDL</xsl:comment>
   <rdf:Description rdf:about="">
    <xsl:if test="$baseuri != ''">
     <xsl:attribute name="xml:base"><xsl:value-of select="$baseuri"/></xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="h:head"/>
    <xsl:apply-templates select="h:body"/>
   </rdf:Description>
  </rdf:RDF>
 </xsl:template>

 
 <!-- . . . . . metadata from XHTML head element . . . . . -->
 
 <xsl:template match="h:head">
  <!--** Get basic document metadata from <title>, <meta> and <link> elements -->
  <dc:title><xsl:value-of select="normalize-space(h:title)"/></dc:title>
  <xsl:apply-templates select="h:meta"/>
  <xsl:apply-templates select="h:link"/>
  <!--@ setup author's metadata (ページ作者に関するメタデータを取得) -->
  <xsl:call-template name="who"/>
 </xsl:template>

 <xsl:template match="h:meta">
  <!--** Meta elements with a predefined name. Steps: -->
  <xsl:choose>
   <!--@ if @name is schema prefixed, resolve ns, and generate a property ('.'を含む名前のときは名前空間を解決してからプロパティを生成) -->
   <xsl:when test="contains(@name,'.')">
    <xsl:variable name="ns">
     <xsl:call-template name="find-ns">
      <xsl:with-param name="pfx" select="substring-before(@name,'.')"/>
     </xsl:call-template>
    </xsl:variable>
    <xsl:element name="{translate(@name,'.',':')}" namespace="{$ns}">
     <xsl:value-of select="@content"/>
    </xsl:element>
   </xsl:when>
   <!--@ else if @name='description', dc:description (descriptionからはdc:descriptionを) -->
   <xsl:when test="@name='description'">
    <dc:description><xsl:value-of select="@content"/></dc:description>
   </xsl:when>
   <!--@ else if @name='keywords', call dcsubject template to generate appropriate dc:subject (keywordsについてはテンプレートdcsubjectを呼び出して適切なプロパティを) -->
   <xsl:when test="@name='keywords'">
    <xsl:call-template name="proc-keyword">
     <xsl:with-param name="s" select="@content"/>
    </xsl:call-template>
   </xsl:when>
  </xsl:choose>
  <!--@ note: @name='author' will be treated by template who, not here (注：authorは別のテンプレートで)  -->
 </xsl:template>

 <xsl:template match="h:link">
  <!--** Link elements with a predefined relation. Steps: -->
  <xsl:choose>
   <!--@ if @rel='copyright', generates dc:rights (copyrightはdc:rightsに) -->
   <xsl:when test="@rel='copyright'">
    <dc:rights>
     <xsl:attribute name="rdf:resource"><xsl:value-of select="@href"/></xsl:attribute>
    </dc:rights>
   </xsl:when>
   <!--@ else if @rel='alternate', then (alternateのときは): -->
   <xsl:when test="@rel='alternate'">
    <xsl:choose>
     <!--@ _ if @hreflang presents, generates dcterms:hasVersion (@hreflanがあればdcterms:hasVersion) -->
     <xsl:when test="@hreflang">
      <dcterms:hasVersion>
       <xsl:attribute name="rdf:resource"><xsl:value-of select="@href"/></xsl:attribute>
       <xsl:attribute name="dc:lang"><xsl:value-of select="@hreflang"/></xsl:attribute>
      </dcterms:hasVersion>
     </xsl:when>
     <!--@ _ if @type contains '+xml', generates rdfs:seeAlso (@typeが'+xml'含むときはrdfs:seeAlso) -->
     <xsl:when test="contains(@type,'+xml')">
      <rdfs:seeAlso>
       <xsl:attribute name="rdf:resource"><xsl:value-of select="@href"/></xsl:attribute>
       <xsl:if test="@title">
        <xsl:attribute name="rdfs:label"><xsl:value-of select="@title"/></xsl:attribute>
       </xsl:if>
      </rdfs:seeAlso>
     </xsl:when>
    </xsl:choose>
   </xsl:when>
   <!--@ else if @rel='meta' and @type='application/rdf+xml' and not(@title='FOAF'), generates rdfs:seeAlso (@title='FOAF' will be treated by template who) (metaで@type='application/rdf+xml'でかつ@titleが'FOAF'でないときはrdfs:seeAlso) -->
   <xsl:when test="@rel='meta' and @type='application/rdf+xml' and not(@title='FOAF')">
    <rdfs:seeAlso>
     <xsl:attribute name="rdf:resource"><xsl:value-of select="@href"/></xsl:attribute>
     <xsl:if test="@title">
      <xsl:attribute name="rdfs:label"><xsl:value-of select="@title"/></xsl:attribute>
     </xsl:if>
    </rdfs:seeAlso>
   </xsl:when>
   <!--@ otherwise, call proc-rel -->
   <xsl:otherwise>
    <xsl:call-template name="proc-rel">
     <xsl:with-param name="rel" select="@rel"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
  <!--@ note: @rev='made' will be treated by template who, and @rel='schema.pfx' by findns,  not here (他のテンプレートで扱うものもあり)  -->
 </xsl:template>



 <!-- . . . . . proc XHTML body and weblog entry . . . . . -->


 <xsl:template match="h:body">
  <!--** Generates document metadata and finds blocks (topics). Steps:-->
  <!--@ find table of contents -->
  <xsl:call-template name="toc"/>
  <!--@ test if the body has multple weblog entries -->
  <xsl:choose>
   <xsl:when test="count(.//h:div[contains(&articleclass;, &classnames;)]) &gt; 1">
    <!--@ if yes, proc each entry which is a topic of the document (ignoring other blocks) -->
   <xsl:apply-templates select=".//h:div[contains(&articleclass;, &classnames;)]" mode="entry"/>
   </xsl:when>
   <xsl:otherwise>
    <!--@ else, get metadata topics for the document from block level elements -->
    <!--no inline element should present here!-->
    <xsl:apply-templates select="&hblockelt;"/>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:call-template name="refs"/><!--＠＠ in case reflist is defined by @id-->

 </xsl:template>


 <xsl:template match="h:div[contains(&articleclass;, &classnames;)]" mode="entry">
  <!--** Processes an weblog entry block as topic for the document. Steps:-->
  <!--@ generates document property (foaf:topic) and a topic node (rdf:Description) -->
  <foaf:topic>
   <rdf:Description>
    <!--@ finds a title for this entry -->
    <xsl:call-template name="find-title"/>
    <!--@ find metadata from inline constructs for this entry -->
    <xsl:apply-templates select="&hinlineelt;"/>
    <!--＠＠@ treats all external links as dcterms:references (test)
    <xsl:call-template name="find-reflink"/>
     -->
    <!--@ and process nested block level elements -->
    <xsl:apply-templates select="&hblockelt;"/>
   </rdf:Description>
  </foaf:topic>
 </xsl:template>




 <!-- . . . . . metadata from block level elements . . . . . -->

 <xsl:template match="h:*[contains(&blockelt;, &localnames;)]">
  <!--** A common 'catch all' for a blocklevel element -->
  <xsl:apply-templates select="&hinlineelt;"/>
  <xsl:apply-templates select="&hblockelt;"/>
 </xsl:template>

 <xsl:template match="h:table" priority="-0.5">
  <!--** in case a <table> skip tbody,tr and directly goes into cells. Priority is set low in order to process microformats if <table> has class='vcard' etc. -->
  <xsl:apply-templates select="h:th|h:td"/>
 </xsl:template>


 <xsl:template match="h:blockquote[@cite]">
  <!--** blockquote with @cite attribute will be a dcterms:references -->
  <dcterms:references rdf:resource="{@cite}">
   <xsl:if test="@title">
    <xsl:attribute name="dc:title"><xsl:value-of select="@title"/></xsl:attribute>
   </xsl:if>
  </dcterms:references>
  <xsl:apply-templates select="&hinlineelt;"/>
  <xsl:apply-templates select="&hblockelt;"/>
 </xsl:template>


 <xsl:template match="h:*[contains(&blockelt;, &localnames;)][@class]">
  <!--** Block level element with a general @class (一般的な@classを持つブロックレベル要素). Steps: -->
  <xsl:choose>
   <!--@ if the first letter of @class name is capital, generates WordNet Classes (大文字で始まるclass属性値を持つブロックレベル要素をWordNetクラスの型付ノード要素として処理). -->
   <xsl:when test="contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(@class,1,1))">
    <xsl:call-template name="gen-class">
     <xsl:with-param name="attrs" select="concat('wn.',@class)"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:when test="contains(@class,'.')">
   <!--@ if schema prefixed @class name (ピリオドを含むclass属性値なら): -->
    <xsl:choose>
     <!--@ _ if the class name starts with capital letter, then RDF Class (typed node) -->
     <xsl:when test="contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(substring-after(@class,'.'),1,1))">
      <xsl:call-template name="gen-class">
       <xsl:with-param name="attrs" select="@class"/>
      </xsl:call-template>
     </xsl:when>
     <!--@ _ otherwise RDF Property -->
     <xsl:otherwise>
      <xsl:call-template name="gen-prop">
       <xsl:with-param name="props" select="@class"/>
      </xsl:call-template>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when>
   <!--@ otherwise, process children without creating a node (いずれも一致しなければノードは作らずに子要素を処理) -->
   <xsl:otherwise>
    <xsl:apply-templates select="&hinlineelt;"/>
    <xsl:apply-templates select="&hblockelt;"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>


 <!-- . . . . . metadata from microformats . . . . . -->

 <xsl:template match="h:*[contains('&core-and-tbl;', &localnames;)][@class='vcard']">
  <!--** for hCard, generates foaf:Person as foaf:topic, and calls template hcard-elt -->
  <foaf:topic>
   <foaf:Person >
    <xsl:call-template name="hcard-elt"/>
   </foaf:Person>
  </foaf:topic>
 </xsl:template>

 <xsl:template match="h:*[contains('&core-and-tbl;', &localnames;)][@class='vevent']">
  <!--** for hCalendar, generates foaf:topic, and calls template proc-hcal -->
  <foaf:topic>
   <xsl:call-template name="proc-hcal"/>
  </foaf:topic>
 </xsl:template>

 <xsl:template match="h:*[contains('&core-and-tbl;', &localnames;)][@class='hreview']">
  <!--** for hReview, generates foaf:topic, and calls template proc-hreview -->
  <foaf:topic>
   <xsl:call-template name="proc-hreview"/>
  </foaf:topic>
 </xsl:template>

 <!-- microformats will be processed by imported templates -->
 <xsl:include href="./micro-model.xsl"/>



 <!-- . . . . . metadata from inline elements . . . . . -->

 <xsl:template match="*[contains(&inlineelt;, &localnames;)]">
  <!--** catch all for inline element (to process nested elements) -->
  <xsl:apply-templates select="&hinlineelt;"/>
 </xsl:template>

 <xsl:template match="*[contains(&inlineelt;, &localnames;)][@class][not(@rel)]">
  <!--** Gets Property from general schema prefixed @class of an inline element -->
  <xsl:call-template name="gen-prop">
   <xsl:with-param name="props" select="@class"/>
  </xsl:call-template>
 </xsl:template>



 <!-- . . . . . metadata from predefined classes . . . . . -->

 <xsl:template match="h:*[@class='me']" mode="who">
  <!--** Special paragraph that talks about the author of the document -->
<!--
  <xsl:apply-templates select=".//h:a[contains(@rel,'.')]"/>
  <xsl:apply-templates select=".//*[contains(@class,'.')][not(@rel)]"/>
  <xsl:call-template name="hcard-elt"/>
-->
  <xsl:call-template name="proc-rel">
   <xsl:with-param name="rel" select="@rel"/>
  </xsl:call-template>
  <xsl:call-template name="gen-prop">
   <xsl:with-param name="props" select="@class"/>
  </xsl:call-template>
    <xsl:apply-templates select="&hinlineelt;"/>
    <xsl:apply-templates select="&hblockelt;"/>
 </xsl:template>

 <xsl:template match="h:*[@class='me']">
  <!--** @class='me' should only be processed from within template 'who' -->
 </xsl:template>


 <xsl:template match="h:dfn">
  <!--** DFN is regardes as a keyword of this document -->
  <xsl:call-template name="proc-keyword">
   <xsl:with-param name="s" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="proc-keyword">
  <!--** generates 'linked keywords' (recursively if separated by commas) -->
  <xsl:param name="s"/>
  <xsl:variable name="kw">
   <xsl:choose>
    <xsl:when test="contains($s,',')">
     <xsl:value-of select="substring-before($s,',')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$s"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="rest" select="substring-after($s,',')"/>
  <xsl:call-template name="keyword-elt">
   <xsl:with-param name="term" select="$kw"/>
  </xsl:call-template>
  <xsl:if test="not($rest='')">
   <xsl:call-template name="proc-keyword">
    <xsl:with-param name="s" select="$rest"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>


 <xsl:template name="keyword-elt">
  <!--** an experimental kw:hasKeyword property -->
  <xsl:param name="term"/>
  <xsl:param name="ref"/>
  <xsl:variable name="kwuri" select="concat('http://purl.org/net/ns/keyword/',translate(normalize-space($term),' ','_'))"/>
  <xsl:choose>
   <xsl:when test="$ref=''">
    <kw:hasKeyword rdf:resource="{$kwuri}"/>
   </xsl:when>
   <xsl:otherwise>
    <kw:hasKeyword>
     <kw:Keyword rdf:about="{$kwuri}">
      <dc:relation rdf:resource="{$ref}"/>
     </kw:Keyword>
    </kw:hasKeyword>
   </xsl:otherwise>
  </xsl:choose>
  <!--
  <xsl:variable name="lang">
   <xsl:choose>
    <xsl:when test="lang('ja')">ja</xsl:when>
    <xsl:otherwise>en</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <kw:hasKeyword rdf:parseType="Resource">
   <rdfs:label><xsl:value-of select="$nterm"/></rdfs:label>
   <kw:someSenseDescribedBy rdf:resource="http://{$lang}.wikipedia.org/wiki/{$nterm}"/>
  </kw:hasKeyword>
  -->
 </xsl:template>


 <xsl:template match="h:*[@class='abstract']">
  <!--** Abstract of the content of this document -->
  <dcterms:abstract><xsl:value-of select="normalize-space(.)"/></dcterms:abstract>
  <xsl:apply-templates select="&hinlineelt;"/>
 </xsl:template>



 <xsl:template match="h:ul[@class='rss-items']">
  <!--** Processes general XHTML ul element. -->
  <rss:items xmlns:rss="http://purl.org/rss/1.0/">
   <rdf:Bag>
    <xsl:for-each select="h:li">
     <rdf:li rdf:resource="{h:a/@href}" rdfs:label="{h:a}"/>
    </xsl:for-each>
   </rdf:Bag>
  </rss:items>
 </xsl:template>




 <!-- . . . . . Reference and publication list block . . . . . -->

 <xsl:template match="h:*[contains('|&listelt;', &localnames;)][@class='refs' or @class='bib']">
  <!--** list element as bibliographic reference list. When a list has a class="refs" and children include <cite> elements, then it will be a bibliographic resource list. See also following template rules. (リスト要素がclass="refs"を持ち、子要素内に<cite>要素が含まれていれば、参照文献リストを生成できます。) -->
  <xsl:call-template name="reflist">
   <xsl:with-param name="nodes" select="h:*[h:cite]"/>
   <xsl:with-param name="prop" select="'dcterms:references'"/>
  </xsl:call-template>
 </xsl:template>


 <xsl:template match="h:*[contains('|&listelt;', &localnames;)][@class='pubs']">
  <!--** list element as publication list. When a list has a class="pubs" and children include <cite> elements, then it will be a bibliographic resource list. See also following template rules. (リスト要素がclass="pubs"を持ち、子要素内に<cite>要素が含まれていれば、成果物リストを生成できます。) -->
  <xsl:call-template name="reflist">
   <xsl:with-param name="nodes" select="h:*[h:cite]"/>
   <xsl:with-param name="prop" select="'foaf:made'"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="refs">
  <!--** in case the reference list is defined by @id='references', such as some W3C specs -->
  <xsl:if test="h:*[@id='references']">
   <xsl:call-template name="reflist">
    <xsl:with-param name="nodes" select="h:*[@id='references']/following-sibling::h:dl/h:dd"/>
    <xsl:with-param name="prop" select="'dcterms:references'"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>

 <xsl:template name="reflist">
  <!--** a list member element with a child <cite> is a reference to a bibliographic resource or publication. Steps: -->
  <xsl:param name="nodes"/>
  <xsl:param name="prop"/>
  <xsl:for-each select="$nodes">
   <xsl:element name="{$prop}">
    <rdf:Description>
     <xsl:if test="h:cite/h:a[@href]">
      <!--@ if a <cite> has a child <a>, this is a reference to online resource -->
      <xsl:attribute name="rdf:about">
       <xsl:value-of select="h:cite/h:a/@href"/>
      </xsl:attribute>
     </xsl:if>
     <!--@ a <cite> element will be a dc:title property -->
     <dc:title><xsl:value-of select="normalize-space(h:cite)"/></dc:title>
     <!--@ other elements with class name from DC (i.e. creator, date, etc) will be corresponding dc:* property. Special case: 'author' will be dc:creator. -->
     <xsl:apply-templates select="h:*[@class]"/>
    </rdf:Description>
   </xsl:element>
  </xsl:for-each>
 </xsl:template>







 <!-- . . . . . metadata from rel attributes . . . . . -->

 <xsl:template match="h:a[@rel]">
  <!--** processes <a> element with @rel value. -->
  <!-- call proc-rel to process space separated @rel tokens -->
  <xsl:call-template name="proc-rel">
   <xsl:with-param name="rel" select="@rel"/>
  </xsl:call-template>
  <!-- ＠＠検討74：if it has class='title'
  <xsl:if test="@class='title'">
   <dc:title><xsl:call-template name="get-label"/></dc:title>
  </xsl:if>
  -->
  <xsl:call-template name="gen-prop">
   <xsl:with-param name="props" select="@class"/>
  </xsl:call-template>
 </xsl:template>


 <xsl:template name="proc-rel">
  <!--** processes each of space separated @rel tokens. Steps: -->
  <xsl:param name="rel"/>
  <!--@ set $r as one name from a list of space separated @rel value (空白区切りの属性値リストから最初の1つを取り出して変数$rにセット)-->
  <xsl:variable name="r">
   <xsl:choose>
    <xsl:when test="contains($rel,' ')">
     <xsl:value-of select="substring-before($rel,' ')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$rel"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="rest" select="substring-after($rel,' ')"/>


  <xsl:choose>
   <xsl:when test="$r=''">
   </xsl:when>
   <!--@ License URI -->
   <xsl:when test="$r='license'">
    <cc:license rdf:resource="{@href}"/>
   </xsl:when>

   <!--@ Reference to a topic that the document talks about -->
   <xsl:when test="$r='topic' or $r='foaf.topic'">
    <foaf:topic>
    <!--
     <xsl:attribute name="rdfs:label">
      <xsl:call-template name="get-label"/>
     </xsl:attribute>
     -->
     <xsl:call-template name="proc-prop-body">
      <xsl:with-param name="type" select="@class"/>
     </xsl:call-template>
    </foaf:topic>
   </xsl:when>

   <!--@ authors with URI -->
   <!-- $r=''はテスト済み。実体参照の前後に''をつけることに注意-->
   <xsl:when test="contains('&dcwho;', $r)">
    <xsl:call-template name="gen-prop">
     <xsl:with-param name="props" select="concat('dc.',$r)"/>
     <xsl:with-param name="type" select="'ifp'"/>
    </xsl:call-template>
   </xsl:when>

   <!--@ Dublin Core relations -->
   <xsl:when test="contains(&dcrel;, $r)">
    <xsl:call-template name="gen-prop">
     <xsl:with-param name="props" select="concat('dc.',$r)"/>
    </xsl:call-template>
   </xsl:when>

   <!--@ Dublin Core Terms relations -->
   <xsl:when test="contains(&dctrel;, $r)">
    <xsl:call-template name="gen-prop">
     <xsl:with-param name="props" select="concat('dcterms.',$r)"/>
    </xsl:call-template>
   </xsl:when>

   <!--@ tag as the subject of this document -->
   <xsl:when test="$r='tag'">
    <xsl:call-template name="keyword-elt">
     <xsl:with-param name="term" select="."/>
     <xsl:with-param name="ref" select="@href"/>
    </xsl:call-template>
   </xsl:when>

   <!--@ parent or subsection of this document -->
   <xsl:when test="$r='section' or $r='subsection'">
    <xsl:variable name="p">
     <xsl:choose>
      <xsl:when test="@rel='section'">dcterms:isPartOf</xsl:when>
      <xsl:when test="@rel='subsection'">dcterms:hasPart</xsl:when>
     </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$p}">
     <xsl:attribute name="rdf:resource">
      <xsl:value-of select="@href"/>
     </xsl:attribute>
     <xsl:attribute name="rdfs:label">
      <xsl:call-template name="get-label"/>
     </xsl:attribute>
    </xsl:element>
   </xsl:when>

   <!--@ Reference to other resource this document has some specific relation -->
   <xsl:when test="contains($r, '.')">
    <xsl:call-template name="gen-prop">
     <xsl:with-param name="props" select="$r"/>
     <xsl:with-param name="type" select="@class"/>
    </xsl:call-template>
   </xsl:when>

  </xsl:choose>

  <!--@ If more value(s) in @class list, then process them recursively. (空白で区切られた属性値リストがあれば、2番目以降の値を引数として再起呼び出しを行う)-->
  <xsl:if test="$rest">
   <xsl:call-template name="proc-rel">
    <xsl:with-param name="rel" select="$rest"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>





 <xsl:template name="gen-prop">
  <!--** Generates Property from schema prefixed @class/@rel(再起呼び出ししながら@class/@relからプロパティ要素を生成していく。属性値に'.'を含むときのみ、要素を生成。). Steps: -->
  <xsl:param name="props"/><!--** @class/@rel (@class/@rel属性値)-->
  <xsl:param name="type"/><!--** @class if called from a template for 'a' (さらに<a>要素で@relに加えて@classがある場合はこのパラメータを使う)-->
  
  <!--@ set $p as one name from a list of space separated @class/@rel value (空白区切りの属性値リストから最初の1つを取り出して変数$pにセット)-->
  <xsl:variable name="p">
   <xsl:choose>
    <xsl:when test="contains($props,' ')">
     <xsl:value-of select="substring-before($props,' ')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$props"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="rest" select="substring-after($props,' ')"/>

  <xsl:choose>
   <xsl:when test="$p=''">
   </xsl:when>
  <!--@ if the name is schema prefixed, resolve namespace, ('.'を含む名前の処理を行う。まず名前空間を解決)-->
   <xsl:when test="contains($p,'.')">
    <xsl:variable name="ns">
     <xsl:call-template name="find-ns">
      <xsl:with-param name="pfx" select="substring-before($p,'.')"/>
     </xsl:call-template>
    </xsl:variable>
    <xsl:if test="not($ns='')">
     <!--@ _ if namespace is resolved, then generates a property element (名前空間が解決されたら、要素生成に進む) -->
     <xsl:element name="{translate($p,'.',':')}" namespace="{$ns}">
      <xsl:call-template name="proc-prop-body">
       <xsl:with-param name="type" select="$type"/>
      </xsl:call-template>
     </xsl:element>
    </xsl:if>
   </xsl:when>

   <!--** Processes DC specific class names. Steps: -->
   <xsl:when test="contains(&dcclass;, $p)">
    <xsl:choose>
     <xsl:when test="$p='author'">
      <!--@ if 'author', then literal value of dc:creator. Not this can have multiple authors -->
      <dc:creator><xsl:value-of select="."/></dc:creator>
     </xsl:when>
     <xsl:when test="contains('&dcwho;', &classnames;)">
      <!--@ if 'creator' or 'publisher' or 'contributor', then entity value of corresponding dc:*. Note entity will be foaf:Agent, not foaf:Person. If multiple name presents, it will be interpreted as a foaf:Group.-->
      <xsl:call-template name="gen-entity">
       <xsl:with-param name="prop" select="concat('dc:',$p)"/>
       <xsl:with-param name="type" select="'foaf:Agent'"/>
       <xsl:with-param name="attr">
        <xsl:choose>
         <xsl:when test="contains(.,',')">rdfs:label</xsl:when>
         <xsl:otherwise>foaf:name</xsl:otherwise>
        </xsl:choose>
       </xsl:with-param>
      </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
      <!--@ otherwise, simple literal property of dc:*. -->
      <xsl:element name="{concat('dc:', $p)}">
       <xsl:call-template name="get-label"/>
      </xsl:element>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:when><!-- end contains(&dcclass;, $p)-->
   
   <xsl:when test="$p='subject'">
    <!--** 'subject' is treated as keyword for @class (cf. dc:subject if @rel) -->
    <xsl:call-template name="keyword-elt">
     <xsl:with-param name="term" select="."/>
    </xsl:call-template>
   </xsl:when>

   <xsl:when test="$p='created' or $p='modified'">
    <!--** Creation or modification date of this document -->
    <xsl:element name="dcterms:{$p}">
     <xsl:value-of select="substring(.,1,10)"/>
    </xsl:element>
   </xsl:when>

 </xsl:choose>

  <!--@ If more value(s) in @class list, then process them recursively. (空白で区切られた属性値リストがあれば、2番目以降の値を引数として再起呼び出しを行う)-->
  <xsl:if test="$rest">
   <xsl:call-template name="gen-prop">
    <xsl:with-param name="props" select="$rest"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>





 <xsl:template name="proc-prop-body">
<!--** プロパティノードの属性や子要素を処理する-->
  <xsl:param name="type"/>
  <xsl:if test="@xml:lang">
   <xsl:attribute name="xml:lang"><xsl:value-of select="@xml:lang"/></xsl:attribute>
  </xsl:if>
  <xsl:choose>
   <!--@ __ if no @href, a literal property (@hrefがないときはリテラルプロパティ、@hrefがあれば、さらにノード要素/属性も生成する。). -->
   <xsl:when test="not(@href)">
    <xsl:call-template name="get-label"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:choose>
     <!--@ __ if the element is 'a' and has pfxed name for @class, it is a typed node. call gen-class-elt template. (<a>要素から呼ばれるときの$typeに'.'が含まれていれば、型付要素ノードにする) -->
     <xsl:when test="contains($type,'.')">
      <xsl:call-template name="gen-class-elt">
       <xsl:with-param name="c" select="$type"/>
      </xsl:call-template>
     </xsl:when>
     <!--@ __ else if elt is 'a' and has @class='ifp', it is foaf:homepage ($typeが'ifp'のときは、URIをfoaf:homepageの値にして逆関数プロパティをつくる). -->
     <xsl:when test="contains($type,'ifp')">
      <rdf:Description>
       <foaf:homepage rdf:resource="{@href}"/>
       <rdfs:label><xsl:call-template name="get-label"/></rdfs:label>
      </rdf:Description>
     </xsl:when>
     <!--@ __ otherwise, rdf:resource to @href with literal as rdfs:label. (それ以外の目的語は、rdf:resourceでノード属性として表現する。) -->
     <xsl:otherwise>
     <!--
      <xsl:attribute name="rdf:resource"><xsl:value-of select="@href"/></xsl:attribute>
      <xsl:attribute name="rdfs:label"><xsl:call-template name="get-label"/></xsl:attribute>
      -->
      <rdf:Description rdf:about="{@href}">
       <xsl:if test="not($type='title')"><!-- ＠＠検討74：@class='title'のときは親ノードのタイトル-->
        <rdfs:label><xsl:call-template name="get-label"/></rdfs:label>
       </xsl:if>
      </rdf:Description>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>






 <!-- . . . . . named templates for specific purposes . . . . . -->

 <xsl:template name="get-label">
  <!--** get a label of a resource, from @title if presents, otherwise from content of the element -->
  <xsl:choose>
   <xsl:when test="@title"><xsl:value-of select="@title"/></xsl:when>
   <xsl:otherwise><xsl:value-of select="normalize-space(.)"/></xsl:otherwise>
  </xsl:choose>
 </xsl:template>


 <xsl:template name="find-title">
  <!--** Tries to find a title for a weblog entry. Steps:-->
  <xsl:variable name="title">
   <xsl:choose>
    <!--@ If an <h2> found in the block, first of it will be a title, -->
    <xsl:when test="h:h2">
     <xsl:value-of select="h:h2"/>
    </xsl:when>
    <!--@ else if an <h3> found in the block, first of it will be a title, -->
    <xsl:when test="h:h3">
     <xsl:value-of select="h:h3"/>
    </xsl:when>
    <!--@ else try to find a nearest preceding <h3>, -->
    <xsl:when test="preceding-sibling::h:h3">
     <xsl:value-of select="preceding-sibling::h:h3[1]"/>
    </xsl:when>
    <!--@ finally try to find a nearest preceding <h2>. -->
    <xsl:when test="preceding-sibling::h:h2">
     <xsl:value-of select="preceding-sibling::h:h2[1]"/>
    </xsl:when>
   </xsl:choose>
  </xsl:variable>
  <!--@ If any title found, then dc:title. -->
  <xsl:if test="not($title='')">
   <dc:title><xsl:value-of select="$title"/></dc:title>
  </xsl:if>
 </xsl:template>

 <xsl:template name="find-reflink">
  <!--** generates dcterms:references for all links within an weblog entry -->
  <xsl:for-each select=".//h:a">
   <xsl:choose>
    <xsl:when test="@class='permalink'">
     <dcterms:hasVersion rdf:resource="{@href}"/>
    </xsl:when>
    <xsl:when test="starts-with(@href,'http:')">
     <dcterms:references rdf:resource="{@href}">
      <xsl:attribute name="rdfs:label">
       <xsl:call-template name="get-label"/>
      </xsl:attribute>
     </dcterms:references>
    </xsl:when>
   </xsl:choose>
  </xsl:for-each>
 </xsl:template>

 <xsl:template name="toc">
  <!--** A table of contents of this document. Steps: -->
  <xsl:choose>
   <!--@ If an <ul> with class='toc' or id='toc', use <li>'s as param for gen-toc -->
   <xsl:when test=".//h:ul[@class='toc' or @id='toc']">
    <xsl:call-template name="gen-toc">
     <xsl:with-param name="li" select=".//h:ul[@class='toc']/h:li"/>
    </xsl:call-template>
   </xsl:when>
   <!--@ otherwise, use <h2>'s in the document as param for gen-toc -->
   <xsl:otherwise>
    <xsl:call-template name="gen-toc">
     <xsl:with-param name="li" select=".//h:h2"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="gen-toc">
  <!--** Generates a MARC field 505 style TOC of this document as dcterms:tableOfContents -->
  <xsl:param name="li"/>
  <xsl:variable name="nc" select="count($li)"/>
  <xsl:variable name="toc">
   <xsl:for-each select="$li">
    <xsl:value-of select="h:img/@alt"/>
    <xsl:value-of select="normalize-space(.)"/>
    <!-- MARC field 505 style list -->
    <xsl:choose>
     <xsl:when test="position()=$nc"> .</xsl:when>
     <xsl:otherwise> -- </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:variable>
  <xsl:if test="not($toc='')">
   <dcterms:tableOfContents>
    <xsl:copy-of select="$toc"/>
   </dcterms:tableOfContents>
  </xsl:if>
 </xsl:template>





 <xsl:template name="gen-entity">
  <!--** Generates entity-relashionship style model from a literal element -->
  <xsl:param name="prop"/>
  <xsl:param name="type"/>
  <xsl:param name="attr"/>
  <xsl:element name="{$prop}">
   <xsl:element name="{$type}">
    <xsl:element name="{$attr}">
     <xsl:call-template name="get-label"/>
    </xsl:element>
    <xsl:if test="h:a/@href">
     <foaf:homepage rdf:resource="{h:a/@href}"/>
    </xsl:if>
   </xsl:element>
  </xsl:element>
 </xsl:template>




 <xsl:template name="gen-class">
  <!--** Generates Class from schema prefixed @class. ('.'を含む属性値からRDFのクラスを生成する) Steps: -->
  <xsl:param name="attrs"/>
  <xsl:param name="id"/>
  <!--@ get one name from a list of space separated @class value (空白区切りの属性値リストから最初の1つを取り出して変数$cにセット) -->
  <xsl:variable name="c">
   <xsl:choose>
    <xsl:when test="contains($attrs,' ')">
     <xsl:value-of select="substring-before($attrs,' ')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$attrs"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <xsl:variable name="rest" select="substring-after($attrs,' ')"/>

  <xsl:choose>
   <!--@ If the name is schema prefixed, resolve namespace,  ('.'を含む名前ならば) -->
   <xsl:when test="contains($c,'.')">
    <!--@ _ then generates a Class element as a child of foaf:topic, by calling gen-class-elt (foaf:topicをプロパティ要素とし、その中にクラス要素を作るためにgen-class-eltを呼び出す): -->
    <foaf:topic>
     <xsl:call-template name="gen-class-elt">
      <xsl:with-param name="c" select="$c"/>
     </xsl:call-template>
    </foaf:topic>
   </xsl:when>
  <!--@ Otherwise, if more value(s) in @class list, then process them recursively (そうでなければ、2番目以降を引数に再起呼び出しを行う). -->
   <xsl:otherwise>
    <xsl:if test="$rest">
     <xsl:call-template name="gen-class">
      <xsl:with-param name="attrs" select="$rest"/>
     </xsl:call-template>
    </xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>



 <xsl:template name="gen-class-elt">
  <!--** generates appropriate typed node element. ('.'を含む名前から型付ノード要素を生成する) Steps: -->
  <xsl:param name="c"/>
  <!--@ find namespace by calling find-ns template (まず名前空間を解決). -->
  <xsl:variable name="ns">
   <xsl:call-template name="find-ns">
    <xsl:with-param name="pfx" select="substring-before($c,'.')"/>
   </xsl:call-template>
  </xsl:variable>
  <!--@ then generate a node by calling typed-or-untyped template (typed-or-untypedを呼んでノード要素を生成). -->
  <xsl:call-template name="typed-or-untyped">
   <xsl:with-param name="elt">
    <xsl:choose>
     <xsl:when test="$ns=''">rdf:Description</xsl:when>
     <xsl:otherwise><xsl:value-of select="translate($c,'.',':')"/></xsl:otherwise>
    </xsl:choose>
   </xsl:with-param>
   <xsl:with-param name="ns">
    <xsl:choose>
     <xsl:when test="$ns=''">&rdfns;</xsl:when>
     <xsl:otherwise><xsl:value-of select="$ns"/></xsl:otherwise>
    </xsl:choose>
   </xsl:with-param>
  </xsl:call-template>
 </xsl:template>


 <xsl:template name="typed-or-untyped">
  <!--** generates typed or untyped node element. (名前空間が解決されていれば型付ノード要素を、そうでなければrdf:Descriptionを生成する) Steps: -->
  <xsl:param name="elt"/>
  <xsl:param name="ns"/>
  <!--@ generate a typed or untyped node element. -->
  <xsl:element name="{$elt}" namespace="{$ns}">
   <!--@ if subject URI presents, add rdf:about. -->
   <xsl:if test="@href">
    <xsl:attribute name="rdf:about"><xsl:value-of select="@href"/></xsl:attribute>
   </xsl:if>
   <!--@ processes child elements to find properties for this node. -->
   <xsl:apply-templates select="&hinlineelt;"/>
   <xsl:apply-templates select="&hblockelt;"/>
  </xsl:element>
 </xsl:template>
 

 <xsl:template name="find-ns">
  <!--** find appropriate namespace for an element. Steps: -->
  <xsl:param name="pfx"/>
  <xsl:choose>
   <!--@ Try to find prefix:uri mapping from <link> elements in the source document. -->
   <xsl:when test="$schemas[@rel=concat('schema.',$pfx)]">
    <xsl:value-of select="$schemas[@rel=concat('schema.',$pfx)]/@href"/>
   </xsl:when>
   <!--@ or, if the prefix is used in this stylesheet (e.g. dc:, dcterms:, foaf:, wn:, etc.), use it. -->
   <xsl:when test="$myns[name()=$pfx]">
    <xsl:value-of select="$myns[name()=$pfx]"/>
   </xsl:when>
  </xsl:choose>
 </xsl:template>



 <xsl:template name="who">
  <!--** Processes metadata for the author. Steps: -->
  <!--@ get author's mbox -->
  <xsl:variable name="mbox">
   <xsl:choose>
    <xsl:when test="h:link[@rev='made']"><xsl:value-of select="h:link[@rev='made']/@href"/></xsl:when>
    <xsl:when test="count(..//h:address//h:a[starts-with(@href,'mailto:')])=1"><xsl:value-of select="..//h:address//h:a[starts-with(@href,'mailto:')]/@href"/></xsl:when>
   </xsl:choose>
  </xsl:variable>
  <!--@ get author's name -->
  <xsl:variable name="foafname">
   <xsl:choose>
   <xsl:when test="h:meta[@name='author']">
    <xsl:value-of select="h:meta[@name='author']/@content"/>
   </xsl:when>
   <xsl:when test="h:meta[@name='dc.creator']">
    <xsl:value-of select="h:meta[@name='dc.creator']/@content"/>
   </xsl:when>
   <xsl:when test="$mbox='mailto:webmaster&#64;kanzaki.com'">KANZAKI, Masahide</xsl:when>
   </xsl:choose>
  </xsl:variable>
  <!--@ get author's foaf file location -->
  <xsl:variable name="foafloc">
   <xsl:choose>
    <xsl:when test="h:link[@rel='meta' and @title='FOAF']">
     <xsl:value-of select="h:link[@rel='meta' and @title='FOAF']/@href"/>
    </xsl:when>
    <xsl:when test="$mbox='mailto:webmaster&#64;kanzaki.com'">http://www.kanzaki.com/info/webwho.rdf</xsl:when>
   </xsl:choose>
  </xsl:variable>

  <!--@ if any info on the author found, then generate <foaf:maker> -->
  <xsl:variable name="who">
   <xsl:if test="not($mbox='')">
    <foaf:mbox rdf:resource="{$mbox}"/>
   </xsl:if>
   <xsl:if test="not($foafname='')">
    <foaf:name><xsl:value-of select="$foafname"/></foaf:name>
   </xsl:if>
   <xsl:if test="not($foafloc='')">
    <rdfs:seeAlso rdf:resource="{$foafloc}"/>
   </xsl:if>
   <xsl:apply-templates select="../h:body//h:*[@class='me']" mode="who"/>
  </xsl:variable>

  <xsl:if test="not($who='')">
   <foaf:maker>
    <!-- not necessarily a person -->
    <foaf:Agent rdf:nodeID="who">
     <xsl:copy-of select="$who"/>
    </foaf:Agent>
   </foaf:maker>
  </xsl:if>
  <!--
  <xsl:if test="h:meta[@name='ICBM']">
   <foaf:based_near rdf:parseType="Resource">
    <geo:lat><xsl:value-of select="substring-before(h:meta[@name='ICBM']/@content,',')"/></geo:lat>
    <geo:long><xsl:value-of select="substring-after(h:meta[@name='ICBM']/@content,',')"/></geo:long>
   </foaf:based_near>
  </xsl:if>
  -->
 </xsl:template>

 <xsl:template match="*">
  <!--** Do nothing for any unmatched element -->
 </xsl:template>


</xsl:stylesheet>
