CSS selectors in Java

CSS selectors are a nice and intuitive alternative to XPath for DOM navigation. While XPath is more complete and has more functionality, CSS selectors were tailored for HTML DOM, where the document content is usually less structured than in XML.

Here are some examples of CSS selector and equivalent XPath expressions:

CSS:   document > library > books > book
XPath: //document/library/books/book

CSS:   document book
XPath: //document//book

CSS:   document book#id3
XPath: //document//book[@id='3']

CSS:   document book[title='CSS for dummies']
XPath: //document//book[@title='CSS for dummies']

 

This becomes more interesting when implementing pseudo-selectors in XPath:

CSS:   book:first-child
XPath: //book[not(preceding-sibling::*)]

CSS:   book:empty
XPath: //book[not(*|@*|node())]

 

A very nice library that allows for parsing selector expressions according to the w3c specification is this “css-selectors” by Christer Sandberg:

https://github.com/chrsan/css-selectors

The next version of jOOX will include css-selector’s parser for simpler DOM navigation. The following two expressions will hold the same result:

Match match1 = $(document).find("book:empty");
Match match2 = $(document).xpath("//book[not(*|@*|node())]");

7 thoughts on “CSS selectors in Java

    1. Hi Rakesh,

      Version 1.0.0 already partially supports CSS selectors in $(..).find(), e.g.

      $(document).find(“books authors > author[name=Lukas]”);

      Cheers
      Lukas

  1. Lukas, it seems that we are bumping into one another in the blogosphere! I started to look at alternative to XPath for testing in order to implement functionality in ActiveWeb that is similar to Ruby on Rails assert_select: assert_select.

    I looked at the css-selectors by https://github.com/chrsan/css-selectors by Christer Sandberg and it seems like a perfect fit to test the views in ActiveWeb. However, I was unable to find it in Maven repository. Could you please share where you sourced it from?

    thank you,
    Igor

    1. Igor, yes, it’s a small world! :-)

      Me too, I was thrilled to find a very comprehensive and standards-compliant CSS selector implementation in Java. I asked Christer if it was OK for me to include the relevant parts directly in jOOX, as I only needed the lexer / parser, not the execution facilities. Christer had no time so far to create a Maven Central artefact, but you could upvote adding css-selectors to Maven here:
      https://github.com/chrsan/css-selectors/issues/3

      Or, optionally, create a dependency on joox, if you only need the scanner, too. It is available here:
      http://repo1.maven.org/maven2/org/jooq/joox/

      Cheers
      Lukas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.