Best way to label hierarchical sections with their depths

"T. Kurt Bond" <[email protected]> Mon, 27 Oct 2003 15:06:23 -0500 (EST)
Newsgroups gmane.lisp.scheme.ssax-sxml
Message-ID <[email protected]>
I'm a novice just starting to work with SXML and SXSLT.

I have a document that looks something like this:

    (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")))

In the general case the sections can be nested even more deeply.  To
convert this to the troff/groff MM I need to know the depth of each
section to generate the appropriate call to the H (numbered section
heading) macro.  As a first step I thought I'd rewrite the section
nodes using pre-post-order so they each have the depth following the
tag, like this:

    (*section 1 (title "T1")
      ...
      (*section 2 (title "T1.1")
        ...
        (*section 3 (title "T1.1.1")
          ...
          )))

My first attempt only works for sections up to three deep:

    (define my-ss
      `((*text*
	 . ,(lambda (tag str) str))
	(*default*
	 . ,(lambda x x))
	(section
	 ((section
	   ((section
	     . ,(lambda (tag . elems)
		  (cons '*section (cons 3 elems)))))
	   . ,(lambda (tag . elems)
		(cons '*section (cons 2  elems)))))
	 . ,(lambda (tag . elems)
	      (cons '*section (cons 1 elems))))))

My second attempt requires modifying pre-post-order so that the
handlers are passed the current bindings as the first parameter:

    (define my-ss
      `((*level* . 1)
	(*text*
	 . ,(lambda (b tag str) str))
	(*default*
	 . ,(lambda (b . x) x))
	(section *preorder*
	 . ,(lambda (b tag . elems)
	      (pp b)
	      (let* ((level (cdr (assoc '*level* b)))
		     (new-level (+ 1 level))
		     (new-b (cons (cons '*level*  new-level) b)))
		(cons
		 '*section
		 (cons level 
		       (map (lambda (el)
			      (pre-post-order el new-b))
			    elems))))))))

Is there a better approach to this sort of thing?
-- 
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/