XSLT <xsl:preserve-space> and <xsl:strip-space> elements

Definition and Usage

The <xsl:preserve-space> element is used to define elements where whitespace is to be preserved.

The <xsl:strip-space> element is used to define elements where whitespace is to be removed.

Note:Preserving whitespace is the default setting, so it is only necessary to use the <xsl:preserve-space> element when using the <xsl:strip-space> element.

Note:Both the <xsl:preserve-space> and <xsl:strip-space> elements are top-level elements (top-level element).

Syntax

<xsl:preserve-space elements="list-of-element-names"/>
<xsl:strip-space elements="list-of-element-names"/>

Attribute

Attribute Value Description
elements list-of-element-names

Required. A space-separated list of elements that specifies which elements to preserve/delete whitespace.

Note: The list can include "*" and "prefix:*", which allows the addition of all elements or all elements from a specific namespace.

Instance

Example 1

In this example, we have reserved whitespace nodes for the title and artist elements, and removed whitespace nodes from the country, company, price, and year elements:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="country company price year" />
<xsl:preserve-space elements="title artist" />
<xsl:template match="/">
  <html>
  <body>
  <xsl:for-each select="catalog/cd">
    <p>
    <xsl:value-of select="title" /><br />
    <xsl:value-of select="artist" /><br />
    <xsl:value-of select="country" /><br />
    <xsl:value-of select="company" /><br />
    <xsl:value-of select="price" /><br />
    <xsl:value-of select="year" />
    </p>
  </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>