Basic Filters

The selectors reviewed up to this point in the chapter have always changed the type of nodes being selected. Conversely, when a filter is appended to an expression, it merely restricts the returned set of nodes to a subset of those matching the original expression.

Note that available filters vary per library. Support for filters in jQuery is fairly comprehensive and as such it is used as the primary reference for sections related to filters in this chapter.

  • li:first selects only the first li node found in the document.
  • li:last likewise selects the last li node found in the document.
  • li:even selects all evenly positioned nodes in the document beginning from 0.
  • li:odd likewise selects all oddly positioned nodes in the document, also beginning from 0.
  • li:eq(0) selects the li node with a position of 0 within the set of li nodes (i.e. the first one) in the document.
  • li:gt(0) selects all li nodes with a position greater than 0 within the set of li nodes (i.e. all but the first one) in the document.
  • li:lt(1) selects all li nodes with a position less than 1 within the set of li nodes (i.e. the first one) in the document.
  • :header matches all header nodes. (i.e. h1, h2, etc.)
  • :not(:first) negates the first selector and thus selects all li nodes except the first one in the document.

 

Selector

CSS

XPath

first node

li:first

//li[1]

last node

li:last

//li[last()]

even nodes

li:even

//li[position() mod 2 = 0]

odd nodes

li:odd

//li[position() mod 2 = 1]

specific node

li:eq(0)

//li[1]

all nodes after

li:gt(0)

//li[position() > 1]

all nodes before

li:lt(1)

//li[position() < 2]

header nodes

:header

//h1|//h2|//h3|//h4|//h5|

//h6

all nodes not matching an expression

:not(:first)

//*[not(position() = 1)]

 

Note when reading this table that CSS selectors begin set indices at 0 whereas XPath begins them at 1. 


© CSS Selector Libraries — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (03.09.2014)
Views: 314 | Rating: 0.0/0
Total comments: 0
avatar