The simplest solution is to use the SXPath library. This is a Scheme implementation of XPath. The query above would be written as
> (definequery (sxpath'(// (a (@ (equal? (class"hometoc")))) *text*)))
> (definedocument'(*TOP*
(html (head (title"Foo"))
(body (h1"Foo")
(p
(a (@ (class"bar")) "don't want this")
(a (@ (class"foo")) "this is the answer"))))))
> (querydocument)
("this is the answer")
The meaning of the query is as follows:
// search in all the elements of the tree
a for elements called a
(@ (equal? (class "hometoc"))) with an attribute named class with value "hometoc"
SXPath works on SXML in the example above. To convert XML to SXML, use the SSAX parser. To convert badly-formed HTML to SXML use HtmlPrag. If you use WebIt there are versions of SXPath and SSAX that work with it.