Re: Native DOM way to get nodes of arbitrary type/name

Bjoern Hoehrmann <[email protected]>
Newsgroups gmane.comp.web.dom.general
Message-ID <[email protected]>
* Marat Tanalin wrote:
>It would be nice to have a native (usable and performant) DOM way for 
>retrieving DOM nodes by node type (or, alternatively, by node name).
>
>This could be represented by these two simple methods:
>
>    * element.getNodesByType(type) -- to get _all_ nodes
>      of specified type contained in the element
>      (like `element.getElementsByTagName('*')` for elements);

Use XPath, the convenient API would have it

  element.selectNodes('.//text()')
  element.selectNodes('.//comment()')
  element.selectNodes('.//processing-instruction()')

but in some browsers you have to use the DOM Level 3 XPath methods,
which are slightly less convenient. Note in particular that XPath
allows you to use predicates to select a subset of "all" the nodes
that would otherwise be returned.

>    * element.getChildNodesByType(type) -- to get _direct child_
>      nodes of the element that have the specified type
>      (like `element.children` for elements).

Drop the `.//` in the paths above for this.
-- 
Björn Höhrmann · mailto:[email protected] · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.