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

Definition and Usage

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

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

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

Note: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 or remove whitespace from.

Note: The list can include "*" and "prefix:*", which allows for the inclusion 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>