views:

47

answers:

2

I know how to quickly extract text nodes from a DOM:

document.evaluate('//text()', document, null, XPathResult.ANY_TYPE, null)

But is there an easy way to exclude text from SCRIPT, STYLE, or other tags that are not shown to the user?

Something like:

'//text()[ parent.name not in ("SCRIPT", "STYLE") ]'

Thanks, Mike

+3  A: 
//*[not(self::script or self::style)]/text()
Nick Jones
@Nick Jones: +1 Good answer.
Alejandro
A: 

Besides Nick Jones correct answer, for more complex exclusion you should use XPath node set exclusion expression:

$ns1[not(count(.|$ns2)=count($ns2))]

In this case:

//*[not(count(.|//script|/*/*/style)=count(//script|/*/*/style))]/text()
Alejandro