XSLT » Elements » xsl:preserve-space


The xsl:preserve-space element is used to keep white-space only nodes in the output. Note that the default is to leave white-space only nodes. Therefore, it is only necessary to use the xsl:preserve-space element when you use the xsl:strip-space element and wish to insure that certain white-space nodes are not removed. By white-space, we refer to carriage returns, line feeds, spaces and tabs. No text or numbers appear in the node.

The related xsl:strip-space element is used to remove white-space only nodes so that they do not appear in the output.
This is a self-closing element and it cannot contain any child elements or any content.


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="phone address" />
<xsl:preserve-space elements="name" />
<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:value-of select="name" />
<br />
<xsl:value-of select="dob" />
<br />
<xsl:value-of select="age" />
<br />
<xsl:value-of select="address" />
<br />
<xsl:value-of select="phone" />
<hr />

In this example, we preserve white-space nodes for name and remove white nodes for phone and address.

Language(s): XSLT

See Also: