[ssax-sxml] Best way to label hierarchical sections with their depths
Brought to you by:
oleg
From: T. K. B. <tk...@tk...> - 2003-10-27 20:09:23
|
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, tk...@tk... |