Child Filters

Child filters are fairly similar to the basic filters reviewed earlier, except applied to child nodes.

  • ul:nth-child(2) selects the second child element within each ul node. The parameter passed to the filter can also be even or odd (which are figured relative to child position within the parent element) or can use expressions involving a variable n (such as 3n for every third child).
  • li:first-child selects all li nodes that are the first child of their parent node.
  • li:last-child likewise selects all li nodes that are the last child of their parent node.
  • li:only-child selects all li nodes that are the only child of their parent node.

Selector

CSS

XPath

nth child nodes

ul:nth-child(2)

//ul/*[position() = 2]

first child nodes

li:first-child

//*/*[name() = “li" and position() = 1]

last child nodes

li:last-child

//*/*[name() = “li" and position() = last()]

only child nodes

li:only-child

//*/*[name() = “li" and count() = 1]


© CSS Selector Libraries — Web Scraping

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