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


XSLT 元素参考手册Complete XSLT Element Reference Manual

Definition and Usage

The <xsl:preserve-space> element is used to define the elements for which whitespace should be preserved.

The <xsl:strip-space> element is used to define the elements for which whitespace should be removed.

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

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


Syntax

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

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

Attributes

Attribute Value Description
elements list-of-element-names

Required. A space-separated list of elements that specifies the elements for which whitespace is to be preserved/removed.

Note:The list may contain "*" and "prefix:*" to include all elements or all elements from a specific namespace.

Example 1

In this example, we preserve whitespace nodes for the title and artist elements, and remove 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>


XSLT 元素参考手册Complete XSLT Element Reference Manual Other Extensions