javascript - XPath expression to select *all* elements, text nodes, and comment nodes in source order - Stack Overflow

What’s the XPath expression to select all elements, text nodes, and ment nodes, in the same order as th

What’s the XPath expression to select all elements, text nodes, and ment nodes, in the same order as they appear in the document?

The following effectively selects all elements, but not text nodes and ment 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 ment()');

What’s the XPath expression to select all elements, text nodes, and ment nodes, in the same order as they appear in the document?

The following effectively selects all elements, but not text nodes and ment 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 ment()');
Share Improve this question asked May 30, 2012 at 10:59 Mathias BynensMathias Bynens 150k54 gold badges222 silver badges251 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
//node()

selects every node that is a child of something: i.e all elements, text nodes, ments, and processing instructions (but not attributes, namespace nodes, or the document node)

Tested with text and ment nodes in an XML document. There may be a more efficient approach, but this returns elements, text and ment nodes in document order for me, using union:

//*|//*/text()|//*/ment()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096712a4611023.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信