Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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/8/xslt/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
Xml 递归排序_Xml_Xslt_Transformation - Fatal编程技术网

Xml 递归排序

Xml 递归排序,xml,xslt,transformation,Xml,Xslt,Transformation,我面临一个问题,我需要根据元素的值对元素进行排序,该值包含一个数字,由句点分隔。我需要根据第一个周期之前的数字值,然后是第一个周期和第二个周期之间的数字,对元素进行排序,依此类推。我不知道,这种等级制度能有多深,这是最大的问题 <?xml version="1.0" encoding="UTF-8"?> <root> <ROW>2.0.1</ROW> <ROW>1.2</ROW> <ROW>

我面临一个问题,我需要根据元素的值对元素进行排序,该值包含一个数字,由句点分隔。我需要根据第一个周期之前的数字值,然后是第一个周期和第二个周期之间的数字,对元素进行排序,依此类推。我不知道,这种等级制度能有多深,这是最大的问题

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <ROW>2.0.1</ROW>
    <ROW>1.2</ROW>
    <ROW>1.1.1</ROW>
    <ROW>1.2.0</ROW>
    <ROW>1</ROW>
</root>

2.0.1
1.2
1.1.1
1.2.0
1.
结果应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <ROW>1</ROW>
    <ROW>1.1.1</ROW>
    <ROW>1.2</ROW>
    <ROW>1.2.0</ROW>
    <ROW>2.0.1</ROW>
</root>

1.
1.1.1
1.2
1.2.0
2.0.1
这可能吗?感谢您的帮助。

有一个不使用任何扩展名的“简单”答案:将行值拆分为卡盘并在其上排序

<xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates select="ROW">
            <xsl:sort select="substring-before(concat(., '.'), '.')" data-type="number"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>
<xsl:template match="ROW">
    <xsl:param name="prefix" select="''"/>
    <xsl:choose>
        <!-- end of recursion, there isn't any more ROW with more chucks -->
        <xsl:when test=". = substring($prefix, 1, string-length($prefix)-1)">
            <xsl:copy-of select="."/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="chuck" select="substring-before(concat(substring-after(., $prefix), '.'), '.')"/>
            <!-- this test is for grouping ROW with same prefix, to skip duplicates -->
            <xsl:if test="not(preceding-sibling::ROW[starts-with(., concat($prefix, $chuck))])">
                <xsl:variable name="new-prefix" select="concat($prefix, $chuck, '.')"/>
                <xsl:apply-templates select="../ROW[starts-with(., $new-prefix) or . = concat($prefix, $chuck)]">
                    <xsl:sort select="substring-before(concat(substring-after(., $new-prefix), '.'), '.')" data-type="number"/>
                    <xsl:with-param name="prefix" select="$new-prefix"/>
                </xsl:apply-templates>
            </xsl:if>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

实现这一点的唯一问题是处理不同文本长度的单个数字(周期之间)(即按该顺序排序1.0、2.0和10.0)。如果单个数字的大小有一个上限(比如说n个数字),那么创建一个排序键,将所有数字从零填充到n个数字。对于n=3,这将导致

Row               Key (string)
1                 001
1.0               001000
1.0.1             001000001
1.1               001001
1.2.1             001002001
2.0.1             002000001
10.0.1            010000001

然后按键排序。如果您陷入XSLT 1.0的泥潭,您将不得不求助于EXSLT扩展函数来进行解析和键规范化。

这并不完美,但您可以使用xalan:nodeset函数将数字“预处理”成一个节点集,该节点集具有Jim描述的易于排序的表达式

此示例适用于Xalan 2.5.1:

<?xml version="1.0"?>

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

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
    <root>
        <!-- Create a sort node with a sort expression wrapping each ROW -->
        <xsl:variable name="nodes">
            <xsl:for-each select="/root/ROW">
                <xsl:variable name="sort-string">
                    <xsl:call-template name="create-sort-string">
                        <xsl:with-param name="sort-string" select="text()" />
                    </xsl:call-template>
                </xsl:variable>
                <sort sort-by="{$sort-string}">
                    <xsl:copy-of select="." />
                </sort>
            </xsl:for-each>
        </xsl:variable>

        <!-- Now sort the sort nodes and copy out the ROW elements -->
        <xsl:for-each select="xalan:nodeset($nodes)/sort">
            <xsl:sort select="@sort-by" data-type="text" />
            <xsl:copy-of select="*" />
        </xsl:for-each>
    </root>
</xsl:template>

<xsl:template name="create-sort-string">
    <xsl:param name="sort-string" />
    <!-- Biggest number at each level -->
    <xsl:variable name="max-num" select="1000" />
    <xsl:choose>
        <xsl:when test="contains($sort-string, '.')">
            <xsl:value-of select="$max-num + number(substring-before($sort-string, '.'))" />
            <xsl:call-template name="create-sort-string">
                <xsl:with-param name="sort-string" select="substring-after($sort-string, '.')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat($max-num + number($sort-string), '0')" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>


我个人认为编写扩展函数会更好,但我知道这并不总是一个选项。

XSLT版本?厌恶脚本/扩展器还是需要纯XSLT?平台?这应该通过Xalan 2.5.1 Java库完成,因此这意味着XLST版本1.2.01应该在2.1之前还是之后?应该有2.01吗?很好,我错过了这段时间。周期之间有数字。你的“唯一问题”恰恰是最难的部分。