SizzleUser Guide

Overview

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

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.

Note: Sizzle has no association with Sizzle - The JavaScript CSS Selector Engine as used by JQuery. Well, except for the name of course!

Install

Install Sizzle with the Fantom Respository Manager:

C:\> fanr install -r http://repo.status302.com/fanr/ afSizzle

Add a dependency to build.fan to use in a Fantom project:

depends = ["sys 1.0", ..., "afSizzle 0+"]

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!

Usage

Sizzle usage is fairly simple and self explanatory; XML document in, XML elements out.

SizzleDoc contains compiled and cached document information and is intended for re-use with multiple CSS selectors:

doc    := SizzleDoc("""<html><p class="welcome">Hello from Sizzle!</p></html>""")
elems1 := doc.select("p.welcome")
elems2 := doc.select("html p")

Currently, whitespace around child and sibling selectors is mandatory, meaning:

  • div > p is valid, and
  • div>p is NOT valid

Note that the CSS selectors are case insensitive; meaning both .stripe and .STRIPE with match <p class="Stripe">.

Have fun!

Release Notes

v0.0.6

  • New: Support for :first-child pseudo-class.
  • New: Support for :lang(xxx) pseudo-class.

v0.0.4

  • New: Added attribute selectors.
  • New: Added checked parameter to SizzleDoc.select()
  • Chg: Optomised DOM search.

v0.0.2

  • New: Preview Release
  • New: Support for universal, type, id, class, descendant, child and adjacent sibling selectors.