使用xslt或xquery转换从xml中删除换行符/空格

时间:2014-04-02 06:31:34

标签: xml xslt xquery

如何使用xslt或xquery transformation从xml中删除换行符/空格。可以将所有元素放在一行中。

1 个答案:

答案 0 :(得分:1)

在xslt中,您可以添加顶级元素

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

使用下面的示例输入XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <a>XXX</a>
    <b>YYY</b>
    <c>ZZZ</c>
</root>

以及以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>

    <!-- this is called an identity template -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

产生

<?xml version="1.0" encoding="utf-8"?><root><a>XXX</a><b>YYY</b><c>ZZZ</c></root>