简体   繁体   中英

XSLT: remove white spaces when transforming to HTML

I have a XML doc that is transformed to HTML but I want the result HTML to be as small as possible. So I must remove all white spaces and line endings. How can I do that?

你应该能够使用strip-space

<xsl:strip-space elements="*"/>

Using

<xsl:strip-space elements="*"/>

is a good idea.

So is specifying the details of the output:

<xsl:output
    indent="no"
    method="html"/>

If the above still are not good enough, you can try altering the processing of text() nodes (thinking along the lines of DocBook's schema, where any text you explicitly wanted would be in <para/> tags, or similar):

<xsl:template match="chapter/text()"/>

You can use just match="text()" but that might be too aggressive as it is very vague--it will not necessarily kill the text you want (again, in your <para/> tags, or similar) as those text nodes will probably be processed implicitly by XSLT's built in templates.

xsl:strip-space will let you strip out whitespace from the result tree. Also make sure you don't generate extra whitespace in the stylesheet. That is, make sure instead of something like

<xsl:value-of select="@key"/>
:
<xsl:value-of select="@value"/>

use xsl:text

<xsl:value-of select="@key"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="@value"/>

你应该去

<xsl:strip-space elements="*"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM