Table of contents

Abstract

Introduction

The basic concepts

Stylesheet organization

Root template

Match processing instruction nodes

Match text nodes

Match attributes

Match other nodes

Keepspace template

Using the stylesheet

The complete stylesheet

Match other nodes

This template handles any node that isn’t a processing instruction or text node (essentially, elements and comments).

The template examines the xml:space attribute to test if the element preserves space (used in <codeblock> and <pre> elements). If xml:space is set to preserve, the element is copied and its contents are processed using the keepspace mode, which does a literal copy, including all whitespace in the element’s contents. If you’re not processing DITA content, you may have to modify this test look for the elements in which space should be preserved.

If the element doesn’t preserve space, the element is copied and its contents are processed using the identity mode.

<xsl:template match="node()[not(self::processing-instruction()) and not(self::text())]" mode="identity">
    <xsl:choose>
        <!-- If the element specifies xml:space="preserve",
             cannot mess with the whitespace, so use keepspace template. -->
        <xsl:when test="@xml:space = 'preserve'">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" mode="keepspace"/>
            </xsl:copy>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" mode="identity"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

NOTE: Rather than test the name of the node to a series of possible element names (name(.)='codeblock'), you could use a “class contains” predicate:*[contains(@class,' topic/pre ')].

If you need to modify or eliminate specific comment nodes, you can create a separate template to handle comment nodes.

 

Next Page:
Keepspace template


Scriptorium Publishing | Post Office Box 12761 Research Triangle Park, NC 27709 | (919) 481 2701 | info@scriptorium.com