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)
Bonus CSS3 selectors:
- The pseudo-class
:last-child - The pseudo-class
:nth-child(n)(basic numeric implementation) - The pseudo-class
:nth-last-child(n)(basic numeric implementation)
The following selectors can not be supported:
- The link pseudo-classes
:linkand:visitedcan not be supported because they refer to a DOM model. - The dynamic pseudo-classes
:hover,:active, and:focuscan not be supported because they require user input. - The pseudo-elements
:first-line,:first-letter,:before, and:aftercan 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 xml
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 > pis valid, anddiv>pis NOT valid
Note that the CSS selectors are case insensitive; meaning both .stripe and .STRIPE will match <p class="Stripe">.
Have fun!
Release Notes
v1.0.0
- New: Support for
:last-childCSS3 pseudo-class. - New: Support for
:nth-child(n)CSS3 pseudo-class. (basic numeric implementation) - New: Support for
:nth-last-child(n)CSS3 pseudo-class. (basic numeric implementation)
v0.0.6
- New: Support for
:first-childpseudo-class. - New: Support for
:lang(xxx)pseudo-class.
v0.0.4
- New: Added attribute selectors.
- New: Added
checkedparameter toSizzleDoc.select() - Chg: Optomised DOM search.
v0.0.2
- New: Preview Release
- New: Support for universal, type, id, class, descendant, child and adjacent sibling selectors.