XPath Predicates

Predicates are filters that restrict the nodes selected by an XPath expression. Predicates appear inside [square brackets].

Courses

  1. /courses/course[@acad_year = 2009][@offered = 'Y']
  2. /courses/course[@acad_year = 2009 and @offered = 'Y']

Congress

  1. /congress/person[role/@type = 'sen']
  2. /congress/person[role/@state = 'CA']

RSS

  1. /rss/channel/item[position() = 1]
  2. /rss/channel/item[position() = last()]
  3. /rss/channel/item[position() mod 2 = 0]
  4. /rss/channel/item[position() mod 2 = 0]/title
  5. /rss/channel/item[1]/title

XHTML

  1. /html/head/title
  2. /html/body//a[@href]

What is the difference between the following?

Copyright © 2002-2009 David P. Heitmeyer

Bookmark and Share