DocBook CookBook
Updated
11 Apr 2008
    Linking
    DocBook XSL
      +Removing html wrappers <-
       Online Formatting
    Printing

Removing html, head, and body elements from html output

Problem

You need to remove the <html>, <head>, and <body> elements that wrap a stand-alone html page. This might be needed to generate “naked” html that will be inserted into an existing web page.

Solution

Customize the template that generates these wrappers. If you use the chunking stylesheets, customize the template named “chunk-element-content” in html/chunk-common.xsl. If you use the non-chunking stylesheets, customize the template that matches “*” with mode="process.root" in html/docbook.xsl.

You will see that these templates generate all three elements. The two examples below remove all three elements and all header and footer content. If you need to just remove one or two of these elements, you can modify the template as necessary.

Example 1. Modified chunk-element-content template

  <xsl:template name="chunk-element-content">
    <xsl:param name="prev"/>
    <xsl:param name="next"/>
    <xsl:param name="nav.context"/>
    <xsl:param name="content">
      <xsl:apply-imports/>
    </xsl:param>

    <xsl:call-template name="user.preroot"/>
    <xsl:copy-of select="$content"/>
    <xsl:value-of select="$chunk.append"/>
  </xsl:template>
      
      

Example 2. Modified mode="process.root" template

  <xsl:template match="*" mode="process.root">
    <xsl:variable name="doc" select="self::*"/>

    <xsl:call-template name="user.preroot"/>
    <xsl:call-template name="root.messages"/>
    <xsl:apply-templates select="."/>
    <xsl:value-of select="$html.append"/>
  </xsl:template>
      
      

Discussion

The original chunk-element-content and mode="process.root" templates generate all three html wrapper elements and also insert header and footer content. They also allow you to add content before (user.preroot) and after (html.append or chunk.append) other content. The two examples remove all three elements and all header and footer content, but leave in the pre and post content, if any.

If you need to just remove one or two of these elements, or you want to keep the header or footer content, you will find that the original templates are reasonably clear and can be modified easily.

Reference