<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns="http://www.w3.org/1999/xhtml" >

<xsl:output 
  method="html"
  omit-xml-declaration="no" 
  doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
  doctype-system="http://www.w3.org/TR/html4/loose.dtd"
  indent="no"
  media-type="text/html"/>   
  
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="xhtml:html">
    <html lang="ja">
      <head>
        <xsl:apply-templates select="xhtml:head"/>
      </head>
      <body>
        <xsl:if test="xhtml:body/@id">
          <xsl:attribute name="id">
            <xsl:value-of select="xhtml:body/@id"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="xhtml:body/@class">
          <xsl:attribute name="class">
            <xsl:value-of select="xhtml:body/@class"/> 
          </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates select="xhtml:body"/>
      </body>
    </html>
  </xsl:template>

<!-- //////////////////// [ head ] //////////////////// -->
<xsl:template match="xhtml:meta|xhtml:head/xhtml:title|xhtml:head/xhtml:link|xhtml:head/xhtml:style|xhtml:head/xhtml:script">
  <xsl:copy>
    <xsl:for-each select="@*">
      <xsl:copy />
    </xsl:for-each>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<!-- //////////////////// [ body ] //////////////////// -->

<!-- //////////////////// [ HTML 5 ] //////////////////// -->

<xsl:template match="xhtml:section|xhtml:nav|xhtml:article|xhtml:aside|xhtml:hgroup|xhtml:header|xhtml:footer|xhtml:figure|xhtml:figure/xhtml:legend" >
  <xsl:call-template name="tmp_html5">
    <xsl:with-param name="type">block</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="xhtml:mark|xhtml:time|xhtml:progress|xhtml:meter|xhtml:ruby|xhtml:rt|xhtml:rp" >
  <xsl:call-template name="tmp_html5">
    <xsl:with-param name="type">inline</xsl:with-param>
  </xsl:call-template>
</xsl:template>

<!-- //////////////////// [ HTML 4 ] //////////////////// -->

<xsl:template match="xhtml:div|xhtml:h1|xhtml:h2|xhtml:h3|xhtml:h4|xhtml:h5|xhtml:h6|xhtml:p|xhtml:ul|xhtml:ol|xhtml:li|xhtml:dl|xhtml:dt|xhtml:dd|xhtml:table|xhtml:thead|xhtml:tfoot|xhtml:tbody|xhtml:tr|xhtml:th|xhtml:td|xhtml:span|xhtml:a|xhtml:strong|xhtml:em|xhtml:small|xhtml:br|xhtml:img|xhtml:hr|xhtml:legend" >
  <xsl:copy>
    <xsl:for-each select="@*">
      <xsl:copy />
    </xsl:for-each>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<!-- //////////////////// [ template ] //////////////////// -->

<xsl:template name="tmp_html5">
  <xsl:param name="type">inline</xsl:param>
  <xsl:variable name="elementType">
     <xsl:if test="$type='block'">div</xsl:if>
    <xsl:if test="not($type='block')">span</xsl:if>
  </xsl:variable>
  <xsl:element name="{$elementType}">
  
    <xsl:for-each select="@id|@style|@title|@lang|@dir|@onclick|@ondblclick|@onmousedown|@onmouseup|@onmouseover|@onmousemove|@onmouseout|@onkeypress|@onkeydown|@onkeyup|@onload|@onunload|@onfocus|@onblur|@onsubmit|@onreset|@charset|@type|@name|@href|@target|@accesskey|@shape|@coords|@src|@alt|@longdesc|@height|@width|@usemap|@data|@value|@valuetype|@cite|@start|@action|@method|@enctype|@accept|@name">
      <xsl:copy />
    </xsl:for-each>
    
    <xsl:attribute name="class">
      <xsl:if test="@class">
        <xsl:value-of select="./@class" /> 
      </xsl:if>
      <xsl:value-of select="concat(' ',local-name())"/>
    </xsl:attribute>
    
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
