Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 保留xsl:sort';d节点_Xslt_Sorting - Fatal编程技术网

Xslt 保留xsl:sort';d节点

Xslt 保留xsl:sort';d节点,xslt,sorting,Xslt,Sorting,我试图找出如何在我正在排序的节点之间保留空白节点。这里有一个例子 输入: <a> <b> <c> <d>world</d> </c> <c> <d>hello</d> </c> </b> <e>some other st

我试图找出如何在我正在排序的节点之间保留空白节点。这里有一个例子

输入:

<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>

你好
世界
一些其他的东西

我不想事后再看一遍。想法?

您需要在样式表顶部添加以下两行:

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


第一行去掉文档中的所有空白,第二行缩进输出。

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

您必须重新设置树的格式,因为文本节点在重新排序后看起来并不漂亮(即使您设法将它们包括在内)。安德鲁斯解决方案将做到这一点