Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath expression to select *all* elements, text nodes, and comment nodes in source order

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()');
like image 861
Mathias Bynens Avatar asked May 30 '12 10:05

Mathias Bynens


1 Answers

//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)

like image 128
Michael Kay Avatar answered Nov 15 '22 03:11

Michael Kay