<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.daisy.org/z3986/2005/dtbook/" version="2.0">
<xsl:output omit-xml-declaration="no" encoding="UTF-8" doctype-public="-//NISO//DTD dtbook 2005-1//EN" doctype-system="http://www.daisy.org/z3986/2005/dtbook-2005-1.dtd" indent="yes" method="xml"/>
<xsl:template name="copyAtts">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name()='lang'">
<xsl:if test="not(../@xml:lang)">
<xsl:attribute name="xml:lang">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="http://www.daisy.org/z3986/2005/dtbook/">
<xsl:if test="local-name()!='head' and local-name()!='meta'">
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
</xsl:if>
<xsl:call-template name="copyAtts"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/dtbook">
<dtbook version="2005-1">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name()='version'"/>
<xsl:when test="name()='lang'">
<xsl:if test="not(../@xml:lang)">
<xsl:attribute name="xml:lang">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</dtbook>
</xsl:template>
<xsl:template match="frontmatter|bodymatter|rearmatter">
<xsl:element name="{local-name()}" namespace="http://www.daisy.org/z3986/2005/dtbook/">
<xsl:call-template name="copyAtts"/>
<xsl:for-each-group select="*" group-adjacent="local-name()='level' or local-name()='level1' or local-name()='doctitle' or local-name()='docauthor'">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:for-each select="current-group()">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<level depth="1">
<xsl:for-each select="current-group()">
<xsl:apply-templates select="."/>
</xsl:for-each>
</level>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
<xsl:template match="level|level1|level2|level3|level4|level5|level6">
<xsl:element name="{local-name()}" namespace="http://www.daisy.org/z3986/2005/dtbook/">
<xsl:call-template name="copyAtts"/>
<xsl:apply-templates/>
<xsl:if test="count(*[name()!='hd' and name()!='h1' and name()!='h2' and name()!='h3' and name()!='h4' and name()!='h5' and name()!='h6'])=0">
<p/>
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template match="levelhd">
<hd>
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name()='lang'">
<xsl:if test="not(../@xml:lang)">
<xsl:attribute name="xml:lang">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:if>
</xsl:when>
<xsl:when test="name()='depth'"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates/>
</hd>
</xsl:template>
<xsl:template match="hr | style | head/title"/>
<xsl:template match="prodnote[@class='photoDesc']">
<imggroup>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:apply-templates/>
</imggroup>
</xsl:template>
<xsl:template match="prodnote/span[@class='photoCaption']">
<caption>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:apply-templates/>
</caption>
</xsl:template>
<xsl:template match="span[@class='sidebarHead']">
<hd>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</hd>
</xsl:template>
<xsl:template match="span[@class='sidebarTitle']">
<hd>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</hd>
</xsl:template>
<xsl:template match="span[@class='defineTerm']">
<em>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</em>
</xsl:template>
<xsl:template match="bdo">
<bdo>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</bdo>
</xsl:template>
<xsl:template match="sidebar | prodnote">
<xsl:element name="{local-name()}" namespace="http://www.daisy.org/z3986/2005/dtbook/">
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:attribute name="render">optional</xsl:attribute>
<xsl:call-template name="copyAtts"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="lin">
<line>
<xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
<xsl:call-template name="copyAtts"/>
<xsl:apply-templates/>
</line>
</xsl:template>
</xsl:stylesheet>