按文档顺序对 AS3 XMLList 进行排序

发布于 2024-12-06 04:25:00 字数 597 浏览 3 评论 0原文

在 Actionscript 3 中,有没有一种方法可以按文档顺序对 XMLList 中的节点(XML 类型的实例)进行排序(如 XDM 规范; “通俗地说,文档顺序是节点在文档的 XML 序列化中出现的顺序”)?或者,有没有办法比较两个节点的文档位置?

这是我的意思的一个小例子。在实际情况中,列表是通过更加复杂的过程构建的,并且可能有数百个节点。

var x:XML = <a><b/><b/></a>;
var b0:XML = x.b[0];
var b1:XML = x.b[1];
var l:XMLList = new XMLList();
l += b1;
l += b0;
var sl:XMLList = documentSortFunction(l);
assertTrue(sl[0] === b0);

我不确定我在这里有很大的希望,因为 ECMA-357 (E4X) 似乎并没有真正的文档概念,更不用说文档顺序了。

In Actionscript 3, is there a way to sort the nodes (instances of type XML) in an XMLList in document order (as defined in the XDM spec; "Informally, document order is the order in which nodes appear in the XML serialization of a document")? Alternately, is there a way to compare the document position of two nodes?

Here's a little example of what I mean. In the real case, the list is constructed by a much more complicated process and might have hundreds of nodes.

var x:XML = <a><b/><b/></a>;
var b0:XML = x.b[0];
var b1:XML = x.b[1];
var l:XMLList = new XMLList();
l += b1;
l += b0;
var sl:XMLList = documentSortFunction(l);
assertTrue(sl[0] === b0);

I'm not sure I have a lot of hope here, since it seems that ECMA-357 (E4X) doesn't really have a concept of document, much less document order.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-12-13 04:25:00

嗯,这是我得出的答案的草图。这还没有经过太多测试,但我想我应该在忘记之前记录下来。

这个想法是实现一个函数来为节点生成字符串,使得字符串的顺序与节点的顺序相同。

protected function generateSortID(n:XML):String
{
    var ret:String = '';
    while (n != null && n.childIndex() > -1)
    {
        var s:String = '' + n.childIndex();
        ret = '0000000'.substring(s.length) + s + '!' + ret;
        n = n.parent();
    }
    return ret;
}

protected function compareNodePair(a:Array, b:Array):int
{
    var ai:String = a[0];
    var bi:String = b[0];
    if (ai < bi)
        return -1;
    else if (ai > bi)
        return 1;
    else
        return 0;
}

// all nodes in input list must be part of the same document.
protected function documentSortFunction(l:XMLList):XMLList
{
    var augArr:Array = [];
    for (var il:String in l)
        augArr.push([generateSortID(l[il]), l[il]]);
    augArr.sort(compareNodePair);
    var ret:XMLList = new XMLList();
    for (var ia:String in augArr)
        ret += augArr[ia][1];
    return ret;
}

Well, here's a sketch of the answer I came up with. This hasn't been tested much yet, but I thought I'd record it before I forget to.

The idea is to implement a function to generate a string for a node, such that the order of the strings is the same as the order of the nodes.

protected function generateSortID(n:XML):String
{
    var ret:String = '';
    while (n != null && n.childIndex() > -1)
    {
        var s:String = '' + n.childIndex();
        ret = '0000000'.substring(s.length) + s + '!' + ret;
        n = n.parent();
    }
    return ret;
}

protected function compareNodePair(a:Array, b:Array):int
{
    var ai:String = a[0];
    var bi:String = b[0];
    if (ai < bi)
        return -1;
    else if (ai > bi)
        return 1;
    else
        return 0;
}

// all nodes in input list must be part of the same document.
protected function documentSortFunction(l:XMLList):XMLList
{
    var augArr:Array = [];
    for (var il:String in l)
        augArr.push([generateSortID(l[il]), l[il]]);
    augArr.sort(compareNodePair);
    var ret:XMLList = new XMLList();
    for (var ia:String in augArr)
        ret += augArr[ia][1];
    return ret;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文