RE: Best way to label hierarchical sections with their depths

"T. Kurt Bond" <[email protected]> Mon, 27 Oct 2003 16:36:55 -0500
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
I wrote:
> I'm a novice just starting to work with SXML and SXSLT.
> 
> I have a document that looks something like this:
[Changed slightly for easier pasting into a repl:]

    (define doc2
      '(article
	(section (title "T1") (para "text")
	 (section (title "T1.1") (para "text")
	  (section (title "T1.1.1") (para "text"))
	  (section (title "T1.1.2") (para "text")))
	 (section (title "T1.2") (para "text")))
	(section (title "T2") (para "text"))))

[I gave two partial solutions, one of only handled sections nested
three deep and one of which required changing pre-post-order to pass
the current bindings to the handlers.]

> Is there a better approach to this sort of thing?

Not long after sending the original e-mail I thought of a better
solution, after looking the number-sections function in the SXSLT
talk: 

    (define (level-sections level sections)
      (map (lambda (el)
	     (pre-post-order el
	       `((section *preorder* 
		  . ,(lambda (tag . elems)
		       (cons '*section
			     (cons level
				   (level-sections (+ 1 level) elems)))))
		 (*default* *preorder* . ,(lambda x x))
		 (*text* . ,(lambda (tag str) str)))))
	   sections))

    (define my-ss
      `((article *macro*
	 . ,(lambda (tag . elems)
	      (cons '*article
		    (level-sections 1 elems))))
	(*default* . ,(lambda x x))
	(*text* . ,(lambda (tag str) str))))

The result of evalutating

    (pre-post-order doc2 my-ss)

is:

    (*article
      (*section 1 (title "T1") (para "text")
	(*section 2 (title "T1.1") (para "text")
	  (*section 3 (title "T1.1.1") (para "text"))
	  (*section 3 (title "T1.1.2") (para "text")))
	(*section 2 (title "T1.2") (para "text")))
      (*section 1 (title "T2") (para "text")))

This seems like a better solution.  Can anybody think of any better
ways to do it?

-- 
T. Kurt Bond, [email protected]



-------------------------------------------------------
This SF.net email is sponsored by: The SF.net Donation Program.
Do you like what SourceForge.net is doing for the Open
Source Community?  Make a contribution, and help us add new
features and functionality. Click here: http://sourceforge.net/donate/