#2225 [ANN] Sizzle Preview Release!

SlimerDude Thu 9 Jan 2014

Sizzle v0.0.6 Released!

Sizzle is a library for querying XML documents by means of CSS 2.1 selectors.

Overview

Sizzle currently supports:

  • The Universal selector - *
  • Type selectors - div
  • ID selectors - #id
  • Class selectors - .heading
  • Descendant selectors - html div
  • Child selectors - html > div
  • Adjacent sibling selectors - div + p
  • Any value attribute selector - [att]
  • Exact value attribute selector - [att=val]
  • Whitespace value attribute selector - [att~=val]
  • Language value attribute selector - [att|=val]
  • The pseudo-class :first-child
  • The language pseudo-class :lang(xxx)

The following selectors can not be supported:

  • The link pseudo-classes :link and :visited can not be supported because they refer to a DOM model.
  • The dynamic pseudo-classes :hover, :active, and :focus can not be supported because they require user input.
  • The pseudo-elements :first-line, :first-letter, :before, and :after can not be supported because they do not select XML elements.

Quick Start

1). Create a text file called Example.fan:

using afSizzle

class Example {
    Void main() {
        xml   := """<html><p class="welcome">Hello from Sizzle!</p></html>"""
        elems := Sizzle().selectFromStr(xml, "p.welcome")
        echo(elems.first.text)  // -> Hello from Sizzle!
    }
}

2). Run Example.fan as a Fantom script from the command line:

C:\> fan Example.fan
Hello from Sizzle!

Why?

Sizzle was created primarily for querying (X)HTML documents and web pages for testing web applications.

Lemme know if you have any problems.

Have fun!

brian Fri 10 Jan 2014

This is cool, I had ambitions of adding XPath into the core xml APIs. But find I actually don't do much XML these days so never got around to id. CSS selector is an interesting approach too - maybe even better approach than XPath (at least more known). For people out doing XML, what sort of selection APIs are popular these days?

SlimerDude Fri 10 Jan 2014

I don't know of any other XML selection standards other than XPath and CSS. For instance, Capybara (a popular Ruby testing framework) only supports these two.

XPath always has been the XML selection standard, but it's always seemed large and cumbersome (in both the specification and reference implementations!). By comparison, for testing web apps, CSS has always (seemed to me) a lighter and more natural alternative.

I see them as serving 2 different problem spaces; XPath for information documents, and CSS for visual HTML documents. The 2 documents have very different structures and, hence, require 2 different selection mechanisms.

Anyone who's tried to select a class with XPath knows this! For example:

CSS   = .foo 
XPath = //*[contains(concat(" ", normalize-space(@class), " "), " foo ")]

XPath would be cool, but the specification is too bloated for (the likes of) me to tackle!

Login or Signup to reply.