Latest web development tutorials

XSLT <xsl:preserve-space> 和<xsl:strip-space> 元素

XSLT 元素參考手冊 完整的XSLT元素參考手冊

定義和用法

<xsl:preserve-space> 元素用於定義保留空白的元素。

<xsl:strip-space> 元素用於定義刪除空白的元素。

註釋:保留空白是默認的設置,所以只有當使用<xsl:strip-space>元素時才有必要使用<xsl:preserve-space>元素。

註釋: <xsl:preserve-space>元素和<xsl:strip-space>元素都是頂層元素(top-level element)。


語法

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

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

屬性

属性 描述
elements list-of-element-names

必需。一个空格分隔的元素列表,规定了保留/删除空白的元素。

注意:列表中可包含 "*" 和 "prefix:*",这样就可以加入所有元素或来自特定命名空间的所有元素。

實例1

在本例中,我們為title 和artist 元素預留了空白節點,並從country、company、price 以及year 元素刪除了空白節點:

<?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 元素參考手冊 完整的XSLT元素參考手冊