Re: string-value of a given element...

[email protected] Mon, 1 Nov 2004 13:26:10 -0800 (PST)
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
Hello!

> I'd like to know how, using tree-traversals, I could create a list of
> all strings belonging to a certain element (and then return it).

Once we have a node, it is easy to get tree traversals to return the
list of all strings that occur within the node and its descendants.
To find the desired node within an SXML document, we can use SXPath.

Let's solve the first problem. It's better to use the version of
pre-post-order defined in 
	SSAX/examples/sxml-to-sxml.scm


(define doc
  '(doc
     (body
       (h1 "header")
       (div (p "p1" "p1 again") (p "p2"))
       (h2 "header")
       (div "div2"))))

(define (strings-of-node node)
  (pre-post-order node
    `((*text* . ,(lambda (tag str) str))
      (*default* . ,(lambda (tag . elems) elems)))))

	(write (strings-of-node doc))

and we get the list of all the strings that occur in the entire `doc'.

To find the desired node, say, the first `div' node in the document,
we can do

	(write ((sxpath '(// (div 1))) doc))


Now, we can just compose sxpath with strings-of-node. We can do it in
two ways:

(write (strings-of-node ((sxpath '(// (div 1))) doc)))

(write ((sxpath `(// (div 1)
		   ,(lambda (node . rest) (strings-of-node node)))) doc))

which are equivalent and give the same results.



-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click