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)
Note: Sizzle has no association with Sizzle - The JavaScript CSS Selector Engine as used by JQuery. Well, except for the name of course!
Install Sizzle with the Fantom Pod Manager ( FPM ):
C:\> fpm install afSizzle
Or install Sizzle with fanr:
C:\> fanr install -r http://eggbox.fantomfactory.org/fanr/ afSizzle
To use in a Fantom project, add a dependency to build.fan:
depends = ["sys 1.0", ..., "afSizzle 1.1"]
Full API & fandocs are available on the Eggbox - the Fantom Pod Repository.
-
Create a text file called
Example.fanusing xml using afSizzleclass Example { Void main() { xhtml := "
Hello from Sizzle!
" elems := SizzleXml(xhtml).select("p.welcome") echo(elems.first.text) // --> Hello from Sizzle! } } -
Run
Example.fanas a Fantom script from the command line: C:> fan Example.fan Hello from Sizzle!
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 := SizzleXml("<html><p class='welcome'>Hello from Sizzle!</p></html>")
elems1 := doc.select("p.welcome")
elems2 := doc.select("html p")
Sizzle only works with parsed XML documents, meaning your data has to be well formed.
HTML allows dodgy, and non well formed, syntax such as void elements <br>, and empty attributes <input required>. If your HTML contains such syntax then it needs to be converted to XML before it can be used with Sizzle.
Use HTML Parser to do this.
Also note that XML does not allow named character references such as - they all need to be replaced with numerical character references such as   or  
Note that the CSS selectors in Sizzle are case insensitive; meaning both .stripe and .STRIPE will match <p class="Stripe">.
Whitespace around child and sibling selectors is mandatory, meaning:
div > pis valid, anddiv>pis NOT valid
Also, the following selectors are not / 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.