]> granicus.if.org Git - docbook-dsssl/commitdiff
new stylesheets for better word processor support and easier maintenance
authorSteve Ball <balls@users.sourceforge.net>
Sun, 15 Jul 2007 01:19:04 +0000 (01:19 +0000)
committerSteve Ball <balls@users.sourceforge.net>
Sun, 15 Jul 2007 01:19:04 +0000 (01:19 +0000)
xsl/roundtrip/blocks2dbk.xsl [new file with mode: 0644]
xsl/roundtrip/normalise2sections.xsl [new file with mode: 0644]
xsl/roundtrip/sections2blocks.xsl [new file with mode: 0644]
xsl/roundtrip/wordml2normalise.xsl [new file with mode: 0644]

diff --git a/xsl/roundtrip/blocks2dbk.xsl b/xsl/roundtrip/blocks2dbk.xsl
new file mode 100644 (file)
index 0000000..0392c7f
--- /dev/null
@@ -0,0 +1,1120 @@
+<!DOCTYPE xsl:stylesheet [
+  <!ENTITY components "dbk:appendix |
+    dbk:article |
+    dbk:book |
+    dbk:chapter |
+    dbk:part |
+    dbk:preface |
+    dbk:section |
+    dbk:sect1 |
+    dbk:sect2 |
+    dbk:sect3 |
+    dbk:sect4 |
+    dbk:sect5">
+
+  <!ENTITY blocks "dbk:bibliography |
+    dbk:bibliodiv |
+    dbk:glossary |
+    dbk:glossdiv |
+    dbk:qandaset |
+    dbk:qandadiv">
+
+  <!ENTITY metadata-content '@rnd:style = "abstract" or
+                            @rnd:style = "abstract-title" or
+                            @rnd:style = "author" or
+                            @rnd:style = "editor" or
+                            @rnd:style = "othercredit" or
+                            @rnd:style = "revhistory" or
+                            @rnd:style = "revision" or
+                            @rnd:style = "date" or
+                            @rnd:style = "pubdate" or
+                            @rnd:style = "personblurb" or
+                            @rnd:style = "address" or
+                            @rnd:style = "affiliation" or
+                            @rnd:style = "contrib" or
+                            @rnd:style = "email" or
+                            @rnd:style = "pagenums" or
+                            @rnd:style = "issuenum" or
+                            @rnd:style = "volumenum" or
+                            @rnd:style = "biblioid" or
+                            @rnd:style = "bibliosource" or
+                            @rnd:style = "releaseinfo"'>
+
+  <!ENTITY author-content '@rnd:style = "author" or
+                          @rnd:style = "personblurb" or
+                          @rnd:style = "address" or
+                          @rnd:style = "affiliation" or
+                          @rnd:style = "contrib" or
+                          @rnd:style = "email"'>
+
+  <!ENTITY admonition '@rnd:style = "caution" or
+    @rnd:style = "important" or
+    @rnd:style = "note" or
+    @rnd:style = "tip" or
+    @rnd:style = "warning"'>
+  <!ENTITY admonition-title '@rnd:style = "caution-title" or
+    @rnd:style = "important-title" or
+    @rnd:style = "note-title" or
+    @rnd:style = "tip-title" or
+    @rnd:style = "warning-title"'>
+]>
+<xsl:stylesheet version="1.0"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  xmlns:dbk='http://docbook.org/ns/docbook'
+  xmlns:rnd='http://docbook.org/ns/docbook/roundtrip'>
+
+  <!-- $Id$ -->
+  <!-- Stylesheet to convert word processing docs to DocBook -->
+  <!-- This stylesheet processes the output of sections2blocks.xsl -->
+
+  <xsl:output indent="yes" method="xml" 
+    cdata-section-elements='dbk:programlisting dbk:literallayout'/>
+
+  <!-- ================================================== -->
+  <!--    Parameters                                      -->
+  <!-- ================================================== -->
+
+  <xsl:param name='docbook5'>0</xsl:param>
+  <xsl:param name="nest.sections">1</xsl:param>
+
+  <xsl:strip-space elements='*'/>
+  <xsl:preserve-space elements='dbk:para'/>
+
+  <xsl:template match="&components; |
+                       &blocks;">
+    <xsl:copy>
+      <xsl:call-template name='rnd:attributes'/>
+
+      <xsl:variable name='metadata'>
+        <xsl:apply-templates select='*[1]'
+          mode='rnd:metadata'/>
+      </xsl:variable>
+      <xsl:if test='$metadata'>
+        <dbk:info>
+          <xsl:copy-of select='$metadata'/>
+        </dbk:info>
+      </xsl:if>
+
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+  <xsl:template match="dbk:para" name='rnd:para'>
+    <!-- Some elements are normally suppressed,
+         since they are processed in a different context.
+         If this parameter is false then the element will be processed normally.
+      -->
+    <xsl:param name='suppress' select='true()'/>
+
+    <!-- This paragraph may be in a sidebar -->
+    <xsl:variable name='sidebar'
+                 select='preceding-sibling::*[self::dbk:para and @rnd:style = "sidebar-title"][1]'/>
+
+    <!-- This paragraph may be in the textobject of a table or figure -->
+    <xsl:variable name='table'
+                 select='preceding-sibling::dbk:informaltable[1]'/>
+    <xsl:variable name='figure'
+                 select='preceding-sibling::dbk:para[dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = ""][1]'/>
+    <xsl:variable name='caption'
+                 select='following-sibling::dbk:para[@rnd:style = "Caption"]'/>
+
+    <xsl:choose>
+      <!-- continue style paragraphs are handled in context -->
+      <xsl:when test='$suppress and
+                      @rnd:style = "para-continue"'/>
+
+      <!-- Certain elements gather the following paragraph -->
+      <xsl:when test='$suppress and
+                      preceding-sibling::*[1][self::dbk:para and
+                      @rnd:style = "example-title"]'/>
+
+      <xsl:when test='$suppress and
+                     $sidebar and
+                     not(preceding-sibling::dbk:para[(not(@rnd:style) or @rnd:style = "") and
+                       preceding-sibling::*[preceding-sibling::*[generate-id() = generate-id($sidebar)]]])'/>
+
+      <!-- Separate processing is performed for table/figure titles and captions -->
+      <xsl:when test='$suppress and
+                     @rnd:style = "table-title" and
+                     following-sibling::*[1][self::dbk:informaltable]'/>
+      <xsl:when test='$suppress and
+                     @rnd:style = "figure-title" and
+                     following-sibling::*[1][self::dbk:para][dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = ""]'/>
+      <xsl:when test='$suppress and
+                     @rnd:style = "Caption" and
+                     (preceding-sibling::*[self::dbk:informaltable] or
+                     preceding-sibling::*[self::dbk:para][dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = ""])'/>
+
+      <xsl:when test='$suppress and
+                     $table and
+                     $caption and
+                     generate-id($caption/preceding-sibling::dbk:informaltable[1]) = generate-id($table)'/>
+      <xsl:when test='$suppress and
+                     $figure and
+                     $caption and
+                     generate-id($caption/preceding-sibling::dbk:para[dbk:inlinemediaobject and count(*) = 1 and normalize-space(.) = ""][1]) = generate-id($figure)'/>
+
+      <!-- Ignore empty paragraphs -->
+      <xsl:when test='(not(@rnd:style) or
+                      @rnd:style = "") and
+                      normalize-space(.) = "" and
+                     not(*)'/>
+
+      <!-- Image inline or block? -->
+      <xsl:when test='(not(@rnd:style) or
+                      @rnd:style = "") and
+                      normalize-space(.) = "" and
+                     count(*) = 1 and
+                     dbk:inlinemediaobject'>
+        <xsl:apply-templates/>
+      </xsl:when>
+      <xsl:when test='not(@rnd:style) or
+                      @rnd:style = "" or
+                      @rnd:style = "para-continue"'>
+        <dbk:para>
+          <xsl:apply-templates/>
+        </dbk:para>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "xinclude"'
+        xmlns:xi='http://www.w3.org/2001/XInclude'>
+        <xi:include>
+          <xsl:attribute name='href'>
+            <xsl:apply-templates mode='rnd:xinclude'/>
+          </xsl:attribute>
+        </xi:include>
+      </xsl:when>
+
+      <xsl:when test='$suppress and
+                      preceding-sibling::*[1]/self::dbk:para[&admonition-title;]'/>
+      <xsl:when test='&admonition-title;'>
+        <xsl:element name='{substring-before(@rnd:style, "-title")}'
+          namespace='http://docbook.org/ns/docbook'>
+          <dbk:title>
+            <xsl:apply-templates/>
+          </dbk:title>
+          <xsl:apply-templates select='following-sibling::*[1]'>
+            <xsl:with-param name='suppress' select='false()'/>
+          </xsl:apply-templates>
+          <xsl:apply-templates select='following-sibling::*[2]'
+            mode='rnd:continue'/>
+        </xsl:element>
+      </xsl:when>
+
+      <xsl:when test='starts-with(@rnd:style, "itemizedlist") or
+                      starts-with(@rnd:style, "orderedlist")'>
+
+        <xsl:variable name='stop.node'
+          select='following-sibling::dbk:para[not(starts-with(@rnd:style, "itemizedlist") or starts-with(@rnd:style, "orderedlist")) and @rnd:style != "para-continue"][1]'/>
+
+        <xsl:choose>
+          <xsl:when test='translate(substring-after(@rnd:style, "list"), "0123456789", "") != "" or
+                          substring-after(@rnd:style, "list") = ""'>
+            <xsl:call-template name='rnd:error'>
+              <xsl:with-param name='code' select='"list-bad-level"'/>
+              <xsl:with-param name='message'>style "<xsl:value-of select='@rnd:style'/>" is not a valid list style</xsl:with-param>
+            </xsl:call-template>
+          </xsl:when>
+
+          <!-- TODO: the previous para-continue may not be associated with a list -->
+          <xsl:when test='preceding-sibling::*[1][self::dbk:para][starts-with(@rnd:style, "itemizedlist") or starts-with(@rnd:style, "orderedlist") or @rnd:style = "para-continue"]'/>
+          <xsl:when test='substring-after(@rnd:style, "list") != 1'>
+            <xsl:call-template name='rnd:error'>
+              <xsl:with-param name='code'>list-wrong-level</xsl:with-param>
+              <xsl:with-param name='message'>list started at the wrong level</xsl:with-param>
+            </xsl:call-template>
+          </xsl:when>
+          <xsl:when test='$stop.node'>
+            <xsl:element name='{substring-before(@rnd:style, "1")}'
+              namespace='http://docbook.org/ns/docbook'>
+              <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = current()/@rnd:style][following-sibling::*[generate-id() = generate-id($stop.node)]]'
+                mode='rnd:listitem'/>
+            </xsl:element>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:element name='{substring-before(@rnd:style, "1")}'
+              namespace='http://docbook.org/ns/docbook'>
+              <xsl:apply-templates select='.|following-sibling::dbk:para[@rnd:style = current()/@rnd:style]'
+                mode='rnd:listitem'/>
+            </xsl:element>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "programlisting" and
+                      preceding-sibling::*[1][self::dbk:para and @rnd:style = "programlisting"]'/>
+      <xsl:when test='@rnd:style = "literallayout" and
+                      preceding-sibling::*[1][self::dbk:para and @rnd:style = "literallayout"]'/>
+      <xsl:when test='@rnd:style = "programlisting" or
+                      @rnd:style = "literallayout"'>
+
+        <xsl:variable name='stop.node'
+          select='following-sibling::*[@rnd:style != current()/@rnd:style][1]'/>
+
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <xsl:apply-templates/>
+
+          <xsl:choose>
+            <xsl:when test='$stop.node'>
+              <xsl:apply-templates select='following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]]'
+                mode='rnd:programlisting'/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:apply-templates select='following-sibling::*'
+                mode='rnd:programlisting'/>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:element>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "example-title"'>
+        <xsl:element name='{substring-before(@rnd:style, "-title")}'
+          namespace='http://docbook.org/ns/docbook'>
+          <dbk:title>
+            <xsl:apply-templates/>
+          </dbk:title>
+
+          <xsl:apply-templates select='following-sibling::*[1]'>
+            <xsl:with-param name='suppress' select='false()'/>
+          </xsl:apply-templates>
+        </xsl:element>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "sidebar-title"'>
+       <xsl:variable name='stop.node'
+                     select='following-sibling::dbk:para[(not(@rnd:style) or @rnd:style = "") and
+                             normalize-space(.) = ""][1]'/>
+
+       <dbk:sidebar>
+         <dbk:info>
+           <dbk:title>
+             <xsl:apply-templates/>
+           </dbk:title>
+         </dbk:info>
+
+         <xsl:choose>
+           <xsl:when test='$stop.node'>
+             <xsl:apply-templates select='following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]]'
+                                  mode='rnd:sidebar'/>
+           </xsl:when>
+           <xsl:otherwise>
+             <xsl:apply-templates select='following-sibling::*'
+                                  mode='rnd:sidebar'/>
+           </xsl:otherwise>
+         </xsl:choose>
+       </dbk:sidebar>
+      </xsl:when>
+
+      <xsl:when test='&admonition;'>
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <dbk:para>
+            <xsl:apply-templates/>
+          </dbk:para>
+          <xsl:apply-templates select='following-sibling::*[1]'
+            mode='rnd:continue'/>
+        </xsl:element>
+      </xsl:when>
+
+      <!-- TODO: make sure this is in a bibliography.
+          If not, create a bibliolist.
+       -->
+      <xsl:when test='@rnd:style = "bibliomixed"'>
+       <dbk:bibliomixed>
+         <xsl:apply-templates/>
+       </dbk:bibliomixed>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "biblioentry"'>
+        <!-- TODO: handle titles, metadata, etc -->
+       <dbk:biblioentry>
+         <xsl:apply-templates/>
+       </dbk:biblioentry>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "blockquote-attribution" and
+                      preceding-sibling::*[1][self::dbk:para][@rnd:style = "blockquote-title" or @rnd:style = "blockquote"]'/>
+      <xsl:when test='@rnd:style = "blockquote-attribution"'>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>improper-blockquote-attribution</xsl:with-param>
+          <xsl:with-param name='message'>blockquote attribution must follow a blockquote title</xsl:with-param>
+        </xsl:call-template>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "blockquote" or
+                      @rnd:style = "blockquote-title"'>
+        <xsl:choose>
+          <xsl:when test='@rnd:style = "blockquote" and
+                          preceding-sibling::*[1][self::dbk:para][starts-with(@rnd:style, "blockquote")]'/>
+          <xsl:otherwise>
+
+            <xsl:variable name='stop.node'
+              select='following-sibling::*[not(@rnd:style = "blockquote" or
+                      @rnd:style = "blockquote-attribution")][1]'/>
+
+            <dbk:blockquote>
+             <xsl:if test='@rnd:style = "blockquote-title"'>
+               <dbk:info>
+                 <dbk:title>
+                   <xsl:apply-templates/>
+                 </dbk:title>
+               </dbk:info>
+             </xsl:if>
+              <xsl:choose>
+                <xsl:when test='$stop.node'>
+                 <xsl:apply-templates select='following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]][@rnd:style = "blockquote-attribution"]' mode='rnd:blockquote-attribution'/>
+                  <xsl:apply-templates select='self::*[@rnd:style = "blockquote"] |
+                                              following-sibling::*[following-sibling::*[generate-id() = generate-id($stop.node)]]'
+                    mode='rnd:blockquote'/>
+                </xsl:when>
+                <xsl:otherwise>
+                 <xsl:apply-templates select='following-sibling::*[@rnd:style = "blockquote-attribution"]' mode='rnd:blockquote-attribution'/>
+                  <xsl:apply-templates select='self::*[@rnd:style = "blockquote"] |
+                                              following-sibling::*'
+                    mode='rnd:blockquote'/>
+                </xsl:otherwise>
+              </xsl:choose>
+            </dbk:blockquote>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "bridgehead"'>
+        <xsl:element name='{@rnd:style}'>
+          <xsl:call-template name='rnd:attributes'/>
+          <xsl:apply-templates/>
+        </xsl:element>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "formalpara-title"'>
+        <dbk:formalpara>
+          <dbk:title>
+            <xsl:call-template name='rnd:attributes'/>
+            <xsl:apply-templates/>
+          </dbk:title>
+          <xsl:choose>
+            <xsl:when test='following-sibling::*[1][self::dbk:para][@rnd:style = "formalpara"]'>
+              <dbk:para>
+                <xsl:call-template name='rnd:attributes'>
+                  <xsl:with-param name='node'
+                    select='following-sibling::*[1]'/>
+                </xsl:call-template>
+                <xsl:apply-templates select='following-sibling::*[1]/node()'/>
+              </dbk:para>
+            </xsl:when>
+          </xsl:choose>
+        </dbk:formalpara>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "formalpara" and
+                      preceding-sibling::*[1][self::dbk:para][@rnd:style = "formalpara-title"]'/>
+      <xsl:when test='@rnd:style = "formalpara"'>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>formalpara-notitle</xsl:with-param>
+          <xsl:with-param name='message'>formalpara used without a title</xsl:with-param>
+        </xsl:call-template>
+      </xsl:when>
+
+      <xsl:when test='(contains(@rnd:style, "-title") or
+                      contains(@rnd:style, "-titleabbrev") or
+                      contains(@rnd:style, "-subtitle")) and
+                      not(starts-with(@rnd:style, "blockquote") or starts-with(@rnd:style, "formal"))'>
+        <!-- TODO: check that no non-metadata elements occur before this paragraph -->
+      </xsl:when>
+
+      <!-- Metadata elements are handled in rnd:metadata mode -->
+      <!-- TODO: check that no non-metadata elements occur before this paragraph -->
+      <xsl:when test='&metadata-content;'/>
+
+      <xsl:otherwise>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>unknown-style</xsl:with-param>
+          <xsl:with-param name='message'>unknown paragraph style "<xsl:value-of select='@rnd:style'/>" encountered</xsl:with-param>
+        </xsl:call-template>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:emphasis'>
+    <xsl:choose>
+      <xsl:when test='not(@rnd:style) and @role = "italic"'>
+       <xsl:copy>
+          <xsl:apply-templates mode='rnd:copy'/>
+       </xsl:copy>
+      </xsl:when>
+      <xsl:when test='not(@rnd:style) or @role'>
+        <xsl:copy>
+          <xsl:call-template name='rnd:attributes'/>
+          <xsl:apply-templates mode='rnd:copy'/>
+        </xsl:copy>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = preceding-sibling::node()[self::dbk:emphasis]/@rnd:style'/>
+
+      <xsl:when test='@rnd:style = "citetitle"'>
+        <xsl:element name='{@rnd:style}'>
+          <xsl:call-template name='rnd:attributes'/>
+          <xsl:apply-templates/>
+          <xsl:apply-templates select='following-sibling::node()[1]'
+            mode='rnd:emphasis'/>
+        </xsl:element>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "Hyperlink" and
+                     parent::dbk:link'>
+       <!-- This occurs in a hyperlink; parent should be dbk:link -->
+       <xsl:apply-templates/>
+      </xsl:when>
+
+      <xsl:otherwise>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>unknown-style</xsl:with-param>
+          <xsl:with-param name='message'>unknown character span style "<xsl:value-of select='@rnd:style'/>" encountered</xsl:with-param>
+        </xsl:call-template>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <!-- Coalesce emphasis elements into a single element -->
+  <xsl:template match='dbk:emphasis' mode='rnd:emphasis'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = preceding-sibling::node()[self::dbk:emphasis]/@rnd:style'>
+        <xsl:apply-templates/>
+        <xsl:apply-templates select='following-sibling::node()[1]'
+          mode='rnd:emphasis'/>
+      </xsl:when>
+    </xsl:choose>
+  </xsl:template>
+  <xsl:template match='*|text()' mode='rnd:emphasis'/>
+
+  <xsl:template match='dbk:subscript|dbk:superscript'>
+    <xsl:copy>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+  <!-- Images -->
+
+  <xsl:template match='dbk:inlinemediaobject'>
+    <xsl:choose>
+      <xsl:when test='not(preceding-sibling::*|following-sibling::*) and
+                     normalize-space(..) = ""'>
+
+       <xsl:variable name='next.captioned'
+                     select='ancestor::dbk:para/following-sibling::*[self::dbk:informaltable or self::dbk:para[dbk:inlinemediaobject and count(*) = 1 and normalize-space() = ""]][1]'/>
+
+       <xsl:variable name='caption'
+                     select='ancestor::dbk:para/following-sibling::dbk:para[@rnd:style = "Caption"]'/>
+
+       <xsl:variable name='metadata'>
+         <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[1]'
+                              mode='rnd:metadata'/>
+       </xsl:variable>
+
+       <dbk:figure>
+         <xsl:if test='ancestor::dbk:para/preceding-sibling::*[1][self::dbk:para][@rnd:style = "figure-title"] or
+                       $metadata'>
+           <dbk:info>
+             <xsl:if test='ancestor::dbk:para/preceding-sibling::*[1][self::dbk:para][@rnd:style = "figure-title"]'>
+               <dbk:title>
+                 <xsl:apply-templates select='ancestor::dbk:para/preceding-sibling::*[1]/node()'/>
+               </dbk:title>
+             </xsl:if>
+             <xsl:copy-of select='$metadata'/>
+           </dbk:info>
+         </xsl:if>
+
+         <dbk:mediaobject>
+           <xsl:apply-templates mode='rnd:copy'/>
+         </dbk:mediaobject>
+
+         <xsl:choose>
+           <xsl:when test='not($caption)'/>
+           <xsl:when test='not($next.captioned)'>
+             <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[following-sibling::*[generate-id() = generate-id($caption)]][not(&metadata-content;)]'
+                                  mode='rnd:figure'/>
+             <xsl:apply-templates select='$caption'
+                                  mode='rnd:caption'/>
+           </xsl:when>
+           <!-- Does caption belong to this image or next.captioned?
+              - Only if it belongs to this image do we process it here.
+             -->
+           <xsl:when test='$next.captioned[preceding-sibling::*[generate-id() = generate-id($caption)]]'>
+             <xsl:apply-templates select='ancestor::dbk:para/following-sibling::*[following-sibling::*[generate-id() = generate-id($caption)]][not(&metadata-content;)]'
+                                  mode='rnd:figure'/>
+             <xsl:apply-templates select='$caption'
+                                  mode='rnd:caption'/>
+           </xsl:when>
+           <!-- otherwise caption does not belong to this figure -->
+         </xsl:choose>
+       </dbk:figure>
+      </xsl:when>
+      <xsl:otherwise>
+       <xsl:call-template name='rnd:copy'/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:para[@rnd:style = "Caption"]' mode='rnd:caption'>
+    <dbk:caption>
+      <dbk:para>
+       <xsl:apply-templates/>
+      </dbk:para>
+    </dbk:caption>
+  </xsl:template>
+  <xsl:template match='*' mode='rnd:caption'/>
+
+  <xsl:template match='*' mode='rnd:figure'>
+    <xsl:call-template name='rnd:para'>
+      <xsl:with-param name='suppress' select='false()'/>
+    </xsl:call-template>
+  </xsl:template>
+
+  <!-- Sidebars -->
+
+  <xsl:template match='*' mode='rnd:sidebar'>
+    <xsl:call-template name='rnd:para'>
+      <xsl:with-param name='suppress' select='false()'/>
+    </xsl:call-template>
+  </xsl:template>
+
+  <!-- Lists -->
+
+  <xsl:template match='dbk:para' mode='rnd:listitem'>
+    <dbk:listitem>
+      <dbk:para>
+        <xsl:call-template name='rnd:attributes'/>
+        <xsl:apply-templates/>
+      </dbk:para>
+      <xsl:apply-templates select='following-sibling::*[1]'
+        mode='rnd:continue'/>
+
+      <!-- Handle nested lists -->
+      <xsl:variable name='list-type'
+        select='concat(substring-before(@rnd:style, "list"), "list")'/>
+      <xsl:variable name='list-level'
+        select='substring-after(@rnd:style, $list-type)'/>
+
+      <!-- Assuming only five levels of list nesting.
+         - This is probably better done in a previous stage using grouping.
+        -->
+      <xsl:variable name='stop.node'
+        select='following-sibling::dbk:para[@rnd:style != concat("itemizedlist", $list-level + 1) and
+                @rnd:style != concat("orderedlist", $list-level + 1) and
+                @rnd:style != concat("itemizedlist", $list-level + 2) and
+                @rnd:style != concat("orderedlist", $list-level + 2) and
+                @rnd:style != concat("itemizedlist", $list-level + 3) and
+                @rnd:style != concat("orderedlist", $list-level + 3) and
+                @rnd:style != "para-continue"][1]'/>
+
+      <xsl:variable name='nested'
+        select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)][1]'/>
+
+      <xsl:choose>
+        <!-- Is there a nested list at all? -->
+        <xsl:when test='following-sibling::*[self::dbk:para and @rnd:style != "para-continue"][1][@rnd:style != concat("itemizedlist", $list-level + 1) and @rnd:style != concat("orderedlist", $list-level + 1)]'/>
+
+        <xsl:when test='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)] and
+                        $stop.node'>
+          <xsl:element name='{concat(substring-before($nested/@rnd:style, "list"), "list")}'
+            namespace='http://docbook.org/ns/docbook'>
+            <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)][following-sibling::*[generate-id() = generate-id($stop.node)]]'
+              mode='rnd:listitem'/>
+          </xsl:element>
+        </xsl:when>
+        <xsl:when test='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)]'>
+
+          <xsl:element name='{concat(substring-before($nested/@rnd:style, "list"), "list")}'
+            namespace='http://docbook.org/ns/docbook'>
+            <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = concat("itemizedlist", $list-level + 1) or @rnd:style = concat("orderedlist", $list-level + 1)]'
+              mode='rnd:listitem'/>
+          </xsl:element>
+        </xsl:when>
+      </xsl:choose>
+    </dbk:listitem>
+  </xsl:template>
+
+  <!-- Blockquotes -->
+
+  <xsl:template match='dbk:para' mode='rnd:blockquote'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style ="blockquote-attribution"'/>
+      <xsl:when test='@rnd:style ="blockquote-title"'/>
+      <xsl:otherwise>
+        <dbk:para>
+          <xsl:apply-templates/>
+        </dbk:para>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+  <xsl:template match='dbk:para' mode='rnd:blockquote-attribution'>
+    <xsl:if test='@rnd:style ="blockquote-attribution"'>
+      <dbk:attribution>
+        <xsl:apply-templates/>
+      </dbk:attribution>
+    </xsl:if>
+  </xsl:template>
+
+  <!-- Metadata -->
+
+  <xsl:template match='dbk:para' mode='rnd:metadata'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = "abstract-title" or
+                      @rnd:style = "abstract"'>
+        <xsl:variable name='stop.node'
+          select='following-sibling::dbk:para[@rnd:style != "abstract"][1]'/>
+        <dbk:abstract>
+          <xsl:apply-templates select='.' mode='rnd:abstract'/>
+          <xsl:choose>
+            <xsl:when test='$stop.node'>
+              <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = "abstract"][following-sibling::*[generate-id() = generate-id($stop.node)]]'
+                mode='rnd:abstract'/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:apply-templates select='following-sibling::dbk:para[@rnd:style = "abstract"]'
+                mode='rnd:abstract'/>
+            </xsl:otherwise>
+          </xsl:choose>
+        </dbk:abstract>
+        <xsl:apply-templates select='$stop.node'
+          mode='rnd:metadata'/>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "author"'>
+        <dbk:author>
+          <dbk:personname>
+            <!-- TODO: check style of author; mixed content or structured -->
+            <xsl:apply-templates mode='rnd:personname'/>
+          </dbk:personname>
+          <xsl:apply-templates select='following-sibling::*[1]'
+            mode='rnd:author'/>
+        </dbk:author>
+        <xsl:call-template name='rnd:resume-metadata'>
+         <xsl:with-param name='node' select='following-sibling::*[1]'/>
+       </xsl:call-template>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "personblurb" or
+                      @rnd:style = "address" or
+                      @rnd:style = "affiliation" or
+                      @rnd:style = "contrib" or
+                      @rnd:style = "email"'/>
+
+      <xsl:when test='@rnd:style = "releaseinfo" or
+                      @rnd:style = "date" or
+                      @rnd:style = "pubdate" or
+                      @rnd:style = "pagenums" or
+                      @rnd:style = "issuenum" or
+                      @rnd:style = "volumenum" or
+                      @rnd:style = "editor" or
+                      @rnd:style = "othercredit" or
+                      @rnd:style = "biblioid" or
+                      @rnd:style = "bibliosource" or
+                      @rnd:style = "revhistory" or
+                      @rnd:style = "revision"'>
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <xsl:apply-templates mode='rnd:metadata'/>
+        </xsl:element>
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:metadata'/>
+      </xsl:when>
+      <xsl:when test='contains(@rnd:style, "-titleabbrev")'>
+        <xsl:variable name='parent'
+          select='substring-before(@rnd:style, "-titleabbrev")'/>
+
+        <xsl:choose>
+          <xsl:when test='$parent = local-name(..)'>
+            <dbk:titleabbrev>
+              <xsl:apply-templates mode='rnd:metadata'/>
+            </dbk:titleabbrev>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:call-template name='rnd:error'>
+              <xsl:with-param name='code'>bad-titleabbrev</xsl:with-param>
+              <xsl:with-param name='message'>titleabbrev style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
+            </xsl:call-template>
+          </xsl:otherwise>
+        </xsl:choose>
+
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:metadata'/>
+      </xsl:when>
+      <xsl:when test='contains(@rnd:style, "-title")'>
+        <xsl:variable name='parent'
+          select='substring-before(@rnd:style, "-title")'/>
+
+        <xsl:choose>
+          <xsl:when test='$parent = local-name(..)'>
+            <dbk:title>
+              <xsl:apply-templates mode='rnd:metadata'/>
+            </dbk:title>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:call-template name='rnd:error'>
+              <xsl:with-param name='code'>bad-title</xsl:with-param>
+              <xsl:with-param name='message'>title style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
+            </xsl:call-template>
+          </xsl:otherwise>
+        </xsl:choose>
+
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:metadata'/>
+      </xsl:when>
+      <xsl:when test='contains(@rnd:style, "-subtitle")'>
+        <xsl:variable name='parent'
+          select='substring-before(@rnd:style, "-subtitle")'/>
+
+        <xsl:choose>
+          <xsl:when test='$parent = local-name(..)'>
+            <dbk:subtitle>
+              <xsl:apply-templates mode='rnd:metadata'/>
+            </dbk:subtitle>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:call-template name='rnd:error'>
+              <xsl:with-param name='code'>bad-subtitle</xsl:with-param>
+              <xsl:with-param name='message'>subtitle style "<xsl:value-of select='@rnd:style'/>" mismatches parent "<xsl:value-of select='local-name(..)'/>"</xsl:with-param>
+            </xsl:call-template>
+          </xsl:otherwise>
+        </xsl:choose>
+
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:metadata'/>
+      </xsl:when>
+    </xsl:choose>
+  </xsl:template>
+  <xsl:template match='dbk:emphasis' mode='rnd:metadata'>
+    <xsl:choose>
+      <xsl:when test='not(@rnd:style)'>
+        <xsl:copy>
+          <xsl:apply-templates mode='rnd:metadata'/>
+        </xsl:copy>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:element name='{@rnd:style}'>
+          <xsl:apply-templates mode='rnd:metadata'/>
+        </xsl:element>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+  <xsl:template match='*' mode='rnd:metadata'/>
+
+  <xsl:template name='rnd:resume-metadata'>
+    <xsl:param name='node' select='/..'/>
+
+    <xsl:choose>
+      <xsl:when test='$node[self::dbk:para][&author-content;]'>
+       <xsl:call-template name='rnd:resume-metadata'>
+         <xsl:with-param name='node' select='$node/following-sibling::*[1]'/>
+       </xsl:call-template>
+      </xsl:when>
+      <xsl:when test='$node[self::dbk:para][&metadata-content;]'>
+       <xsl:apply-templates select='$node' mode='rnd:metadata'/>
+      </xsl:when>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:para' mode='rnd:abstract'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = "abstract-title"'>
+        <dbk:title>
+          <xsl:call-template name='rnd:attributes'/>
+          <xsl:apply-templates/>
+        </dbk:title>
+      </xsl:when>
+      <xsl:otherwise>
+        <dbk:para>
+          <xsl:call-template name='rnd:attributes'/>
+          <xsl:apply-templates/>
+        </dbk:para>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:emphasis' mode='rnd:personname'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = "honorific" or
+                      @rnd:style = "firstname" or
+                      @rnd:style = "lineage" or
+                      @rnd:style = "othername" or
+                      @rnd:style = "surname"'>
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <xsl:apply-templates/>
+        </xsl:element>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>bad-author-inline</xsl:with-param>
+          <xsl:with-param name='message'>character span "<xsl:value-of select='@rnd:style'/>" not allowed in an author paragraph</xsl:with-param>
+        </xsl:call-template>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:para' mode='rnd:author'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = "personblurb" and
+                      preceding-sibling::*[1][self::dbk:para and @rnd:style != "personblurb"]'>
+        <dbk:personblurb>
+          <xsl:apply-templates select='.'
+            mode='rnd:personblurb'/>
+        </dbk:personblurb>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "personblurb"'>
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:author'/>
+      </xsl:when>
+
+      <!-- Web and mail addresses may appear in a simplified form -->
+      <xsl:when test='@rnd:style = "address"'>
+        <xsl:choose>
+          <xsl:when test='dbk:link and
+                          count(dbk:link) = count(*)'>
+            <!-- simplified form -->
+            <dbk:otheraddr>
+              <xsl:apply-templates select='dbk:link'
+                mode='rnd:otheraddr'/>
+            </dbk:otheraddr>
+          </xsl:when>
+          <xsl:otherwise>
+            <dbk:address>
+              <xsl:apply-templates mode='rnd:author'/>
+            </dbk:address>
+            <xsl:apply-templates select='following-sibling::*[1]'
+              mode='rnd:author'/>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:when>
+
+      <xsl:when test='@rnd:style = "affiliation"'>
+       <dbk:affiliation>
+         <xsl:choose>
+           <xsl:when test='not(*)'>
+              <dbk:jobtitle>
+               <xsl:apply-templates mode='rnd:author'/>
+             </dbk:jobtitle>
+           </xsl:when>
+           <xsl:otherwise>
+              <xsl:apply-templates mode='rnd:author'/>
+           </xsl:otherwise>
+         </xsl:choose>
+       </dbk:affiliation>
+       <xsl:apply-templates select='following-sibling::*[1]'
+                            mode='rnd:author'/>
+      </xsl:when>
+      <xsl:when test='@rnd:style = "contrib" or
+                      @rnd:style = "email"'>
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <xsl:apply-templates mode='rnd:author'/>
+        </xsl:element>
+        <xsl:apply-templates select='following-sibling::*[1]'
+          mode='rnd:author'/>
+      </xsl:when>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:link' mode='rnd:otheraddr'>
+    <xsl:copy>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates mode='rnd:otheraddr'/>
+    </xsl:copy>
+  </xsl:template>
+
+  <!-- TODO: not all of these inlines are allowed in all elements that are children of author.
+       Need to further refine validation of inlines.
+    -->
+  <xsl:template match='dbk:emphasis' mode='rnd:author'>
+    <xsl:choose>
+      <xsl:when test='@rnd:style = "city" or
+                      @rnd:style = "country" or
+                      @rnd:style = "email" or
+                      @rnd:style = "fax" or
+                      @rnd:style = "jobtitle" or
+                      @rnd:style = "orgdiv" or
+                      @rnd:style = "orgname" or
+                      @rnd:style = "otheraddr" or
+                      @rnd:style = "phone" or
+                      @rnd:style = "pob" or
+                      @rnd:style = "postcode" or
+                      @rnd:style = "shortaffil" or
+                      @rnd:style = "state" or
+                      @rnd:style = "street"'>
+        <xsl:element name='{@rnd:style}'
+          namespace='http://docbook.org/ns/docbook'>
+          <xsl:apply-templates/>
+        </xsl:element>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:call-template name='rnd:error'>
+          <xsl:with-param name='code'>metadata-bad-inline</xsl:with-param>
+          <xsl:with-param name='message'>character span "<xsl:value-of select='@rnd:style'/>" not allowed in author metadata</xsl:with-param>
+        </xsl:call-template>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='dbk:para' mode='rnd:personblurb'>
+    <xsl:if test='@rnd:style = "personblurb"'>
+      <dbk:para>
+        <xsl:apply-templates/>
+      </dbk:para>
+      <xsl:apply-templates select='following-sibling::*[1]'
+        mode='rnd:personblurb'/>
+    </xsl:if>
+  </xsl:template>
+
+  <xsl:template match='dbk:para' mode='rnd:programlisting'>
+    <xsl:text>&#xa;</xsl:text>
+    <xsl:apply-templates/>
+  </xsl:template>
+
+  <!-- Continuing paragraphs -->
+
+  <xsl:template match='*' mode='rnd:continue'/>
+  <xsl:template match='dbk:para' mode='rnd:continue'>
+    <xsl:if test='@rnd:style = "para-continue"'>
+      <dbk:para>
+        <xsl:call-template name='rnd:attributes'/>
+        <xsl:apply-templates/>
+      </dbk:para>
+      <xsl:apply-templates select='following-sibling::*[1]'
+        mode='rnd:continue'/>
+    </xsl:if>
+  </xsl:template>
+
+  <!-- Tables -->
+
+  <xsl:template match='dbk:informaltable'>
+    <xsl:choose>
+      <xsl:when test='preceding-sibling::*[1][self::dbk:para][@rnd:style ="table-title"]'>
+       <dbk:table>
+         <xsl:apply-templates select='@*' mode='rnd:copy'/>
+
+         <dbk:info>
+           <xsl:apply-templates select='preceding-sibling::dbk:para[1]'
+                                mode='rnd:table-title'/>
+         </dbk:info>
+
+         <xsl:call-template name='rnd:table-textobject'/>
+
+         <xsl:apply-templates/>
+
+         <xsl:call-template name='rnd:table-caption'/>
+       </dbk:table>
+      </xsl:when>
+      <xsl:otherwise>
+       <xsl:copy>
+         <xsl:apply-templates select='@*' mode='rnd:copy'/>
+
+         <xsl:call-template name='rnd:table-textobject'/>
+
+         <xsl:apply-templates/>
+
+         <xsl:call-template name='rnd:table-caption'/>
+       </xsl:copy>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+  <xsl:template match='dbk:tgroup|dbk:tbody|dbk:row|dbk:colspec'>
+    <xsl:copy>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+  <xsl:template match='dbk:entry'>
+    <dbk:entry>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates/>
+    </dbk:entry>
+  </xsl:template>
+
+  <xsl:template match='dbk:para' mode='rnd:table-title'>
+    <dbk:title>
+      <xsl:apply-templates/>
+    </dbk:title>
+  </xsl:template>
+
+  <!-- Find the caption associated with this table -->
+  <xsl:template name='rnd:table-caption'>
+    <xsl:variable name='candidate'
+                 select='following-sibling::dbk:para[@rnd:style = "Caption"][1]'/>
+
+    <xsl:if test='$candidate != "" and
+                 generate-id($candidate/preceding-sibling::dbk:informaltable[1]) = generate-id(.)'>
+      <dbk:caption>
+       <dbk:para>
+         <xsl:apply-templates select='$candidate/node()'/>
+       </dbk:para>
+      </dbk:caption>
+    </xsl:if>
+  </xsl:template>
+
+  <!-- Find table associated text -->
+  <xsl:template name='rnd:table-textobject'>
+    <xsl:variable name='caption'
+                 select='following-sibling::dbk:para[@rnd:style = "Caption"][1]'/>
+
+    <xsl:if test='generate-id($caption/preceding-sibling::dbk:informaltable[1]) = generate-id(.)'>
+      <xsl:variable name='content'
+                   select='following-sibling::*[following-sibling::*[generate-id($caption) = generate-id()]]'/>
+      <xsl:if test='$content'>
+       <dbk:textobject>
+         <xsl:apply-templates select='$content' mode='rnd:table-textobject'/>
+       </dbk:textobject>
+      </xsl:if>
+    </xsl:if>
+  </xsl:template>
+  <xsl:template match='dbk:para' mode='rnd:table-textobject'>
+    <xsl:call-template name='rnd:para'>
+      <xsl:with-param name='suppress' select='false()'/>
+    </xsl:call-template>
+  </xsl:template>
+
+  <!-- Footnotes -->
+  <xsl:template match='dbk:footnote'>
+    <xsl:copy>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+  <!-- utilities -->
+
+  <!-- rnd:attributes reconstitutes an element's attributes -->
+  <xsl:template name='rnd:attributes'>
+    <xsl:param name='node' select='.'/>
+
+    <xsl:apply-templates select='$node/@*[namespace-uri() != "http://docbook.org/ns/docbook/roundtrip"]' mode='rnd:copy'/>
+  </xsl:template>
+
+  <xsl:template match='*' name='rnd:copy' mode='rnd:copy'>
+    <xsl:copy>
+      <xsl:apply-templates select='@*' mode='rnd:copy'/>
+      <xsl:apply-templates mode='rnd:copy'/>
+    </xsl:copy>
+  </xsl:template>
+  <xsl:template match='@*' mode='rnd:copy'>
+    <xsl:copy/>
+  </xsl:template>
+
+  <!-- These templates are invoked whenever an error condition is detected in the conversion of a document.
+    -->
+  <xsl:template name='rnd:error'>
+    <xsl:param name='node' select='.'/>
+    <xsl:param name='code'/>
+    <xsl:param name='message'/>
+
+    <xsl:comment><xsl:value-of select='$message'/></xsl:comment>
+    <xsl:message>ERROR "<xsl:value-of select='$code'/>": <xsl:value-of select='$message'/></xsl:message>
+    <rnd:error>
+      <rnd:code>
+        <xsl:value-of select='$code'/>
+      </rnd:code>
+      <rnd:message>
+        <xsl:value-of select='$message'/>
+      </rnd:message>
+    </rnd:error>
+  </xsl:template>
+
+</xsl:stylesheet>
diff --git a/xsl/roundtrip/normalise2sections.xsl b/xsl/roundtrip/normalise2sections.xsl
new file mode 100644 (file)
index 0000000..2838731
--- /dev/null
@@ -0,0 +1,1270 @@
+<?xml version="1.0"?>
+<axsl:stylesheet xmlns:axsl="http://www.w3.org/1999/XSL/Transform" xmlns:dbk="http://docbook.org/ns/docbook" xmlns:rnd="http://docbook.org/ns/docbook/roundtrip" version="1.0">
+<!--====================================-->
+<!--=                                  =-->
+<!--=   DO NOT EDIT THIS STYLESHEET    =-->
+<!--=                                  =-->
+<!--= This stylesheet is generated     =-->
+<!--= by makeSections.xsl and a        =-->
+<!--= mapping specification.           =-->
+<!--=                                  =-->
+<!--= Revision history:                =-->
+<!--=                                  =-->
+<!--= $Id$ =-->
+<!--=                                  =-->
+<!--====================================-->
+  <axsl:output indent="yes"/>
+  <axsl:strip-space elements="*"/>
+  <axsl:preserve-space elements="dbk:para"/>
+  <axsl:template match="dbk:article">
+    <axsl:copy>
+      <axsl:for-each select="@*">
+        <axsl:copy/>
+      </axsl:for-each>
+      <axsl:variable name="books" select="dbk:para[@rnd:style = &quot;book&quot; or @rnd:style = &quot;book-title&quot;]"/>
+      <axsl:variable name="toplevel-components" select="dbk:para[@rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;]"/>
+      <axsl:choose>
+        <axsl:when test="$books">
+          <axsl:apply-templates select="$books[1]/preceding-sibling::*"/>
+          <axsl:apply-templates select="$books[1]" mode="book">
+            <axsl:with-param name="books" select="$books[position() != 1]"/>
+          </axsl:apply-templates>
+        </axsl:when>
+        <axsl:when test="$toplevel-components">
+          <axsl:apply-templates select="$toplevel-components[1]/preceding-sibling::*"/>
+          <axsl:apply-templates select="$toplevel-components[1]" mode="toplevel-component">
+            <axsl:with-param name="toplevel-components" select="$toplevel-components[position() != 1]"/>
+          </axsl:apply-templates>
+        </axsl:when>
+        <axsl:otherwise>
+          <axsl:apply-templates/>
+        </axsl:otherwise>
+      </axsl:choose>
+    </axsl:copy>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book">
+    <axsl:param name="books" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="$books and (@rnd:style = &quot;book&quot; or @rnd:style = &quot;book-title&quot;)">
+        <axsl:call-template name="make-book">
+          <axsl:with-param name="books" select="$books"/>
+          <axsl:with-param name="book-components" select="$books[1]/preceding-sibling::dbk:para[@rnd:style = &quot;part&quot; or @rnd:style = &quot;part-title&quot; or @rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;]"/>
+        </axsl:call-template>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="make-book">
+          <axsl:with-param name="books" select="$books"/>
+          <axsl:with-param name="book-components" select="following-sibling::dbk:para[@rnd:style = &quot;part&quot; or @rnd:style = &quot;part-title&quot; or @rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;]"/>
+        </axsl:call-template>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template name="make-book">
+    <axsl:param name="books" select="/.."/>
+    <axsl:param name="book-components" select="/.."/>
+<!-- mode toplevel-templates -->
+    <book xmlns="http://docbook.org/ns/docbook">
+      <axsl:call-template name="copy"/>
+      <axsl:apply-templates select="following-sibling::*[1]" mode="book-component">
+        <axsl:with-param name="nextbook" select="$books[1]"/>
+        <axsl:with-param name="book-components" select="$book-components"/>
+      </axsl:apply-templates>
+    </book>
+    <axsl:apply-templates select="$books[1]" mode="book">
+      <axsl:with-param name="books" select="$books[position() != 1]"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-component">
+    <axsl:param name="toplevel-components" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="$toplevel-components and (@rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;)">
+        <axsl:call-template name="make-toplevel-component">
+          <axsl:with-param name="toplevel-components" select="$toplevel-components"/>
+          <axsl:with-param name="sect1s" select="$toplevel-components[1]/preceding-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;]"/>
+        </axsl:call-template>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="make-toplevel-component">
+          <axsl:with-param name="toplevel-components" select="$toplevel-components"/>
+          <axsl:with-param name="sect1s" select="following-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;]"/>
+        </axsl:call-template>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template name="make-toplevel-component">
+    <axsl:param name="toplevel-components" select="/.."/>
+    <axsl:param name="sect1s" select="/.."/>
+<!-- mode toplevel-templates: group:level - do not create element -->
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect1">
+      <axsl:with-param name="nexttoplevel-component" select="$toplevel-components[1]"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+    <axsl:apply-templates select="$toplevel-components[1]" mode="toplevel-component">
+      <axsl:with-param name="toplevel-components" select="$toplevel-components[position() != 1]"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-component">
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;part&quot; or @rnd:style = &quot;part-title&quot; or @rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;">
+        <axsl:variable name="nextbook-component" select="following-sibling::dbk:para[@rnd:style = &quot;part&quot; or @rnd:style = &quot;part-title&quot; or @rnd:style = &quot;article&quot; or @rnd:style = &quot;article-title&quot; or @rnd:style = &quot;appendix&quot; or @rnd:style = &quot;appendix-title&quot; or @rnd:style = &quot;chapter&quot; or @rnd:style = &quot;chapter-title&quot; or @rnd:style = &quot;preface&quot; or @rnd:style = &quot;preface-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextbook-component">
+            <axsl:variable name="sect1s" select="$nextbook-component/preceding-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;]"/>
+<!-- mode sections 1 -->
+            <book-component xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect1">
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </book-component>
+            <axsl:if test="count($book-components|$nextbook-component) = count($book-components)">
+              <axsl:apply-templates select="$nextbook-component" mode="book-component">
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect1s" select="following-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;]"/>
+<!-- mode sections 2 -->
+            <book-component xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect1">
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </book-component>
+            <axsl:if test="count($book-components|$nextbook-component) = count($book-components)">
+              <axsl:apply-templates select="$nextbook-component" mode="book-component">
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-component">
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-sect1">
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;">
+        <axsl:variable name="nextsect1" select="following-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect1">
+            <axsl:variable name="sect2s" select="$nextsect1/preceding-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect1 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect1>
+            <axsl:if test="$nextsect1 and (not($book-components) or count($sect1s|$nextsect1) = count($sect1s))">
+              <axsl:apply-templates select="$nextsect1" mode="book-sect1">
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect2s" select="following-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect1 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect1>
+            <axsl:if test="$nextsect1 and (not($book-components) or count($sect1s|$nextsect1) = count($sect1s))">
+              <axsl:apply-templates select="$nextsect1" mode="book-sect1">
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect1">
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-sect2">
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;">
+        <axsl:variable name="nextsect2" select="following-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect2">
+            <axsl:variable name="sect3s" select="$nextsect2/preceding-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect2 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect2>
+            <axsl:if test="$nextsect2 and (not($sect1s) or count($sect2s|$nextsect2) = count($sect2s))">
+              <axsl:apply-templates select="$nextsect2" mode="book-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect3s" select="following-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect2 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect2>
+            <axsl:if test="$nextsect2 and (not($sect1s) or count($sect2s|$nextsect2) = count($sect2s))">
+              <axsl:apply-templates select="$nextsect2" mode="book-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect2">
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-sect3">
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;">
+        <axsl:variable name="nextsect3" select="following-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect3">
+            <axsl:variable name="sect4s" select="$nextsect3/preceding-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect3 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect3>
+            <axsl:if test="$nextsect3 and (not($sect2s) or count($sect3s|$nextsect3) = count($sect3s))">
+              <axsl:apply-templates select="$nextsect3" mode="book-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect4s" select="following-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect3 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect3>
+            <axsl:if test="$nextsect3 and (not($sect2s) or count($sect3s|$nextsect3) = count($sect3s))">
+              <axsl:apply-templates select="$nextsect3" mode="book-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect3">
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-sect4">
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;">
+        <axsl:variable name="nextsect4" select="following-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect4">
+            <axsl:variable name="sect5s" select="$nextsect4/preceding-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect4 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect5">
+                <axsl:with-param name="nextsect4" select="$nextsect4"/>
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect5s" select="$sect5s"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect4>
+            <axsl:if test="$nextsect4 and (not($sect3s) or count($sect4s|$nextsect4) = count($sect4s))">
+              <axsl:apply-templates select="$nextsect4" mode="book-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect5s" select="following-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect4 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect5">
+                <axsl:with-param name="nextsect4" select="$nextsect4"/>
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect5s" select="$sect5s"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </sect4>
+            <axsl:if test="$nextsect4 and (not($sect3s) or count($sect4s|$nextsect4) = count($sect4s))">
+              <axsl:apply-templates select="$nextsect4" mode="book-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+                <axsl:with-param name="nextbook" select="$nextbook"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+                <axsl:with-param name="book-components" select="$book-components"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect4">
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-sect5">
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect4)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:when test="@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;">
+        <axsl:variable name="nextsect5" select="following-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;][1]"/>
+<!-- mode sections 3 -->
+        <sect5 xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:apply-templates select="following-sibling::*[1]" mode="book-terminal">
+            <axsl:with-param name="nextsect5" select="$nextsect5"/>
+            <axsl:with-param name="nextsect4" select="$nextsect4"/>
+            <axsl:with-param name="nextsect3" select="$nextsect3"/>
+            <axsl:with-param name="nextsect2" select="$nextsect2"/>
+            <axsl:with-param name="nextsect1" select="$nextsect1"/>
+            <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+            <axsl:with-param name="nextbook" select="$nextbook"/>
+            <axsl:with-param name="sect5s" select="$sect5s"/>
+            <axsl:with-param name="sect4s" select="$sect4s"/>
+            <axsl:with-param name="sect3s" select="$sect3s"/>
+            <axsl:with-param name="sect2s" select="$sect2s"/>
+            <axsl:with-param name="sect1s" select="$sect1s"/>
+            <axsl:with-param name="book-components" select="$book-components"/>
+          </axsl:apply-templates>
+        </sect5>
+        <axsl:if test="$nextsect5 and (not($sect4s) or count($sect5s|$nextsect5) = count($sect5s))">
+          <axsl:apply-templates select="$nextsect5" mode="book-sect5">
+            <axsl:with-param name="nextsect4" select="$nextsect4"/>
+            <axsl:with-param name="nextsect3" select="$nextsect3"/>
+            <axsl:with-param name="nextsect2" select="$nextsect2"/>
+            <axsl:with-param name="nextsect1" select="$nextsect1"/>
+            <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+            <axsl:with-param name="nextbook" select="$nextbook"/>
+            <axsl:with-param name="sect5s" select="$sect5s"/>
+            <axsl:with-param name="sect4s" select="$sect4s"/>
+            <axsl:with-param name="sect3s" select="$sect3s"/>
+            <axsl:with-param name="sect2s" select="$sect2s"/>
+            <axsl:with-param name="sect1s" select="$sect1s"/>
+            <axsl:with-param name="book-components" select="$book-components"/>
+          </axsl:apply-templates>
+        </axsl:if>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect5">
+          <axsl:with-param name="nextsect4" select="$nextsect4"/>
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect5s" select="$sect5s"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-sect1">
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:when test="@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;">
+        <axsl:variable name="nextsect1" select="following-sibling::dbk:para[@rnd:style = &quot;sect1&quot; or @rnd:style = &quot;sect1-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect1">
+            <axsl:variable name="sect2s" select="$nextsect1/preceding-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect1 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect1>
+            <axsl:if test="count($sect1s|$nextsect1) = count($sect1s)">
+              <axsl:apply-templates select="$nextsect1" mode="toplevel-sect1">
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect2s" select="following-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect1 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect1>
+            <axsl:if test="count($sect1s|$nextsect1) = count($sect1s)">
+              <axsl:apply-templates select="$nextsect1" mode="toplevel-sect1">
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect1">
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-sect2">
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:when test="@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;">
+        <axsl:variable name="nextsect2" select="following-sibling::dbk:para[@rnd:style = &quot;sect2&quot; or @rnd:style = &quot;sect2-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect2">
+            <axsl:variable name="sect3s" select="$nextsect2/preceding-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect2 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect2>
+            <axsl:if test="$nextsect2 and (not($sect1s) or count($sect2s|$nextsect2) = count($sect2s))">
+              <axsl:apply-templates select="$nextsect2" mode="toplevel-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect3s" select="following-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect2 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect2>
+            <axsl:if test="$nextsect2 and (not($sect1s) or count($sect2s|$nextsect2) = count($sect2s))">
+              <axsl:apply-templates select="$nextsect2" mode="toplevel-sect2">
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect2">
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-sect3">
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:when test="@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;">
+        <axsl:variable name="nextsect3" select="following-sibling::dbk:para[@rnd:style = &quot;sect3&quot; or @rnd:style = &quot;sect3-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect3">
+            <axsl:variable name="sect4s" select="$nextsect3/preceding-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect3 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect3>
+            <axsl:if test="$nextsect3 and (not($sect2s) or count($sect3s|$nextsect3) = count($sect3s))">
+              <axsl:apply-templates select="$nextsect3" mode="toplevel-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect4s" select="following-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect3 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect3>
+            <axsl:if test="$nextsect3 and (not($sect2s) or count($sect3s|$nextsect3) = count($sect3s))">
+              <axsl:apply-templates select="$nextsect3" mode="toplevel-sect3">
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect3">
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-sect4">
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:when test="@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;">
+        <axsl:variable name="nextsect4" select="following-sibling::dbk:para[@rnd:style = &quot;sect4&quot; or @rnd:style = &quot;sect4-title&quot;][1]"/>
+        <axsl:choose>
+          <axsl:when test="$nextsect4">
+            <axsl:variable name="sect5s" select="$nextsect4/preceding-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;]"/>
+<!-- mode sections 1 -->
+            <sect4 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect5">
+                <axsl:with-param name="nextsect4" select="$nextsect4"/>
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect5s" select="$sect5s"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect4>
+            <axsl:if test="$nextsect4 and (not($sect3s) or count($sect4s|$nextsect4) = count($sect4s))">
+              <axsl:apply-templates select="$nextsect4" mode="toplevel-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:when>
+          <axsl:otherwise>
+            <axsl:variable name="sect5s" select="following-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;]"/>
+<!-- mode sections 2 -->
+            <sect4 xmlns="http://docbook.org/ns/docbook">
+              <axsl:call-template name="copy"/>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect5">
+                <axsl:with-param name="nextsect4" select="$nextsect4"/>
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect5s" select="$sect5s"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </sect4>
+            <axsl:if test="$nextsect4 and (not($sect3s) or count($sect4s|$nextsect4) = count($sect4s))">
+              <axsl:apply-templates select="$nextsect4" mode="toplevel-sect4">
+                <axsl:with-param name="nextsect3" select="$nextsect3"/>
+                <axsl:with-param name="nextsect2" select="$nextsect2"/>
+                <axsl:with-param name="nextsect1" select="$nextsect1"/>
+                <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+                <axsl:with-param name="sect4s" select="$sect4s"/>
+                <axsl:with-param name="sect3s" select="$sect3s"/>
+                <axsl:with-param name="sect2s" select="$sect2s"/>
+                <axsl:with-param name="sect1s" select="$sect1s"/>
+              </axsl:apply-templates>
+            </axsl:if>
+          </axsl:otherwise>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect4">
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-sect5">
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect4)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:when test="@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;">
+        <axsl:variable name="nextsect5" select="following-sibling::dbk:para[@rnd:style = &quot;sect5&quot; or @rnd:style = &quot;sect5-title&quot;][1]"/>
+<!-- mode sections 3 -->
+        <sect5 xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-terminal">
+            <axsl:with-param name="nextsect5" select="$nextsect5"/>
+            <axsl:with-param name="nextsect4" select="$nextsect4"/>
+            <axsl:with-param name="nextsect3" select="$nextsect3"/>
+            <axsl:with-param name="nextsect2" select="$nextsect2"/>
+            <axsl:with-param name="nextsect1" select="$nextsect1"/>
+            <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+            <axsl:with-param name="sect5s" select="$sect5s"/>
+            <axsl:with-param name="sect4s" select="$sect4s"/>
+            <axsl:with-param name="sect3s" select="$sect3s"/>
+            <axsl:with-param name="sect2s" select="$sect2s"/>
+            <axsl:with-param name="sect1s" select="$sect1s"/>
+          </axsl:apply-templates>
+        </sect5>
+        <axsl:if test="$nextsect5 and (not($sect4s) or count($sect5s|$nextsect5) = count($sect5s))">
+          <axsl:apply-templates select="$nextsect5" mode="toplevel-sect5">
+            <axsl:with-param name="nextsect4" select="$nextsect4"/>
+            <axsl:with-param name="nextsect3" select="$nextsect3"/>
+            <axsl:with-param name="nextsect2" select="$nextsect2"/>
+            <axsl:with-param name="nextsect1" select="$nextsect1"/>
+            <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+            <axsl:with-param name="sect5s" select="$sect5s"/>
+            <axsl:with-param name="sect4s" select="$sect4s"/>
+            <axsl:with-param name="sect3s" select="$sect3s"/>
+            <axsl:with-param name="sect2s" select="$sect2s"/>
+            <axsl:with-param name="sect1s" select="$sect1s"/>
+          </axsl:apply-templates>
+        </axsl:if>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect5">
+          <axsl:with-param name="nextsect4" select="$nextsect4"/>
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect5s" select="$sect5s"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="book-terminal">
+    <axsl:param name="nextsect5" select="/.."/>
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect5)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect4)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook-component)"/>
+      <axsl:when test="generate-id() = generate-id($nextbook)"/>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="book-terminal">
+          <axsl:with-param name="nextsect5" select="$nextsect5"/>
+          <axsl:with-param name="nextsect4" select="$nextsect4"/>
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+          <axsl:with-param name="nextbook" select="$nextbook"/>
+          <axsl:with-param name="sect5s" select="$sect5s"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+          <axsl:with-param name="book-components" select="$book-components"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="toplevel-terminal">
+    <axsl:param name="nextsect5" select="/.."/>
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextsect5)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect4)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect3)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect2)"/>
+      <axsl:when test="generate-id() = generate-id($nextsect1)"/>
+      <axsl:when test="generate-id() = generate-id($nexttoplevel-component)"/>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-terminal">
+          <axsl:with-param name="nextsect5" select="$nextsect5"/>
+          <axsl:with-param name="nextsect4" select="$nextsect4"/>
+          <axsl:with-param name="nextsect3" select="$nextsect3"/>
+          <axsl:with-param name="nextsect2" select="$nextsect2"/>
+          <axsl:with-param name="nextsect1" select="$nextsect1"/>
+          <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+          <axsl:with-param name="sect5s" select="$sect5s"/>
+          <axsl:with-param name="sect4s" select="$sect4s"/>
+          <axsl:with-param name="sect3s" select="$sect3s"/>
+          <axsl:with-param name="sect2s" select="$sect2s"/>
+          <axsl:with-param name="sect1s" select="$sect1s"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="*">
+    <axsl:call-template name="copy"/>
+  </axsl:template>
+  <axsl:template match="*" mode="book">
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book"/>
+  </axsl:template>
+  <axsl:template match="*" mode="book-component">
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-component">
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="book-sect1">
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect1">
+      <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="book-sect2">
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect2">
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="book-sect3">
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect3">
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="book-sect4">
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect4">
+      <axsl:with-param name="nextsect3" select="$nextsect3"/>
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="sect4s" select="$sect4s"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="book-sect5">
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nextbook-component" select="/.."/>
+    <axsl:param name="nextbook" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:param select="/.." name="book-components"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="book-sect5">
+      <axsl:with-param name="nextsect4" select="$nextsect4"/>
+      <axsl:with-param name="nextsect3" select="$nextsect3"/>
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nextbook-component" select="$nextbook-component"/>
+      <axsl:with-param name="nextbook" select="$nextbook"/>
+      <axsl:with-param name="sect5s" select="$sect5s"/>
+      <axsl:with-param name="sect4s" select="$sect4s"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+      <axsl:with-param name="book-components" select="$book-components"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-component">
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-component"/>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-sect1">
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect1">
+      <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-sect2">
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect2">
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-sect3">
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect3">
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-sect4">
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect4">
+      <axsl:with-param name="nextsect3" select="$nextsect3"/>
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+      <axsl:with-param name="sect4s" select="$sect4s"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template match="*" mode="toplevel-sect5">
+    <axsl:param name="nextsect4" select="/.."/>
+    <axsl:param name="nextsect3" select="/.."/>
+    <axsl:param name="nextsect2" select="/.."/>
+    <axsl:param name="nextsect1" select="/.."/>
+    <axsl:param name="nexttoplevel-component" select="/.."/>
+    <axsl:param select="/.." name="sect5s"/>
+    <axsl:param select="/.." name="sect4s"/>
+    <axsl:param select="/.." name="sect3s"/>
+    <axsl:param select="/.." name="sect2s"/>
+    <axsl:param select="/.." name="sect1s"/>
+    <axsl:call-template name="copy"/>
+    <axsl:apply-templates select="following-sibling::*[1]" mode="toplevel-sect5">
+      <axsl:with-param name="nextsect4" select="$nextsect4"/>
+      <axsl:with-param name="nextsect3" select="$nextsect3"/>
+      <axsl:with-param name="nextsect2" select="$nextsect2"/>
+      <axsl:with-param name="nextsect1" select="$nextsect1"/>
+      <axsl:with-param name="nexttoplevel-component" select="$nexttoplevel-component"/>
+      <axsl:with-param name="sect5s" select="$sect5s"/>
+      <axsl:with-param name="sect4s" select="$sect4s"/>
+      <axsl:with-param name="sect3s" select="$sect3s"/>
+      <axsl:with-param name="sect2s" select="$sect2s"/>
+      <axsl:with-param name="sect1s" select="$sect1s"/>
+    </axsl:apply-templates>
+  </axsl:template>
+  <axsl:template name="copy">
+    <axsl:copy>
+      <axsl:for-each select="@*">
+        <axsl:copy/>
+      </axsl:for-each>
+      <axsl:apply-templates/>
+    </axsl:copy>
+  </axsl:template>
+</axsl:stylesheet>
diff --git a/xsl/roundtrip/sections2blocks.xsl b/xsl/roundtrip/sections2blocks.xsl
new file mode 100644 (file)
index 0000000..9ebaba6
--- /dev/null
@@ -0,0 +1,263 @@
+<?xml version="1.0"?>
+<axsl:stylesheet xmlns:axsl="http://www.w3.org/1999/XSL/Transform" xmlns:dbk="http://docbook.org/ns/docbook" xmlns:rnd="http://docbook.org/ns/docbook/roundtrip" version="1.0">
+<!--====================================-->
+<!--=                                  =-->
+<!--=   DO NOT EDIT THIS STYLESHEET    =-->
+<!--=                                  =-->
+<!--= This stylesheet is generated     =-->
+<!--= by makeSubsections.xsl and a     =-->
+<!--= mapping specification.           =-->
+<!--=                                  =-->
+<!--= Revision history:                =-->
+<!--=  1.1 2007-01-10 SRB              =-->
+<!--=   Output DocBook 5.0.            =-->
+<!--=  1.0 2005-11-08 SRB              =-->
+<!--=   Initial version.               =-->
+<!--=                                  =-->
+<!--= $Id: makeSubsections.xsl,v 1.1 2007/01/10 02:04:54 steve Exp steve $ =-->
+<!--=                                  =-->
+<!--====================================-->
+  <axsl:output indent="yes"/>
+  <axsl:strip-space elements="*"/>
+  <axsl:preserve-space elements="dbk:para"/>
+  <axsl:template match="dbk:appendix |&#10;  dbk:article |&#10;  dbk:book |&#10;  dbk:chapter |&#10;  dbk:part |&#10;  dbk:preface |&#10;  dbk:section |&#10;  dbk:sect1 |&#10;  dbk:sect2 |&#10;  dbk:sect3 |&#10;  dbk:sect4 |&#10;  dbk:sect5">
+    <axsl:variable name="subsections" select="dbk:para[@rnd:style = &quot;bibliography&quot; or @rnd:style = &quot;bibliography-title&quot; or @rnd:style = &quot;glossary&quot; or @rnd:style = &quot;glossary-title&quot; or @rnd:style = &quot;qandaset&quot; or @rnd:style = &quot;qandaset-title&quot;]"/>
+    <axsl:copy>
+      <axsl:apply-templates select="@*"/>
+      <axsl:choose>
+        <axsl:when test="$subsections">
+          <axsl:apply-templates select="$subsections[1]/preceding-sibling::node()"/>
+          <axsl:apply-templates select="$subsections[1]" mode="subsections">
+            <axsl:with-param name="subsections" select="$subsections[position() != 1]"/>
+          </axsl:apply-templates>
+        </axsl:when>
+        <axsl:when test="dbk:appendix |&#10;  dbk:article |&#10;  dbk:book |&#10;  dbk:chapter |&#10;  dbk:part |&#10;  dbk:preface |&#10;  dbk:section |&#10;  dbk:sect1 |&#10;  dbk:sect2 |&#10;  dbk:sect3 |&#10;  dbk:sect4 |&#10;  dbk:sect5">
+          <axsl:apply-templates select="*[self::dbk:appendix |&#10;  self::dbk:article |&#10;  self::dbk:book |&#10;  self::dbk:chapter |&#10;  self::dbk:part |&#10;  self::dbk:preface |&#10;  self::dbk:section |&#10;  self::dbk:sect1 |&#10;  self::dbk:sect2 |&#10;  self::dbk:sect3 |&#10;  self::dbk:sect4 |&#10;  self::dbk:sect5][1]/preceding-sibling::node()"/>
+          <axsl:apply-templates select="dbk:appendix |&#10;  dbk:article |&#10;  dbk:book |&#10;  dbk:chapter |&#10;  dbk:part |&#10;  dbk:preface |&#10;  dbk:section |&#10;  dbk:sect1 |&#10;  dbk:sect2 |&#10;  dbk:sect3 |&#10;  dbk:sect4 |&#10;  dbk:sect5"/>
+        </axsl:when>
+        <axsl:otherwise>
+          <axsl:apply-templates/>
+        </axsl:otherwise>
+      </axsl:choose>
+    </axsl:copy>
+    <axsl:choose>
+      <axsl:when test="following-sibling::*[self::dbk:appendix |&#10;  self::dbk:article |&#10;  self::dbk:book |&#10;  self::dbk:chapter |&#10;  self::dbk:part |&#10;  self::dbk:preface |&#10;  self::dbk:section |&#10;  self::dbk:sect1 |&#10;  self::dbk:sect2 |&#10;  self::dbk:sect3 |&#10;  self::dbk:sect4 |&#10;  self::dbk:sect5] | following-sibling::dbk:para[@rnd:style = &quot;bibliography&quot; or @rnd:style = &quot;bibliography-title&quot; or @rnd:style = &quot;glossary&quot; or @rnd:style = &quot;glossary-title&quot; or @rnd:style = &quot;qandaset&quot; or @rnd:style = &quot;qandaset-title&quot;]">
+        <axsl:variable name="nextComponent" select="following-sibling::*[self::dbk:appendix |&#10;  self::dbk:article |&#10;  self::dbk:book |&#10;  self::dbk:chapter |&#10;  self::dbk:part |&#10;  self::dbk:preface |&#10;  self::dbk:section |&#10;  self::dbk:sect1 |&#10;  self::dbk:sect2 |&#10;  self::dbk:sect3 |&#10;  self::dbk:sect4 |&#10;  self::dbk:sect5|self::dbk:para[@rnd:style = &quot;bibliography&quot; or @rnd:style = &quot;bibliography-title&quot; or @rnd:style = &quot;glossary&quot; or @rnd:style = &quot;glossary-title&quot; or @rnd:style = &quot;qandaset&quot; or @rnd:style = &quot;qandaset-title&quot;]][1]"/>
+        <axsl:apply-templates select="following-sibling::*[generate-id(following-sibling::*[self::dbk:appendix |&#10;  self::dbk:article |&#10;  self::dbk:book |&#10;  self::dbk:chapter |&#10;  self::dbk:part |&#10;  self::dbk:preface |&#10;  self::dbk:section |&#10;  self::dbk:sect1 |&#10;  self::dbk:sect2 |&#10;  self::dbk:sect3 |&#10;  self::dbk:sect4 |&#10;  self::dbk:sect5|self::dbk:para[@rnd:style = &quot;bibliography&quot; or @rnd:style = &quot;bibliography-title&quot; or @rnd:style = &quot;glossary&quot; or @rnd:style = &quot;glossary-title&quot; or @rnd:style = &quot;qandaset&quot; or @rnd:style = &quot;qandaset-title&quot;]][1]) = generate-id($nextComponent)]"/>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:apply-templates select="following-sibling::*"/>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="subsections">
+    <axsl:param name="subsections" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="@rnd:style = &quot;bibliography&quot; or @rnd:style = &quot;bibliography-title&quot;">
+        <bibliography xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:variable name="bibliodivs" select="following-sibling::dbk:para[@rnd:style = &quot;bibliodiv&quot; or @rnd:style = &quot;bibliodiv-title&quot;]"/>
+          <axsl:choose>
+            <axsl:when test="$bibliodivs">
+              <axsl:apply-templates select="following-sibling::*[1]" mode="bibliodivs">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+                <axsl:with-param name="bibliodivs" select="$bibliodivs[position() != 1]"/>
+              </axsl:apply-templates>
+            </axsl:when>
+            <axsl:otherwise>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+              </axsl:apply-templates>
+            </axsl:otherwise>
+          </axsl:choose>
+        </bibliography>
+      </axsl:when>
+      <axsl:when test="@rnd:style = &quot;glossary&quot; or @rnd:style = &quot;glossary-title&quot;">
+        <glossary xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:variable name="glossdivs" select="following-sibling::dbk:para[@rnd:style = &quot;glossdiv&quot; or @rnd:style = &quot;glossdiv-title&quot;]"/>
+          <axsl:choose>
+            <axsl:when test="$glossdivs">
+              <axsl:apply-templates select="following-sibling::*[1]" mode="glossdivs">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+                <axsl:with-param name="glossdivs" select="$glossdivs[position() != 1]"/>
+              </axsl:apply-templates>
+            </axsl:when>
+            <axsl:otherwise>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+              </axsl:apply-templates>
+            </axsl:otherwise>
+          </axsl:choose>
+        </glossary>
+      </axsl:when>
+      <axsl:when test="@rnd:style = &quot;qandaset&quot; or @rnd:style = &quot;qandaset-title&quot;">
+        <qandaset xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:variable name="qandadivs" select="following-sibling::dbk:para[@rnd:style = &quot;qandadiv&quot; or @rnd:style = &quot;qandadiv-title&quot;]"/>
+          <axsl:choose>
+            <axsl:when test="$qandadivs">
+              <axsl:apply-templates select="following-sibling::*[1]" mode="qandadivs">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+                <axsl:with-param name="qandadivs" select="$qandadivs[position() != 1]"/>
+              </axsl:apply-templates>
+            </axsl:when>
+            <axsl:otherwise>
+              <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+                <axsl:with-param name="nextSubsection" select="$subsections[1]"/>
+              </axsl:apply-templates>
+            </axsl:otherwise>
+          </axsl:choose>
+        </qandaset>
+      </axsl:when>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="*" mode="subsections">
+    <axsl:param name="subsections" select="/.."/>
+    <axsl:copy>
+      <axsl:apply-templates select="@*"/>
+      <axsl:apply-templates mode="subsections"/>
+    </axsl:copy>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="bibliodivs">
+    <axsl:param name="nextSubsection" select="/.."/>
+    <axsl:param name="bibliodivs" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextSubsection)"/>
+      <axsl:when test="@rnd:style = &quot;bibliodiv&quot; or @rnd:style = &quot;bibliodiv-title&quot;">
+        <bibliodiv xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+            <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+            <axsl:with-param name="nextbibliodiv" select="$bibliodivs[1]"/>
+          </axsl:apply-templates>
+        </bibliodiv>
+        <axsl:choose>
+          <axsl:when test="$nextSubsection and         $bibliodivs and         count($nextSubsection/preceding-sibling::* | $bibliodivs[1]) = count($nextSubsection/preceding-sibling::*)">
+            <axsl:apply-templates select="$bibliodivs[1]" mode="bibliodivs">
+              <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+              <axsl:with-param name="bibliodivs" select="$bibliodivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+          <axsl:when test="$bibliodivs">
+            <axsl:apply-templates select="$bibliodivs[1]" mode="bibliodivs">
+              <axsl:with-param name="bibliodivs" select="$bibliodivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="bibliodivs">
+          <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="glossdivs">
+    <axsl:param name="nextSubsection" select="/.."/>
+    <axsl:param name="glossdivs" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextSubsection)"/>
+      <axsl:when test="@rnd:style = &quot;glossdiv&quot; or @rnd:style = &quot;glossdiv-title&quot;">
+        <glossdiv xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+            <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+            <axsl:with-param name="nextglossdiv" select="$glossdivs[1]"/>
+          </axsl:apply-templates>
+        </glossdiv>
+        <axsl:choose>
+          <axsl:when test="$nextSubsection and         $glossdivs and         count($nextSubsection/preceding-sibling::* | $glossdivs[1]) = count($nextSubsection/preceding-sibling::*)">
+            <axsl:apply-templates select="$glossdivs[1]" mode="glossdivs">
+              <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+              <axsl:with-param name="glossdivs" select="$glossdivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+          <axsl:when test="$glossdivs">
+            <axsl:apply-templates select="$glossdivs[1]" mode="glossdivs">
+              <axsl:with-param name="glossdivs" select="$glossdivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="glossdivs">
+          <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="dbk:para" mode="qandadivs">
+    <axsl:param name="nextSubsection" select="/.."/>
+    <axsl:param name="qandadivs" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextSubsection)"/>
+      <axsl:when test="@rnd:style = &quot;qandadiv&quot; or @rnd:style = &quot;qandadiv-title&quot;">
+        <qandadiv xmlns="http://docbook.org/ns/docbook">
+          <axsl:call-template name="copy"/>
+          <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+            <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+            <axsl:with-param name="nextqandadiv" select="$qandadivs[1]"/>
+          </axsl:apply-templates>
+        </qandadiv>
+        <axsl:choose>
+          <axsl:when test="$nextSubsection and         $qandadivs and         count($nextSubsection/preceding-sibling::* | $qandadivs[1]) = count($nextSubsection/preceding-sibling::*)">
+            <axsl:apply-templates select="$qandadivs[1]" mode="qandadivs">
+              <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+              <axsl:with-param name="qandadivs" select="$qandadivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+          <axsl:when test="$qandadivs">
+            <axsl:apply-templates select="$qandadivs[1]" mode="qandadivs">
+              <axsl:with-param name="qandadivs" select="$qandadivs[position() != 1]"/>
+            </axsl:apply-templates>
+          </axsl:when>
+        </axsl:choose>
+      </axsl:when>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="qandadivs">
+          <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="*" mode="terminal">
+    <axsl:param name="nextSubsection" select="/.."/>
+    <axsl:param name="nextbibliodiv" select="/.."/>
+    <axsl:param name="nextglossdiv" select="/.."/>
+    <axsl:param name="nextqandadiv" select="/.."/>
+    <axsl:choose>
+      <axsl:when test="generate-id() = generate-id($nextSubsection)"/>
+      <axsl:when test="generate-id() = generate-id($nextbibliodiv)"/>
+      <axsl:when test="generate-id() = generate-id($nextglossdiv)"/>
+      <axsl:when test="generate-id() = generate-id($nextqandadiv)"/>
+      <axsl:otherwise>
+        <axsl:call-template name="copy"/>
+        <axsl:apply-templates select="following-sibling::*[1]" mode="terminal">
+          <axsl:with-param name="nextSubsection" select="$nextSubsection"/>
+          <axsl:with-param name="nextbibliodiv" select="$nextbibliodiv"/>
+          <axsl:with-param name="nextglossdiv" select="$nextglossdiv"/>
+          <axsl:with-param name="nextqandadiv" select="$nextqandadiv"/>
+        </axsl:apply-templates>
+      </axsl:otherwise>
+    </axsl:choose>
+  </axsl:template>
+  <axsl:template match="*">
+    <axsl:copy>
+      <axsl:apply-templates select="@*"/>
+      <axsl:apply-templates/>
+    </axsl:copy>
+  </axsl:template>
+  <axsl:template name="copy">
+    <axsl:copy>
+      <axsl:apply-templates select="@*"/>
+      <axsl:apply-templates/>
+    </axsl:copy>
+  </axsl:template>
+  <axsl:template match="@*">
+    <axsl:copy/>
+  </axsl:template>
+</axsl:stylesheet>
diff --git a/xsl/roundtrip/wordml2normalise.xsl b/xsl/roundtrip/wordml2normalise.xsl
new file mode 100644 (file)
index 0000000..c201117
--- /dev/null
@@ -0,0 +1,342 @@
+<xsl:stylesheet version="1.0"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
+  xmlns:v="urn:schemas-microsoft-com:vml"
+  xmlns:w10="urn:schemas-microsoft-com:office:word"
+  xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
+  xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
+  xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
+  xmlns:o="urn:schemas-microsoft-com:office:office"
+  xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
+  xmlns:dbk='http://docbook.org/ns/docbook'
+  xmlns:rnd='http://docbook.org/ns/docbook/roundtrip'
+  xmlns:xlink='http://www.w3.org/1999/xlink'
+  xmlns:exsl='http://exslt.org/common'
+  exclude-result-prefixes='w v w10 sl aml wx o dt'
+  extension-element-prefixes='exsl'>
+
+  <xsl:import href='normalise-common.xsl'/>
+
+  <xsl:output method='xml' indent="yes"/>
+
+  <!-- ********************************************************************
+       $Id$
+       ********************************************************************
+
+       This file is part of the XSL DocBook Stylesheet distribution.
+       See ../README or http://nwalsh.com/docbook/xsl/ for copyright
+       and other information.
+
+       ******************************************************************** -->
+
+  <xsl:strip-space elements='*'/>
+  <xsl:preserve-space elements='w:t'/>
+
+  <xsl:key name='style'
+    match='w:style'
+    use='@w:styleId'/>
+
+  <xsl:template match="w:wordDocument">
+    <dbk:article>
+      <xsl:apply-templates select='w:body'/>
+    </dbk:article>
+  </xsl:template>
+
+  <xsl:template match='wx:borders |
+                       wx:margin-left'/>
+
+  <xsl:template match='w:p'>
+    <xsl:variable name='style'>
+      <xsl:call-template name='rnd:map-paragraph-style'>
+        <xsl:with-param name='style' select='w:pPr/w:pStyle/@w:val'/>
+      </xsl:call-template>
+    </xsl:variable>
+    <dbk:para>
+      <xsl:attribute name='rnd:style'>
+        <xsl:value-of select='$style'/>
+      </xsl:attribute>
+      <xsl:if test='w:pPr/w:pStyle/@w:val and
+                    $style != w:pPr/w:pStyle/@w:val'>
+        <xsl:attribute name='rnd:original-style'>
+          <xsl:value-of select='w:pPr/w:pStyle/@w:val'/>
+        </xsl:attribute>
+      </xsl:if>
+      <xsl:apply-templates/>
+    </dbk:para>
+  </xsl:template>
+
+  <xsl:template match='w:r'>
+    <xsl:param name='do-vert-align' select='true()'/>
+
+    <xsl:variable name='role'>
+      <xsl:choose>
+        <xsl:when test='w:rPr/w:b and
+                        w:rPr/w:i'>
+          <xsl:text>bold-italic</xsl:text>
+        </xsl:when>
+        <xsl:when test='w:rPr/w:b'>
+          <xsl:text>bold</xsl:text>
+        </xsl:when>
+        <xsl:when test='w:rPr/w:i'>
+          <xsl:text>italic</xsl:text>
+        </xsl:when>
+        <xsl:when test='w:rPr/w:u'>
+          <xsl:text>underline</xsl:text>
+        </xsl:when>
+        <!-- TODO: add support for other styles -->
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:variable name='style'>
+      <xsl:if test='w:rPr/w:rStyle'>
+        <xsl:value-of select='w:rPr/w:rStyle/@w:val'/>
+      </xsl:if>
+    </xsl:variable>
+
+    <xsl:choose>
+      <xsl:when test='w:pict'>
+        <xsl:variable name='filename'>
+          <xsl:choose>
+            <xsl:when test='contains(w:pict/w:binData/@w:name, "wordml://")'>
+              <xsl:value-of select='substring-after(w:pict/w:binData/@w:name, "wordml://")'/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:text>image</xsl:text>
+              <xsl:value-of select='count(preceding::w:pict) + 1'/>
+              <xsl:text>.jpg</xsl:text>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+
+        <xsl:if test='element-available("exsl:document")'>
+          <exsl:document href='{$filename}.b64' method='text'>
+            <xsl:value-of select='w:pict/w:binData'/>
+          </exsl:document>
+        </xsl:if>
+
+        <dbk:inlinemediaobject>
+          <dbk:imageobject>
+            <dbk:imagedata fileref='{$filename}'>
+              <xsl:if test='w:pict/v:shape/@style'>
+                <xsl:attribute name='width'>
+                  <xsl:value-of select='normalize-space(substring-before(substring-after(w:pict/v:shape/@style, "width:"), ";"))'/>
+                </xsl:attribute>
+                <xsl:attribute name='depth'>
+                  <xsl:value-of select='normalize-space(substring-after(w:pict/v:shape/@style, "height:"))'/>
+                </xsl:attribute>
+              </xsl:if>
+            </dbk:imagedata>
+          </dbk:imageobject>
+        </dbk:inlinemediaobject>
+      </xsl:when>
+      <xsl:when test='$do-vert-align and
+                      w:rPr/w:vertAlign/@w:val = "subscript"'>
+        <dbk:subscript>
+          <xsl:apply-templates select='.'>
+            <xsl:with-param name='do-vert-align' select='false()'/>
+          </xsl:apply-templates>
+        </dbk:subscript>
+      </xsl:when>
+      <xsl:when test='$do-vert-align and
+                      w:rPr/w:vertAlign/@w:val = "superscript"'>
+        <dbk:superscript>
+          <xsl:apply-templates select='.'>
+            <xsl:with-param name='do-vert-align' select='false()'/>
+          </xsl:apply-templates>
+        </dbk:superscript>
+      </xsl:when>
+      <xsl:when test='w:footnoteRef'/> <!-- is a label supplied? -->
+      <xsl:when test='w:footnote'>
+        <dbk:footnote>
+          <xsl:apply-templates/>
+        </dbk:footnote>
+      </xsl:when>
+      <xsl:when test='$role != "" or $style != ""'>
+        <dbk:emphasis>
+          <xsl:if test='$role != ""'>
+            <xsl:attribute name='role'>
+              <xsl:value-of select='$role'/>
+            </xsl:attribute>
+          </xsl:if>
+          <xsl:if test='$style != ""'>
+            <xsl:attribute name='rnd:style'>
+              <xsl:call-template name='rnd:map-character-style'>
+                <xsl:with-param name='style' select='$style'/>
+              </xsl:call-template>
+            </xsl:attribute>
+          </xsl:if>
+          <xsl:apply-templates/>
+        </dbk:emphasis>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:apply-templates/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='w:hlink'>
+    <dbk:link xlink:href='{@w:dest}'>
+      <xsl:apply-templates/>
+    </dbk:link>
+  </xsl:template>
+
+  <!-- Soft returns don't really have an equivalent in DocBook,
+     - except in literal line environments.
+    -->
+  <xsl:template match='w:br'>
+    <xsl:text>&#xa;</xsl:text>
+  </xsl:template>
+
+  <xsl:template match='w:tbl'>
+    <xsl:variable name='border.top'>
+      <xsl:choose>
+        <xsl:when test='w:tblPr/w:tblBorders/w:top[not(@w:val = "nil" or @w:val = "none")]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblBorders/w:top[@w:val = "nil" or @w:val = "none"]'>0</xsl:when>
+        <xsl:when test='w:tr[1]/w:tc[w:tcPr/w:tcBorders/w:top[not(@w:val = "nil" or @w:val = "none")]]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblStyle and
+                        key("style", w:tblPr/w:tblStyle/@w:val)/w:tblPr/w:tblBorders/w:top'>1</xsl:when>
+        <xsl:otherwise>0</xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:variable name='border.bottom'>
+      <xsl:choose>
+        <xsl:when test='w:tblPr/w:tblBorders/w:bottom[not(@w:val = "nil" or @w:val = "none")]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblBorders/w:bottom[@w:val = "nil" or @w:val = "none"]'>0</xsl:when>
+        <xsl:when test='w:tr[1]/w:tc[w:tcPr/w:tcBorders/w:bottom[not(@w:val = "nil" or @w:val = "none")]]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblStyle and
+                        key("style", w:tblPr/w:tblStyle/@w:val)/w:tblPr/w:tblBorders/w:bottom'>1</xsl:when>
+        <xsl:otherwise>0</xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:variable name='border.left'>
+      <xsl:choose>
+        <xsl:when test='w:tblPr/w:tblBorders/w:left[not(@w:val = "nil" or @w:val = "none")]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblBorders/w:left[@w:val = "nil" or @w:val = "none"]'>0</xsl:when>
+        <xsl:when test='w:tr[1]/w:tc[w:tcPr/w:tcBorders/w:left[not(@w:val = "nil" or @w:val = "none")]]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblStyle and
+                        key("style", w:tblPr/w:tblStyle/@w:val)/w:tblPr/w:tblBorders/w:left'>1</xsl:when>
+        <xsl:otherwise>0</xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+    <xsl:variable name='border.right'>
+      <xsl:choose>
+        <xsl:when test='w:tblPr/w:tblBorders/w:right[not(@w:val = "nil" or @w:val = "none")]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblBorders/w:right[@w:val = "nil" or @w:val = "none"]'>0</xsl:when>
+        <xsl:when test='w:tr[1]/w:tc[w:tcPr/w:tcBorders/w:rightt[not(@w:val = "nil" or @w:val = "none")]]'>1</xsl:when>
+        <xsl:when test='w:tblPr/w:tblStyle and
+                        key("style", w:tblPr/w:tblStyle/@w:val)/w:tblPr/w:tblBorders/w:rightt'>1</xsl:when>
+        <xsl:otherwise>0</xsl:otherwise>
+      </xsl:choose>
+    </xsl:variable>
+
+    <dbk:informaltable>
+      <xsl:choose>
+        <xsl:when test='$border.top = "1" and $border.bottom = "1" and
+                        $border.left = "1" and $border.right = "1"'>
+          <xsl:attribute name='frame'>all</xsl:attribute>
+        </xsl:when>
+        <xsl:when test='$border.top = "1" and $border.bottom = "1"'>
+          <xsl:attribute name='frame'>topbot</xsl:attribute>
+        </xsl:when>
+        <xsl:when test='$border.left = "1" and $border.right = "1"'>
+          <xsl:attribute name='frame'>sides</xsl:attribute>
+        </xsl:when>
+        <xsl:when test='$border.top = "1"'>
+          <xsl:attribute name='frame'>top</xsl:attribute>
+        </xsl:when>
+        <xsl:when test='$border.bottom = "1"'>
+          <xsl:attribute name='frame'>bottom</xsl:attribute>
+        </xsl:when>
+      </xsl:choose>
+
+      <!-- TODO: analyse column widths -->
+
+      <dbk:tgroup>
+        <xsl:apply-templates select='w:tblGrid'/>
+        <dbk:tbody>
+          <xsl:apply-templates select='w:tr'/>
+        </dbk:tbody>
+      </dbk:tgroup>
+    </dbk:informaltable>
+  </xsl:template>
+  <xsl:template match='w:tblPr'/>
+  <xsl:template match='w:tblGrid/w:gridCol'>
+    <dbk:colspec colwidth='{@w:w}*'
+      colname='column-{count(preceding-sibling::w:gridCol) + 1}'/>
+  </xsl:template>
+  <xsl:template match='w:tr'>
+    <dbk:row>
+      <xsl:apply-templates/>
+    </dbk:row>
+  </xsl:template>
+  <xsl:template match='w:tc'>
+    <dbk:entry>
+      <xsl:if test='ancestor::w:tbl[1]/w:tblPr/w:tblBorders/w:insideH[not(@w:val = "nil" or @w:val = "none")] |
+                    w:tcPr/w:tcBorders/w:bottom[not(@w:val = "nil" or @w:val = "none")]'>
+        <xsl:attribute name='rowsep'>1</xsl:attribute>
+      </xsl:if>
+      <xsl:if test='ancestor::w:tbl[1]/w:tblPr/w:tblBorders/w:insideV[not(@w:val = "nil" or @w:val = "none")] |
+                    w:tcPr/w:tcBorders/w:right[not(@w:val = "nil" or @w:val = "none")]'>
+        <xsl:attribute name='colsep'>1</xsl:attribute>
+      </xsl:if>
+
+      <xsl:variable name='this.colnum'
+        select='count(preceding-sibling::w:tc) + 1 +
+                sum(preceding-sibling::w:tc/w:tcPr/w:gridSpan/@w:val) -
+                count(preceding-sibling::w:tc/w:tcPr/w:gridSpan[@w:val])'/>
+
+      <xsl:if test='w:tcPr/w:gridSpan[@w:val > 1]'>
+        <xsl:attribute name='namest'>
+          <xsl:text>column-</xsl:text>
+          <xsl:value-of select='$this.colnum'/>
+        </xsl:attribute>
+        <xsl:attribute name='nameend'>
+          <xsl:text>column-</xsl:text>
+          <xsl:value-of select='$this.colnum + w:tcPr/w:gridSpan/@w:val - 1'/>
+        </xsl:attribute>
+      </xsl:if>
+
+      <xsl:if test='w:tcPr/w:vmerge[@w:val = "restart"]'>
+        <xsl:attribute name='morerows'>
+          <xsl:call-template name='rnd:count-rowspan'>
+            <xsl:with-param name='row' select='../following-sibling::w:tr[1]'/>
+            <xsl:with-param name='colnum' select='$this.colnum'/>
+          </xsl:call-template>
+        </xsl:attribute>
+      </xsl:if>
+
+      <xsl:apply-templates/>
+    </dbk:entry>
+  </xsl:template>
+
+  <xsl:template match='w:pStyle|w:rStyle|w:proofErr'/>
+
+  <xsl:template name='rnd:count-rowspan'>
+    <xsl:param name='row' select='/..'/>
+    <xsl:param name='colnum' select='0'/>
+
+    <xsl:variable name='cell'
+      select='$row/w:tc[count(preceding-sibling::w:tc) + 1 +
+              sum(preceding-sibling::w:tc/w:tcPr/w:gridSpan/@w:val) -
+              count(preceding-sibling::w:tc/w:tcPr/w:gridSpan[@w:val]) = $colnum]'/>
+
+    <xsl:choose>
+      <xsl:when test='not($cell)'>
+        <xsl:text>0</xsl:text>
+      </xsl:when>
+      <xsl:when test='$cell/w:tcPr/w:vmerge[not(@w:val = "restart")]'>
+        <xsl:variable name='remainder'>
+          <xsl:call-template name='rnd:count-rowspan'>
+            <xsl:with-param name='row'
+              select='$row/following-sibling::w:tr[1]'/>
+            <xsl:with-param name='colnum' select='$colnum'/>
+          </xsl:call-template>
+        </xsl:variable>
+        <xsl:value-of select='$remainder + 1'/>
+      </xsl:when>
+      <xsl:otherwise>0</xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template match='w:hdr|w:ftr'/>
+
+</xsl:stylesheet>