<
xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" xmlns:dtb=
"http://www.daisy.org/z3986/2005/dtbook/" xmlns=
"http://www.daisy.org/z3986/2005/dtbook/" version=
"2.0">
<
xsl:include href=
"recursive-copy.xsl"/>
<
xsl:include href=
"output.xsl"/>
<
xsl:template match=
"dtb:p[dtb:list or dtb:dl]">
<
xsl:apply-templates select=
"node()" mode=
"fixList">
<
xsl:with-param name=
"pattr" select=
"@*[name()!='id']"/>
<
xsl:with-param name=
"oldId" select=
"@id"/>
</
xsl:apply-templates>
</
xsl:template>
<
xsl:template match=
"text()" mode=
"fixList"/>
<
xsl:template match=
"dtb:list|dtb:dl" mode=
"fixList">
<
xsl:param name=
"pattr"/>
<
xsl:param name=
"oldId"/>
<
xsl:message>fixing <
xsl:value-of select=
"name()"/></
xsl:message>
<
xsl:variable name=
"prev" select=
"(preceding-sibling::dtb:list|preceding-sibling::dtb:dl)[last()]"/>
<
xsl:variable name=
"pnumber">
<
xsl:choose>
<
xsl:when test=
"$prev">
<
xsl:value-of select=
"count(preceding-sibling::node()[ (preceding-sibling::dtb:list|preceding-sibling::dtb:dl)[last()]=$prev])"/>
</
xsl:when>
<
xsl:otherwise>
<
xsl:value-of select=
"count(preceding-sibling::node())"/>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:variable>
<
xsl:variable name=
"curPos" select=
"position()"/>
<
xsl:variable name=
"prevPos" select=
"$curPos - $pnumber - 1"/>
<
xsl:variable name=
"pcontent" select=
"../node()[position() < $curPos and position() > $prevPos]"/>
<
xsl:if test=
"normalize-space($pcontent) != ''">
<
xsl:element name=
"p">
<
xsl:call-template name=
"idGen">
<
xsl:with-param name=
"oldId" select=
"$oldId"/>
<
xsl:with-param name=
"genNew" select=
"count(../node()[position() < $prevPos and not(self::dtb:list or self::dtb:dl) and normalize-space(.)!=''])!=0"/>
</
xsl:call-template>
<
xsl:copy-of select=
"$pattr"/>
<
xsl:copy-of select=
"$pcontent"/>
</
xsl:element>
</
xsl:if>
<
xsl:copy-of select=
"."/>
<
xsl:if test=
"not(following-sibling::dtb:list or following-sibling::dtb:dl)">
<
xsl:element name=
"p">
<
xsl:call-template name=
"idGen">
<
xsl:with-param name=
"oldId" select=
"$oldId"/>
<
xsl:with-param name=
"genNew" select=
"count(preceding-sibling::node()[not(dtb:list|dtb:dl) and normalize-space(.)!=''])>0"/>
</
xsl:call-template>
<
xsl:copy-of select=
"$pattr"/>
<
xsl:copy-of select=
"following-sibling::node()"/>
</
xsl:element>
</
xsl:if>
</
xsl:template>
<
xsl:template name=
"idGen">
<
xsl:param name=
"oldId"/>
<
xsl:param name=
"genNew"/>
<
xsl:choose>
<
xsl:when test=
"not($oldId)">
</
xsl:when>
<
xsl:when test=
"$genNew">
<
xsl:attribute name=
"id"><
xsl:value-of select=
"generate-id()"/></
xsl:attribute>
</
xsl:when>
<
xsl:otherwise>
<
xsl:copy-of select=
"$oldId"/>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:template>
</
xsl:stylesheet>