使用XPath在DOM中搜索相同字符串的倍数 [英] Searching the DOM for multiples of the same string, using XPath

查看:75
本文介绍了使用XPath在DOM中搜索相同字符串的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Chrome扩展程序,它将搜索DOM并突出显示页面上的所有电子邮件地址。我发现它可以在页面上查找符号,但是只有在有一个电子邮件地址时它才能正确返回,而在找到多个地址时它会中断。

I'm writing a Chrome extension that will search the DOM and highlight all email addresses on the page. I found this to look for at symbols on the page but it only returns correctly when there is one email address, it breaks when there are multiple addresses found.

found = document.evaluate('//*[contains(text(),"@")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);

如果找到多个返回值,此返回倍数的正确方法是什么?

What is the correct way to have this return multiples if more than one is found?

推荐答案

如果要处理多个结果,请不要调用 .snapshotItem(0) document.evaluate()上,而是使用 for 循环和遍历结果snapshotLength()

If you want to handle multiple results, don’t call .snapshotItem(0) on document.evaluate() but instead loop through the results using a for loop and snapshotLength():

var nodesSnapshot = document.evaluate('//*[contains(text(),"@")]',
    document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ )
{
  console.dir( nodesSnapshot.snapshotItem(i) );
}

要么,要么指定 XPathResult.UNORDERED_NODE_ITERATOR_TYPE 参数(而不是 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE ),并使用 while 循环和 iterateNext()

Either that, or specify the XPathResult.UNORDERED_NODE_ITERATOR_TYPE argument (instead of XPathResult.ORDERED_NODE_SNAPSHOT_TYPE), and use a while loop with iterateNext():

var iterator = document.evaluate('//*[contains(text(),"@")]',
    document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null );

try {
  var thisNode = iterator.iterateNext(); 
  while (thisNode) {
    console.dir( thisNode );
    thisNode = iterator.iterateNext();
  } 
}
catch (e) {
  console.log( 'Error: Document tree modified during iteration ' + e );
}






在某些情况下与该问题相反的情况(例如,当您真正只是想获取第一个匹配节点时),您可以指定 XPathResult.FIRST_ORDERED_NODE_TYPE 值,只返回一个节点,然后使用属性(不是方法) singleNodeValue


In cases that are sorta the reverse of the one in this question—cases when you really do just want to get the first matching node—you can specify the XPathResult.FIRST_ORDERED_NODE_TYPE value, to return just a single node, and then use the property (not method) singleNodeValue:

var firstMatchingNode = document.evaluate('// [contains(text(),"@")]',
    document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null );

console.dir( firstMatchingNode.singleNodeValue );






获取文本或倒数,或者测试为true / false条件



请注意,您可以将其他值(常量)指定为 document.evaluate() 的倒数第二个参数,以获取其他结果类型直接返回:


Getting text or counts back instead, or testing true/false conditions

Note that among the other values (constants) you can specify as the second-to-last argument to document.evaluate() to get other results types, you can make it directly return:


  • 单个字符串( XPathResult.STRING_TYPE )从文件的某些部分

  • 一个表示某种
    计数的数字( XPathResult.NUMBER_TYPE );例如,对在文档中找到的
    个电子邮件地址的数量进行计数

  • 一个布尔值( XPathResult.BOOLEAN_TYPE )代表文档的某些正确/错误方面;例如,一个指示符,指示文档是否包含任何电子邮件地址

  • a single string (XPathResult.STRING_TYPE) of text slurped from some part of the document
  • a number representing a count of some kind (XPathResult.NUMBER_TYPE); for example, a count of the number of e-mail addresses found in the document
  • a boolean value (XPathResult.BOOLEAN_TYPE) representing some true/false aspect of the document; e.g., an indicator whether or not the document contains any e-mail addresses

当然,要获取其他结果类型,XPath您作为 document.evaluate()的第一个参数给出的表达式必须是一个实际上将返回字符串,数字或布尔值(而不是返回)的表达式。一组属性节点或元素节点)。

Of course to get those other result types back, the XPath expression you give as the first argument to document.evaluate() needs to be an expression that will actually return a string, or a number, or a boolean value (instead of returning a set of attribute nodes or element nodes).

以上示例均基于MDN 在JavaScript中使用XPath的简介教程,强烈建议尝试使用XPath和 document.evaluate()的任何人。

The examples above are all based on the MDN Introduction to using XPath in JavaScript tutorial, which is highly recommended to anybody trying to work with XPath and document.evaluate().

这篇关于使用XPath在DOM中搜索相同字符串的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆