确定这个节点集的长度?

发布于 2024-09-07 12:17:45 字数 957 浏览 9 评论 0原文

Mozilla 的 Javascript:

  while (result)
    {
    alert(result.childNodes[0].nodeValue);
    //txt=txt+result.childNodes[0].nodeValue+"<br/>";
    result=nodes.iterateNext();
    }

旨在从 xml 文件返回一系列字符串。当我使用警报行时,它会按预期使用一系列正确的字符串发出警报。注释行上的“txt”变量应该转到函数中更下方的innerHTML。但 Firebug 一直告诉我 result.childNodes[0] 未定义。为什么它会在警报中定义而不是在下一行中定义?

我希望有足够的代码来确定问题......如果没有,我会发布更多内容。

感谢帮助

[编辑]

这是结果的定义:

  var x=xmlDoc.responseXML;
  var nodes=x.evaluate(path, x, null, XPathResult.ANY_TYPE, null);
  var result=nodes.iterateNext();

我正在检索 XML

[编辑]

好吧,我将迭代器放入这样的 for 循环中:

  for (var i=0; i<2; i++)
    {
    var res=(xmlNodes.childNodes[0].nodeValue);
    txt=txt+res+"<br/>";
    xmlNodes=xmlQuery.iterateNext();
    }

因为我有一个理论,一旦迭代为空,循环就会失败。这就是发生的事情 - 它一直有效,直到我将循环一个实例设置为高于可用节点的数量。现在我想弄清楚如何获取节点集的“长度”。我该怎么做?

Javascript for Mozilla:

  while (result)
    {
    alert(result.childNodes[0].nodeValue);
    //txt=txt+result.childNodes[0].nodeValue+"<br/>";
    result=nodes.iterateNext();
    }

This is intended to return a series of strings from an xml file. When I use the alert line, it alerts as expected with the proper strings in a series. The "txt" variable on the commented line is supposed to go to an innerHTML further down in the function. But Firebug keeps telling me that the result.childNodes[0] is undefined. Why would it be defined in the alert but not the next line?

I hope that's enough code to determine the problem...if not I will post more.

Thanks for help

[edit]

this is the definition of result:

  var x=xmlDoc.responseXML;
  var nodes=x.evaluate(path, x, null, XPathResult.ANY_TYPE, null);
  var result=nodes.iterateNext();

I am retrieving XML

[edit]

okay I put the iterator in a for loop like this:

  for (var i=0; i<2; i++)
    {
    var res=(xmlNodes.childNodes[0].nodeValue);
    txt=txt+res+"<br/>";
    xmlNodes=xmlQuery.iterateNext();
    }

because I had a theory that once the iteration was null the loop would fail. So that's what happened--it worked until I set the loop one instance higher than the amount of nodes available. now I am trying to figure out how to get the "length" of the node-set. How do I do that?

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

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

发布评论

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

评论(2

无声情话 2024-09-14 12:17:45

到目前为止,我在您显示的代码中没有看到任何问题。如果 childNodes[0] 未定义,则它必须是文本节点或空节点,并且在尝试访问诸如 nodeValue 之类的属性时,您应该会看到异常未定义的 childNodes[0] 。异常将显示在警报或串联或任何其他类型的访问中。

这是您更新的问题的答案。

现在我试图弄清楚如何获取节点集的“长度”。我该怎么做?

您可以从评估函数返回以下节点集类型

  • 迭代器
  • 快照
  • 第一个节点

我将跳过“第一个节点”,因为这不适用于这种情况。

迭代器

使用迭代器,您只能获得一个用于顺序遍历节点的 iterateNext() 方法。迭代器引用活动节点,这意味着如果在遍历结果集时文档中的节点发生更改,迭代器将变得无效。

下面是一个使用迭代器遍历每个结果节点的示例:

var results = doc.evaluate("expr", doc, null, ORDERED_SNAPSHOT, null);
var node;

while(node = results.iterateNext()) {
    console.log(node);
}

如果您想使用迭代器并查找匹配结果的数量,则迭代所有节点并在数组中返回它们的包装函数可能会很有用。

function evaluateXPath(document, expression) {
    var ANY_TYPE = XPathResult.ANY_TYPE;
    var nodes = document.evaluate(expression, document, null, ANY_TYPE, null);
    var results = [], node;

    while(node = nodes.iterateNext()) {
        results.push(node);
    }

    return results;
}

以数组形式获取节点并循环该数组:

var results = evaluateXPath(doc, "expr");

for(var i = 0; i < results.length; i++) {
    console.log(results[i]);
}

快照

快照提供查询时节点的静态结果。对文档的更改不会影响此快照。这里有用的接口是 snapshotLength 属性和 snapshotItem(index) 函数。

以下是使用快照结果的示例:

var ORDERED_SNAPSHOT = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
var results = doc.evaluate("expr", doc, null, ORDERED_SNAPSHOT, null);

for(var i = 0; i < results.snapshotLength; i++) {
    var node = results.snapshotItem(i);
    console.log(node);
}

请参阅工作示例

看来你正在为 Firefox 开发这个。您是否考虑过使用 E4X 来实现此目的?它提供了一个非常简单的界面来处理 XML 文档 - 用于创建、操作和查询。

I don't see any problems in the code you have shown so far. If childNodes[0] is undefined, then it has to be a text node or an empty node, and you should see an exception when trying to access a property such as nodeValue of childNodes[0] which is undefined. The exception will show up on alert or concatenation, or any other type of access.

This is the answer to your updated question.

now I am trying to figure out how to get the "length" of the node-set. How do I do that?

You can have the following types of node-sets returned from the evaluate function:

  • Iterators
  • Snapshots
  • First Nodes

I'll skip "first nodes" as that doesn't apply in this situation.

Iterators

With iterators, you only get an iterateNext() method for traversing nodes sequentially. Iterators refer to live nodes, meaning if the nodes in the document were to change while you are traversing the resultset, the iterator will become invalid.

Here's an example with using an iterator to go over each resulting node:

var results = doc.evaluate("expr", doc, null, ORDERED_SNAPSHOT, null);
var node;

while(node = results.iterateNext()) {
    console.log(node);
}

If you want to use iterators, and find the number of matching results, a wrapper function that iterates through all nodes and returns them in an array might be useful.

function evaluateXPath(document, expression) {
    var ANY_TYPE = XPathResult.ANY_TYPE;
    var nodes = document.evaluate(expression, document, null, ANY_TYPE, null);
    var results = [], node;

    while(node = nodes.iterateNext()) {
        results.push(node);
    }

    return results;
}

Get nodes as an array and loop the array:

var results = evaluateXPath(doc, "expr");

for(var i = 0; i < results.length; i++) {
    console.log(results[i]);
}

Snapshots

Snapshots provide a static result of the nodes at the time of querying. Changes to the document will not affect this snapshot. Useful interfaces here will be the snapshotLength property, and snapshotItem(index) function.

Here's an example using a snapshot result:

var ORDERED_SNAPSHOT = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
var results = doc.evaluate("expr", doc, null, ORDERED_SNAPSHOT, null);

for(var i = 0; i < results.snapshotLength; i++) {
    var node = results.snapshotItem(i);
    console.log(node);
}

See a working example.

It seems you are developing this for Firefox. Did you consider using E4X for this purpose? It provides a really easy interface for dealing with XML documents - for creating, manipulating, and querying.

梦纸 2024-09-14 12:17:45

现在我正在想办法
获取节点集的“长度”。如何
我要这样做吗?

在 XPath 中,这是使用标准 XPath count()< 来实现的/code>函数。

count(someExpression)

计算结果为 someExpression 选择的节点数。

now I am trying to figure out how to
get the "length" of the node-set. How
do I do that?

In XPath this is achieved using the standard XPath count() function.

count(someExpression)

evaluates to the number of nodes selected by someExpression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文