保留xsl:sort'd节点之间的空格

时间:2009-02-11 17:51:39

标签: xslt sorting

我正在试图弄清楚如何保存我正在排序的节点之间的空白节点。这是一个例子。

输入:

<a>
    <b>
        <c>
            <d>world</d>
        </c>
        <c>
            <d>hello</d>
        </c>
    </b>
    <e>some other stuff</e>
</a>

期望的输出:

<a>
    <b>
        <c>
            <d>hello</d>
        </c>
        <c>
            <d>world</d>
        </c>
    </b>
    <e>some other stuff</e>
</a>

这是我的xslt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="a/b">
        <xsl:copy>
            <xsl:apply-templates select="c">
                <xsl:sort select="d"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

当我通过xsltproc运行它时,我得到了这个:

<a>
    <b><c>
            <d>hello</d>
        </c><c>
            <d>world</d>
        </c></b>
    <e>some other stuff</e>
</a>

之后我宁愿不经过整理。想法?

2 个答案:

答案 0 :(得分:3)

您希望将这两行添加到样式表的顶部:

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

第一行从文档中删除所有空格,第二行缩进输出。

答案 1 :(得分:1)

您的第二个模板与所有b匹配,但仅在c元素上应用模板。包含的文本节点被丢弃。这就是为什么你没有在输出中看到b和c元素之间的空白。

您必须重新格式化树,因为重新排序后文本节点看起来不会很漂亮(即使您设法包含它们)。安德鲁斯的解决方案就是这样做的。

相关问题