从XML元素中删除空格

时间:2012-03-10 00:10:28

标签: xslt

我有以下XML

<list>
    <foo attr1="value" attr2="red"> </foo>
    <foo attr1="xx" attr2="blue"> </foo>
    <foo attr1="yy" attr2="green"> </foo>
</list>

我想成为:

<list>
    <foo attr1="value" attr2="red"/>
    <foo attr1="xx" attr2="blue"/>
    <foo attr1="yy" attr2="green"/>
</list>

是否有用于从foo节点中删除空格的XSLT选项?

2 个答案:

答案 0 :(得分:5)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
<xsl:strip-space elements="foo"/>
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <!--Identity template copies all content -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--If the foo/text() is only whitespace, don't include in output-->
    <xsl:template match="foo/text()[not(normalize-space())]"/>

</xsl:stylesheet>