使用 E4X 查找 XML 列表中的不同值

发布于 2024-12-21 08:45:55 字数 394 浏览 4 评论 0原文

考虑以下 XML 片段:

 <book id="5" />
 <book id="15" />
 <book id="5" />
 <book id="25" />
 <book id="35" />
 <book id="5" />

如何编写 E4X 语句/查询来获取 id 属性中的唯一值列表?我在 JavaScript 非浏览器上下文中使用 E4X。

5
15
25
35

是否有任何类型的 distinct()groupby() 方法有帮助?如果不是的话,这怎么可能实现呢?

Consider this XML snippet:

 <book id="5" />
 <book id="15" />
 <book id="5" />
 <book id="25" />
 <book id="35" />
 <book id="5" />

How can I compose an E4X statement/query to get a list of the unique values in the id attribute? I'm using E4X in a JavaScript non-browser context.

5
15
25
35

Is there a distinct() or groupby() method of any kind that would help? If not, how could this be accomplished?

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

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

发布评论

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

评论(2

安静被遗忘 2024-12-28 08:45:55

没有 unique 或 groupby 方法。然而,E4X 允许您快速创建非唯一值的 XMLList。

var xmlSnippet = <stuff><book id="5"/>
                        <book id="15"/>
                        <book id="5"/>
                        <book id="25"/>
                        <book id="35"/>
                        <book id="5"/>
                  </stuff>;
var attributes = xmlSnippet['book'].attribute("id");

为了使其唯一,您可以获取 XMLList,迭代它,并将每个元素作为键值存储在对象中。

var uniqueAttributes = new Object ();

for each (var a in attributes)
{
    uniqueAttributes[a] = null;
}

要访问这些值,请使用 for in 循环(而不是 for every in)迭代它们

for (var u in uniqueAttributes)
{
// do something with u
}

There is no unique or groupby method. However, E4X allow you to quickly create an XMLList of the values non-unique values.

var xmlSnippet = <stuff><book id="5"/>
                        <book id="15"/>
                        <book id="5"/>
                        <book id="25"/>
                        <book id="35"/>
                        <book id="5"/>
                  </stuff>;
var attributes = xmlSnippet['book'].attribute("id");

To make it unique, you can then take the XMLList, iterate through it, and store each element as the key value in an object.

var uniqueAttributes = new Object ();

for each (var a in attributes)
{
    uniqueAttributes[a] = null;
}

To access these values, iterate through them using a for in loop (not a for each in)

for (var u in uniqueAttributes)
{
// do something with u
}
或十年 2024-12-28 08:45:55

这假定所讨论的属性值不包含竖线“|”字符。如果是这样,则将最底部的管道字符更改为值中不存在的其他字符。这些变量是静态类型的,用于 JIT 执行。希望这能满足您的需要。

var idvalues = function (x) { //x = node name
    var a = document.getElementsByTagName(x),
        b,
        c = [],
        d,
        e;
    for (b = a.length - 1; b > -1; b -= 1) { // gather all the id values into an array
        c.push(a[b].getAttribute("id"));
    }
    for (b = c.length - 1; b > -1; b -= 1) {
        if (c[b] !== "") {
            for (d = b - 1; d > -1; d -=1) {
                if (c[b] === c[d]) {
                    c[d] = "";
                }
            }
        }
    }
    e = c.join("|");
    c = e.split("|");
    return c;
};

This presumes the attribute values in question do not contain a pipe, "|", character. If so then change the pipe character at the very bottom to something else not in the values. The variables are statically typed for JIT execution. Hope this does what you need.

var idvalues = function (x) { //x = node name
    var a = document.getElementsByTagName(x),
        b,
        c = [],
        d,
        e;
    for (b = a.length - 1; b > -1; b -= 1) { // gather all the id values into an array
        c.push(a[b].getAttribute("id"));
    }
    for (b = c.length - 1; b > -1; b -= 1) {
        if (c[b] !== "") {
            for (d = b - 1; d > -1; d -=1) {
                if (c[b] === c[d]) {
                    c[d] = "";
                }
            }
        }
    }
    e = c.join("|");
    c = e.split("|");
    return c;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文