Skip to content

Getting Started: 5. CSS Queries

Daniele Orlando edited this page Dec 17, 2020 · 4 revisions

XPath is very powerful but CSS Selectors are more concise sometimes.

Concise syntax

$eggs   = $food('egg');
$fruits = $food('fruit[price="expensive"');

Extended syntax

$eggs   = $food->query('egg');
$fruits = $food->query('fruit[price="expensive"]');

Pro Tip:
Like for XPath, query() supports a variable number of CSS queries. In addition, XPaths and CSS Selectors can be mixed in the same query.

$food('//title', 'chapters > chapter');

The supported CSS selectors are:

  • #id
  • .class
  • E[attr="val"]
  • *
  • :root
  • namespace|E
  • E E
  • E > E
  • E + E
  • E ~ E
  • E , E

Note:
Remember that CSS selectors are translated internally to XPath and this operation adds some overhead. If you need the best performances possible, go with XPath.

6. Iterating And Filtering 〉