<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

<xsl:template match="DOC">
      <xsl:apply-templates select="BODY"/>
</xsl:template>

<xsl:template match="BODY">
  <html>
  <head>
    <title><xsl:value-of select="TITLE"/></title>
  </head>

  <body>
    <p align="right"><xsl:value-of select="DOCNO"/></p>
    <xsl:if test="DATE!=''">
      <p align="right">
<!--
        <xsl:value-of select="substring(DATE&#x2C;1&#x2C;2)"/>
        <xsl:value-of select="format-number(substring(DATE&#x2C;3&#x2C;2)&#x2C;'##')"/>
        <xsl:value-of select="substring(DATE&#x2C;5&#x2C;1)"/>
        <xsl:value-of select="format-number(substring(DATE&#x2C;6&#x2C;2)&#x2C;'##')"/>
        <xsl:value-of select="substring(DATE&#x2C;8&#x2C;1)"/>
        <xsl:value-of select="format-number(substring(DATE&#x2C;9&#x2C;2)&#x2C;'##')"/>
        <xsl:value-of select="substring(DATE&#x2C;11&#x2C;1)"/>
-->
        <xsl:value-of select="DATE"/>
      </p>
    </xsl:if>
    <xsl:apply-templates select="TO"/>
    <xsl:apply-templates select="AUTHOR"/>

    <xsl:apply-templates select="TITLE"/>

    <xsl:apply-templates select="MAINTXT"/>
    <xsl:apply-templates select="APPENDIX"/>
    <br/>
    <br/>

    <xsl:apply-templates select="MAINTXT2"/>
    <xsl:apply-templates select="APPENDIX2"/>
    <br/>
    <br/>

    <xsl:apply-templates select="MAINTXT3"/>

  </body>
  </html>    
</xsl:template>

<xsl:template match="TO" >
  <xsl:if test="AFF!='' and NAME!=''">
    <p>
    <xsl:value-of select="AFF" disable-output-escaping="no"/> 
    </p>
  </xsl:if>
  <xsl:if test="NAME!=''">
    <p>
    <xsl:value-of select="NAME" disable-output-escaping="no"/> 
    <xsl:if test="HONORIFC!='' and NAME!=''">
      <xsl:value-of select="concat('　'&#x2C;HONORIFC)" />
    </xsl:if>
    </p>
  </xsl:if>
</xsl:template>

<xsl:template match="AUTHOR" >
  <p align="right">
  <xsl:value-of select="AFF" />
  </p>
  <xsl:if test="NAME!=''">
  <p align="right">
    <xsl:value-of select="NAME" />
  </p>
  </xsl:if>
</xsl:template>

<xsl:template match="TITLE">
  <p align="center">
    <xsl:value-of select="."/>
  </p>
</xsl:template>

<xsl:template match="MAINTXT">
  <xsl:for-each select=".">
    <xsl:apply-templates select="P"/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="MAINTXT2">
  <xsl:for-each select=".">
    <xsl:apply-templates select="P"/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="MAINTXT3">
  <xsl:for-each select=".">
    <xsl:apply-templates select="P"/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="P">
  <p>
    <xsl:value-of disable-output-escaping="yes" select="."/>
  </p>
</xsl:template>

<xsl:template match="APPENDIX">
  <xsl:for-each select=".">
    <xsl:if test="DOCLINK">
      <xsl:apply-templates select="DOCLINK"/>
      <br/>
    </xsl:if>
    <xsl:if test="not(DOCLINK)">
      <xsl:apply-templates select="APPTITLE"/>
      <br/>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template match="APPENDIX2">
  <xsl:for-each select=".">
    <xsl:apply-templates select="DOCLINK"/>
    <br/>
  </xsl:for-each>
</xsl:template>

<xsl:template match="DOCLINK">
  <a>
    <xsl:attribute name="href">
      <xsl:value-of select="." />
    </xsl:attribute>
    <xsl:attribute name="target">_blank</xsl:attribute>
    <xsl:value-of select="../APPTITLE" />
    <xsl:value-of select="@REF" />
  </a>
</xsl:template>

<xsl:template match="APPTITLE">
  <span>
    <xsl:attribute name="style">
      font-weight:bold; text-decoration:underline;
    </xsl:attribute>
    <xsl:value-of select="." />
  </span>
</xsl:template>
</xsl:stylesheet>
