XPath表达式按源顺序选择*所有*元素,文本节点和注释节点 [英] XPath expression to select *all* elements, text nodes, and comment nodes in source order
本文介绍了XPath表达式按源顺序选择*所有*元素,文本节点和注释节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按什么顺序选择所有元素,文本节点和注释节点的XPath表达式是什么?
What’s the XPath expression to select all elements, text nodes, and comment nodes, in the same order as they appear in the document?
以下内容有效地选择了所有元素,但没有选择文本节点和注释节点:
The following effectively selects all elements, but not text nodes and comment nodes:
var result = document.evaluate('//*', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null),
index = -1;
while (++index < result.snapshotLength) {
console.log(result.snapshotItem(index));
}
是否可以执行以下操作?(注意:这是无效的伪代码.)
Is it possible to do something like the following? (Note: this is non-functional pseudo-code.)
document.evaluate('//* and text() and comment()');
推荐答案
//node()
选择所有子节点,即所有元素,文本节点,注释和处理指令(但不选择属性,名称空间节点或文档节点)
selects every node that is a child of something: i.e all elements, text nodes, comments, and processing instructions (but not attributes, namespace nodes, or the document node)
这篇关于XPath表达式按源顺序选择*所有*元素,文本节点和注释节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文