sxml.css-sxpathというモジュールを書きました
sxpathでは専用のクエリ言語・手続き・XPathを使ってSXMLからノードを選択することができますが、合わせてCSSセレクタも扱えるようなモジュールを書きました。
インストール
Gauche-sxml-css-sxpath-0.1.tgzをダウンロードしてgauche-packageコマンドでインストールできます。
$ gauche-package install [-S root] Gauche-sxml-css-sxpath-0.1.tgz
もしくはgithubからgit cloneしてください。
$ git clone git://github.com/teppey/Gauche-sxml-css-sxpath.git $ cd Gauche-sxml-css-sxpath $ ./DIST gen $ ./configure $ make $ make -s check $ [sudo] make install
使い方
(define html '(*TOP* (html (head (title "Hello")) (body (h1 (@ (id "header")) "Hello, world") (p (@ (align "center")) "lorem ipsum") (ul (li (@ (class "foo")) "abc") (li (@ (class "bar")) "123")) (a (@ (href "http://example.com")) "quux"))))) ;IDセレクタ, クラスセレクタ ((css-sxpath "#header") html) ; => ((h1 (@ (id "header")) "Hello, world")) ((css-sxpath "li.bar") html) ; => ((li (@ (class "bar")) "123")) ;属性セレクタ ((css-sxpath "p[align='center']") html) ; => ((p (@ align "center") "lorem ipsum")) ;疑似クラス ((css-sxpath "ul li:first-child") html) ; => ((li (@ (class "foo")) "abc")) ((css-sxpath "ul li:nth-child(2)") html) ; => ((li (@ (class "bar")) "123")) ;セレクタグループ ((css-sxpath "head > title , a") html) ; => ((title "hello") (a (@ (href "http://example.com")) "quux")) ;SXPathのクエリ言語やXPathと一緒に使うこともできます。 ((css-sxpath '("ul li.bar" *text*)) html) ; => ("123") ((css-sxpath '("//body//ul" "li:first-child")) html) ; => ((li (@ (class "foo")) "abc")) ;接頭辞つきの要素名を指定するときは、":"をバックスラッシュでエスケープします。 ((css-sxpath "rss\\:title") '(*TOP* (rss:title "hocus pocus"))) ; => ((rss:title "hocus pocus")) ;sxpathと同様なラッパ関数もあります。 ((car-css-sxpath '("ul li.bar" *text*)) html) ; => "123" ((if-css-sxpath "body blockquote") html) ; => #f
さらに詳しい説明はREADME.jaにあります。よかったら使ってみてください。