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

What does an XPath expression mean to select all elements, text nodes, and comment nodes in the same order as in the document?

The following efficiently 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 a non-functional pseudo code.)

document.evaluate('//* and text() and comment()');
+5
source share
2 answers
//node()

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

+5
source

XML-. , , , union:

//*|//*/text()|//*/comment()
+1

All Articles