pairing sibling nodes with pre-post-order
"Neil W. Van Dyke" <[email protected]> Sat, 3 Apr 2004 10:54:48 -0500
| Newsgroups | gmane.lisp.scheme.ssax-sxml |
|---|---|
| Message-ID | <[email protected]> |
I would like to know if there is a better way to do this with
"pre-post-order"...
In using HtmlPrag and SXML tools to extract information from one Web
page, I need to treat adjacent "td" elements in pairs.
I currently use SXPath to find the "td" elements in a particular part of
the document, then use "pre-post-order" to transform them into the
desired SXML. The following solution uses a variable to save the first
occurrence in a pair:
(pre-post-order
'((td "A1") (td "A2") (td "B1") (td "B2") (td "C1") (td "C2"))
`((td *preorder* .
,(let ((left #f))
(lambda (tag . elems)
(if left
(begin0 (append (list '*pair) left elems)
(set! left #f))
(begin (set! left elems)
'())))))))
==>
(() (*pair "A1" "A2") () (*pair "B1" "B2") () (*pair "C1" "C2"))
One non-"pre-post-order" approach would be to use SXPath to pattern-match
on the children of the "td" elements, and that might be more resilient.
If I were coding functionally in plain Scheme, I could just use pair of
mutually-recursive procedures to effect the alternating behavior. But I
would like to keep all my code within the SXPath + "pre-post-order"
framework for now.
In any case, I am interested in a general "pre-post-order" solution to
this kind of problem.
--
http://www.neilvandyke.org/
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click