10.2. The <xsl:strip-space> and <xsl:preserve-space> Top-Level Elements

These elements form a complementary pair of XSLT top-level elements whose purpose is to govern the handling of whitespace in the input tree and, by implication, in the output result tree. Both elements have one required attribute, the elements attribute, whose value (tokens) is a whitespace-separated list of element-type names, as shown in the element model definitions for each element below:

<!-- Category: top-level-element -->
<xsl:strip-space
  elements = tokens />

<!-- Category: top-level-element -->
<xsl:preserve-space
  elements = tokens />

The <xsl:preserve-space> top-level element retains whitespace text nodes from the input elements listed in its elements attribute. The <xsl:strip-space> element has the opposite effect of <xsl:preserve-space>. Whitespace text nodes are stripped from the input prior to processing.

The value of the elements attribute contains a list of the elements that will be acted on to preserve the whitespace text nodes they contain, if used with <xsl:preserve-space>, or to strip them, if used with <xsl:strip-space>. All elements in the input document are defaulted to preserve space unless they are explicitly added to the <xsl:strip-space> elements attribute.

If there are conflicts in the matching of elements identified by their element-type name for either of the pair of these top-level elements in a given XSLT stylesheet, the standard rules of precedence discussed in Chapter 7 for <xsl:import> and <xsl:include>, and template rules in general, are used. Any <xsl:preserve-space> or <xsl:strip-space> elements with lower precedence due to being imported are ignored. The default priorities for the names (an element-type name or attribute name receives higher priority) determine precedence.

It should be noted that the <xsl:strip-space> and <xsl:preserve-space> elements do not operate on the text nodes of the specified elements unless those text nodes contain only whitespace. A text node with text other than whitespace will be sent to the output intact. Any excessive whitespace between words in a text node will appear as it did in the input.

For example, if a <block> in our Markup City contained extra whitespace, such as:

<block>1st Street              </block>
<block>2nd       Street</block>
<block>          3rd Street</block>

Specifying "block" as the value for the elements attribute of <xsl:strip-space> would not remove the extra spaces. If, however, there were extra line breaks between <block> elements, specifiying "thoroughfare" as the parent of <block> elements would remove the extra line breaks between the <block>s, as shown in Example 10-1

Notice that the whitespace between text in a node is not stripped, but the extra line breaks, which are considered to be whitespace nodes, are stripped.

Example 10-1. Removing line breaks from Markup City.
					INPUT:

<?xml version="1.0"?>
       <thoroughfare>
                    <sidestreet>Bob Wallace Avenue</sidestreet>
                    <block>1st Street            </block>



                    <block>2nd       Street</block>


                    <block>          3rd Street</block>
                    <sidestreet>Woodridge Street</sidestreet>
       </thoroughfare>

STYLESHEET:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
<xsl:strip-space elements="thoroughfare"/>
<xsl:template match="*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

OUTPUT:

<?xml version="1.0" encoding="utf-8"?>
<thoroughfare><sidestreet>Bob Wallace Avenue</sidestreet><block>1st
Street          </block><block>2nd     Street</block><block>
3rd Street</block><sidestreet>Woodridge
Street</sidestreet></thoroughfare>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset