Re: this use of pre-post-order

"Neil W. Van Dyke" <[email protected]> Sat, 3 Apr 2004 10:34:08 -0500
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
[This is a resend of an earlier message that got lost.]

From: "Neil W. Van Dyke" <[email protected]>
To: [email protected]
Subject: this use of pre-post-order
Date: Mon, 22 Mar 2004 14:05:32 -0500

Hello.  I have been using SXPath and Pre-post-order to extract
information from Web pages parsed with HtmlPrag, and would appreciate
any comments...

One simple but representative problem that I thought would be a good
exercise is to transform to the string "John Smith" the following SXML
representation of HTML from a popular Web site:

    (td (img (@ (id "imgFirstLetter") (src "alph/Js.gif") (border "0")))
        (font (@ (class "L"))
              (span (@ (id "lblName")) "ohn&nbsp;&nbsp;Smith")))

Attached to the end of this email is my first Pre-post-order approach,
which yields the following result:

    ((((() "J" ())) (() (() "ohn&nbsp;&nbsp;Smith"))))

This is the desired result (except for the obvious HTML whitespace
conversion, which will mostly be done at an earlier step, and the string
concatenation).

Is there a much better way to do this with Pre-post-order, or what
improvements could be made to the way I am currently doing it?

Thanks,
Neil

---------- Begin Included Text ----------
;; The 'require' and 'regexp-match' are PLT-specific.

(require (lib "sxml-tree-trans.ss" "sxml"))
         
(define sxml
  '(
    (td (img (@ (id "imgFirstLetter") (src "alph/Js.gif") (border "0")))
        (font (@ (class "L"))
              (span (@ (id "lblName")) "ohn&nbsp;&nbsp;Smith")))
    ))

(define (nothing      . args)  '())
(define (children tag . elems) elems)
(define (text-val tag str)     str)

(define bindings
  `(
    (img ((@ ((src . ,(lambda (attr val)
                        (let ((m (regexp-match
                                  #rx"^alph/([A-Z0-9])[bps]\\.gif$"
                                  val)))
                          (if m
                              (list-ref m 1)
                              '()))))
              (*default* . ,nothing))
             . ,children))
         . ,children)
    (*text*                     . ,text-val)
    (*default* ((@ . ,nothing)) . ,children)
    ))

(pre-post-order sxml bindings)
---------- End Included Text ----------

-- 
                                             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