Hex Artifact Content
Not logged in

Artifact 576ba8f8ae2ef8db4830f9308080a5e4ea5ae9cf:


0000: 3b 09 20 20 20 41 6e 20 61 64 76 61 6e 63 65 64  ;.   An advanced
0010: 20 65 78 61 6d 70 6c 65 20 6f 66 20 53 58 4d 4c   example of SXML
0020: 2d 74 6f 2d 48 54 4d 4c 20 74 72 61 6e 73 66 6f  -to-HTML transfo
0030: 72 6d 61 74 69 6f 6e 73 0a 3b 0a 3b 20 54 68 65  rmations.;.; The
0040: 20 70 72 6f 62 6c 65 6d 0a 3b 20 57 65 20 61 72   problem.; We ar
0050: 65 20 67 69 76 65 6e 20 61 20 73 6f 75 72 63 65  e given a source
0060: 20 64 6f 63 75 6d 65 6e 74 20 69 6e 20 53 58 4d   document in SXM
0070: 4c 2e 20 54 68 65 20 64 6f 63 75 6d 65 6e 74 20  L. The document 
0080: 68 61 73 20 74 68 65 0a 3b 20 48 54 4d 4c 2d 6c  has the.; HTML-l
0090: 69 6b 65 20 27 68 65 61 64 27 20 61 6e 64 20 74  ike 'head' and t
00a0: 68 65 20 27 62 6f 64 79 27 2e 20 54 68 65 20 6c  he 'body'. The l
00b0: 61 74 74 65 72 20 69 73 20 6d 61 64 65 20 6f 66  atter is made of
00c0: 20 73 65 63 74 69 6f 6e 73 2c 0a 3b 20 65 61 63   sections,.; eac
00d0: 68 20 6f 66 20 77 68 69 63 68 20 69 73 20 61 20  h of which is a 
00e0: 73 65 71 75 65 6e 63 65 20 6f 66 20 70 61 72 61  sequence of para
00f0: 67 72 61 70 68 73 20 6f 72 20 28 73 75 62 29 73  graphs or (sub)s
0100: 65 63 74 69 6f 6e 73 2e 20 54 68 65 0a 3b 20 73  ections. The.; s
0110: 75 62 2d 73 65 63 74 69 6f 6e 73 20 61 72 65 20  ub-sections are 
0120: 69 6e 20 74 75 72 6e 20 61 72 65 20 6d 61 64 65  in turn are made
0130: 20 6f 66 20 70 61 72 61 67 72 61 70 68 73 20 6f   of paragraphs o
0140: 72 20 6f 74 68 65 72 20 28 73 75 62 73 75 62 2e  r other (subsub.
0150: 2e 2e 29 0a 3b 20 73 65 63 74 69 6f 6e 73 2c 20  ..).; sections, 
0160: 65 74 63 2e 20 20 53 65 65 20 74 68 65 20 64 65  etc.  See the de
0170: 66 69 6e 69 74 69 6f 6e 20 6f 66 20 27 64 6f 63  finition of 'doc
0180: 27 20 62 65 6c 6f 77 20 66 6f 72 20 61 20 73 61  ' below for a sa
0190: 6d 70 6c 65 0a 3b 20 64 6f 63 75 6d 65 6e 74 2e  mple.; document.
01a0: 0a 3b 0a 3b 20 57 65 20 6e 65 65 64 20 74 6f 20  .;.; We need to 
01b0: 6e 75 6d 62 65 72 20 74 68 65 20 73 65 63 74 69  number the secti
01c0: 6f 6e 73 20 61 6e 64 20 28 73 75 62 2e 2e 2e 29  ons and (sub...)
01d0: 73 65 63 74 69 6f 6e 73 20 61 6e 64 20 6f 75 74  sections and out
01e0: 70 75 74 0a 3b 20 61 6e 20 48 54 4d 4c 20 64 6f  put.; an HTML do
01f0: 63 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f  cument of the fo
0200: 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
0210: 65 3a 0a 3b 0a 3b 20 20 20 20 31 2e 20 46 69 72  e:.;.;    1. Fir
0220: 73 74 20 53 65 63 74 69 6f 6e 0a 3b 20 20 20 20  st Section.;    
0230: 20 20 20 2e 2e 2e 0a 3b 20 20 20 20 32 2e 20 53     ....;    2. S
0240: 65 63 6f 6e 64 20 53 65 63 74 69 6f 6e 0a 3b 20  econd Section.; 
0250: 20 20 20 32 2e 31 2e 20 46 69 72 73 74 20 73 75     2.1. First su
0260: 62 2d 73 65 63 74 69 6f 6e 0a 3b 20 20 20 20 32  b-section.;    2
0270: 2e 32 2e 20 41 6e 6f 74 68 65 72 20 73 75 62 2d  .2. Another sub-
0280: 73 65 63 74 69 6f 6e 0a 3b 20 20 20 2e 2e 2e 73  section.;   ...s
0290: 65 63 74 69 6f 6e 20 63 6f 6e 74 65 6e 74 73 2e  ection contents.
02a0: 2e 2e 0a 3b 0a 3b 20 57 65 20 73 68 6f 75 6c 64  ...;.; We should
02b0: 20 75 73 65 20 74 68 65 20 61 70 70 72 6f 70 72   use the appropr
02c0: 69 61 74 65 20 48 54 4d 4c 20 74 61 67 73 20 28  iate HTML tags (
02d0: 48 32 2c 20 48 33 2c 20 2e 2e 2e 29 20 74 6f 20  H2, H3, ...) to 
02e0: 73 65 74 20 6f 66 66 20 74 68 65 0a 3b 20 74 69  set off the.; ti
02f0: 74 6c 65 20 6f 66 20 74 68 65 20 73 65 63 74 69  tle of the secti
0300: 6f 6e 73 20 61 6e 64 20 73 75 62 73 65 63 74 69  ons and subsecti
0310: 6f 6e 73 2e 0a 3b 20 49 6e 20 61 64 64 69 74 69  ons..; In additi
0320: 6f 6e 2c 20 77 65 20 68 61 76 65 20 74 6f 20 67  on, we have to g
0330: 65 6e 65 72 61 74 65 20 61 20 68 69 65 72 61 72  enerate a hierar
0340: 63 68 69 63 61 6c 20 74 61 62 6c 65 20 6f 66 20  chical table of 
0350: 63 6f 6e 74 65 6e 74 73 0a 3b 20 61 6e 64 20 70  contents.; and p
0360: 6c 61 63 65 20 69 74 20 61 74 20 74 68 65 20 62  lace it at the b
0370: 65 67 69 6e 6e 69 6e 67 2e 0a 3b 0a 3b 20 54 68  eginning..;.; Th
0380: 69 73 20 65 78 61 6d 70 6c 65 20 69 73 20 64 75  is example is du
0390: 65 20 74 6f 20 4a 69 6d 20 42 65 6e 64 65 72 2c  e to Jim Bender,
03a0: 20 77 68 6f 20 75 73 65 64 20 61 20 73 69 6d 69   who used a simi
03b0: 6c 61 72 20 74 72 61 6e 73 66 6f 72 6d 61 74 69  lar transformati
03c0: 6f 6e 20 66 6f 72 0a 3b 20 63 6f 6d 70 69 6c 69  on for.; compili
03d0: 6e 67 20 61 6e 20 58 4d 4c 20 53 63 68 65 6d 61  ng an XML Schema
03e0: 20 69 6e 74 6f 20 41 53 4e 2e 31 20 73 70 65 63   into ASN.1 spec
03f0: 69 66 69 63 61 74 69 6f 6e 73 2e 0a 3b 0a 3b 20  ifications..;.; 
0400: 54 68 69 73 20 66 69 6c 65 20 70 72 65 73 65 6e  This file presen
0410: 74 73 20 61 20 73 6f 6c 75 74 69 6f 6e 2c 20 77  ts a solution, w
0420: 68 69 63 68 20 72 65 6c 69 65 73 20 6f 6e 20 53  hich relies on S
0430: 58 4d 4c 20 74 72 61 6e 73 66 6f 72 6d 61 74 69  XML transformati
0440: 6f 6e 73 0a 3b 20 62 79 20 61 20 74 72 61 76 65  ons.; by a trave
0450: 72 73 61 6c 20 63 6f 6d 62 69 6e 61 74 6f 72 20  rsal combinator 
0460: 70 72 65 2d 70 6f 73 74 2d 6f 72 64 65 72 2e 20  pre-post-order. 
0470: 54 68 65 20 6c 61 74 74 65 72 20 69 73 20 64 65  The latter is de
0480: 66 69 6e 65 64 20 69 6e 20 61 0a 3b 20 66 69 6c  fined in a.; fil
0490: 65 20 2e 2e 2f 6c 69 62 2f 53 58 4d 4c 2d 74 72  e ../lib/SXML-tr
04a0: 65 65 2d 74 72 61 6e 73 2e 73 63 6d 0a 3b 0a 3b  ee-trans.scm.;.;
04b0: 20 54 68 65 20 70 72 65 73 65 6e 74 20 73 6f 6c   The present sol
04c0: 75 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 68 65  ution is not the
04d0: 20 6f 6e 6c 79 20 6f 6e 65 20 70 6f 73 73 69 62   only one possib
04e0: 6c 65 2c 20 6e 6f 72 20 69 73 20 69 74 20 74 68  le, nor is it th
04f0: 65 0a 3b 20 6d 6f 73 74 20 6f 70 74 69 6d 61 6c  e.; most optimal
0500: 2e 20 54 68 65 20 6d 6f 73 74 20 65 66 66 69 63  . The most effic
0510: 69 65 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 73 68  ient solution sh
0520: 6f 75 6c 64 20 76 69 73 69 74 20 65 61 63 68 20  ould visit each 
0530: 6e 6f 64 65 20 6f 66 0a 3b 20 74 68 65 20 53 58  node of.; the SX
0540: 4d 4c 20 74 72 65 65 20 65 78 61 63 74 6c 79 20  ML tree exactly 
0550: 6f 6e 63 65 20 61 6e 64 20 73 68 6f 75 6c 64 20  once and should 
0560: 6e 6f 74 20 63 72 65 61 74 65 20 61 6e 79 20 63  not create any c
0570: 6c 6f 73 75 72 65 73 2c 20 6c 65 74 0a 3b 20 61  losures, let.; a
0580: 6c 6f 6e 65 20 74 72 65 65 73 20 6f 66 20 63 6c  lone trees of cl
0590: 6f 73 75 72 65 73 2e 20 54 68 65 20 70 72 65 2d  osures. The pre-
05a0: 70 6f 73 74 2d 6f 72 64 65 72 20 63 6f 6d 62 69  post-order combi
05b0: 6e 61 74 6f 72 20 2d 2d 20 74 68 65 20 74 72 65  nator -- the tre
05c0: 65 0a 3b 20 66 6f 6c 64 20 69 6e 20 67 65 6e 65  e.; fold in gene
05d0: 72 61 6c 20 2d 2d 20 63 61 6e 6e 6f 74 20 65 78  ral -- cannot ex
05e0: 70 72 65 73 73 20 74 68 69 73 20 73 69 6e 67 6c  press this singl
05f0: 65 20 74 72 61 76 65 72 73 61 6c 2c 20 62 75 74  e traversal, but
0600: 20 61 6e 0a 3b 20 61 63 63 75 6d 75 6c 61 74 69   an.; accumulati
0610: 6e 67 20 74 72 65 65 20 66 6f 6c 64 20 27 66 6f  ng tree fold 'fo
0620: 6c 64 74 73 27 20 28 61 6c 73 6f 20 64 65 66 69  ldts' (also defi
0630: 6e 65 64 20 69 6e 0a 3b 20 2e 2e 2f 6c 69 62 2f  ned in.; ../lib/
0640: 53 58 4d 4c 2d 74 72 65 65 2d 74 72 61 6e 73 2e  SXML-tree-trans.
0650: 73 63 6d 29 20 63 61 6e 2e 0a 3b 0a 3b 20 54 68  scm) can..;.; Th
0660: 65 20 61 70 70 72 6f 61 63 68 20 74 61 6b 65 6e  e approach taken
0670: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
0680: 20 6e 6f 74 20 6d 65 61 6e 74 20 74 6f 20 62 65   not meant to be
0690: 20 74 68 65 20 6d 6f 73 74 0a 3b 20 65 66 66 69   the most.; effi
06a0: 63 69 65 6e 74 2e 20 20 52 61 74 68 65 72 2c 20  cient.  Rather, 
06b0: 69 74 20 69 73 20 61 69 6d 65 64 20 74 6f 20 62  it is aimed to b
06c0: 65 20 69 6c 6c 75 73 74 72 61 74 69 76 65 20 6f  e illustrative o
06d0: 66 20 76 61 72 69 6f 75 73 20 53 58 53 4c 54 0a  f various SXSLT.
06e0: 3b 20 66 61 63 69 6c 69 74 69 65 73 20 61 6e 64  ; facilities and
06f0: 20 69 64 69 6f 6d 73 2e 20 20 49 6e 20 70 61 72   idioms.  In par
0700: 74 69 63 75 6c 61 72 2c 20 77 65 20 64 65 6d 6f  ticular, we demo
0710: 6e 73 74 72 61 74 65 3a 20 68 69 67 68 65 72 2d  nstrate: higher-
0720: 6f 72 64 65 72 0a 3b 20 74 61 67 73 2c 20 70 72  order.; tags, pr
0730: 65 2d 6f 72 64 65 72 20 61 6e 64 20 70 6f 73 74  e-order and post
0740: 2d 6f 72 64 65 72 20 74 72 61 6e 73 66 6f 72 6d  -order transform
0750: 61 74 69 6f 6e 73 2c 20 72 65 2d 77 72 69 74 69  ations, re-writi
0760: 6e 67 20 6f 66 20 53 58 4d 4c 0a 3b 20 65 6c 65  ng of SXML.; ele
0770: 6d 65 6e 74 73 20 69 6e 20 72 65 67 75 6c 61 72  ments in regular
0780: 20 61 6e 64 20 73 70 65 63 69 61 6c 20 77 61 79   and special way
0790: 73 2c 20 63 6f 6e 74 65 78 74 2d 73 65 6e 73 69  s, context-sensi
07a0: 74 69 76 65 20 61 70 70 6c 69 63 61 74 69 6f 6e  tive application
07b0: 73 0a 3b 20 6f 66 20 72 65 2d 77 72 69 74 69 6e  s.; of re-writin
07c0: 67 20 72 75 6c 65 73 2e 20 46 69 6e 61 6c 6c 79  g rules. Finally
07d0: 2c 20 77 65 20 69 6c 6c 75 73 74 72 61 74 65 20  , we illustrate 
07e0: 53 58 53 4c 54 20 72 65 66 6c 65 63 74 69 6f 6e  SXSLT reflection
07f0: 3a 20 61 6e 0a 3b 20 61 62 69 6c 69 74 79 20 6f  : an.; ability o
0800: 66 20 61 20 72 75 6c 65 20 74 6f 20 71 75 65 72  f a rule to quer
0810: 79 20 69 74 73 20 6f 77 6e 20 73 74 79 6c 65 73  y its own styles
0820: 68 65 65 74 20 61 6e 64 20 74 6f 20 72 65 2d 61  heet and to re-a
0830: 70 70 6c 79 20 74 68 65 0a 3b 20 73 74 79 6c 65  pply the.; style
0840: 73 68 65 65 74 20 77 69 74 68 20 22 6d 6f 64 69  sheet with "modi
0850: 66 69 63 61 74 69 6f 6e 73 22 2e 0a 3b 0a 3b 20  fications"..;.; 
0860: 41 6c 6c 20 53 58 53 4c 54 20 74 72 61 6e 73 66  All SXSLT transf
0870: 6f 72 6d 61 74 69 6f 6e 73 20 69 6e 20 74 68 69  ormations in thi
0880: 73 20 66 69 6c 65 20 61 72 65 20 70 75 72 65 6c  s file are purel
0890: 79 20 66 75 6e 63 74 69 6f 6e 61 6c 20 61 6e 64  y functional and
08a0: 0a 3b 20 64 65 63 6c 61 72 61 74 69 76 65 2e 20  .; declarative. 
08b0: 54 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20  The entire code 
08c0: 68 61 73 20 6e 6f 20 6d 75 74 61 74 69 6f 6e 73  has no mutations
08d0: 2e 20 54 68 65 20 63 6f 64 65 20 6d 61 69 6e 74  . The code maint
08e0: 61 69 6e 73 0a 3b 20 74 68 65 20 66 75 6c 6c 20  ains.; the full 
08f0: 72 65 66 65 72 65 6e 74 69 61 6c 20 74 72 61 6e  referential tran
0900: 73 70 61 72 65 6e 63 79 2e 0a 3b 0a 3b 20 24 49  sparency..;.; $I
0910: 64 3a 20 73 78 73 6c 74 2d 61 64 76 61 6e 63 65  d: sxslt-advance
0920: 64 2e 73 63 6d 2c 76 20 31 2e 33 20 32 30 30 33  d.scm,v 1.3 2003
0930: 2f 30 38 2f 30 37 20 31 39 3a 35 35 3a 33 31 20  /08/07 19:55:31 
0940: 6f 6c 65 67 20 45 78 70 20 6f 6c 65 67 20 24 0a  oleg Exp oleg $.
0950: 3b 0a 3b 20 49 4d 50 4f 52 54 0a 3b 20 54 68 65  ;.; IMPORT.; The
0960: 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20   following is a 
0970: 42 69 67 6c 6f 6f 2d 73 70 65 63 69 66 69 63 20  Bigloo-specific 
0980: 6d 6f 64 75 6c 65 20 64 65 63 6c 61 72 61 74 69  module declarati
0990: 6f 6e 2e 0a 3b 20 4f 74 68 65 72 20 53 63 68 65  on..; Other Sche
09a0: 6d 65 20 73 79 73 74 65 6d 73 20 68 61 76 65 20  me systems have 
09b0: 73 6f 6d 65 74 68 69 6e 67 20 73 69 6d 69 6c 61  something simila
09c0: 72 2e 0a 3b 0a 3b 20 28 6d 6f 64 75 6c 65 20 73  r..;.; (module s
09d0: 78 73 6c 74 2d 61 64 76 61 6e 63 65 64 0a 3b 20  xslt-advanced.; 
09e0: 09 28 69 6e 63 6c 75 64 65 20 22 6d 79 65 6e 76  .(include "myenv
09f0: 2d 62 69 67 6c 6f 6f 2e 73 63 6d 22 29 0a 3b 20  -bigloo.scm").; 
0a00: 09 28 69 6e 63 6c 75 64 65 20 22 73 72 66 69 2d  .(include "srfi-
0a10: 31 33 2d 6c 6f 63 61 6c 2e 73 63 6d 22 29 20 3b  13-local.scm") ;
0a20: 20 6f 72 20 69 6d 70 6f 72 74 20 66 72 6f 6d 20   or import from 
0a30: 53 52 46 49 2d 31 33 20 69 66 20 61 76 61 69 6c  SRFI-13 if avail
0a40: 61 62 6c 65 0a 3b 20 09 28 69 6e 63 6c 75 64 65  able.; .(include
0a50: 20 22 75 74 69 6c 2e 73 63 6d 22 29 0a 3b 20 09   "util.scm").; .
0a60: 28 69 6e 63 6c 75 64 65 20 22 53 58 4d 4c 2d 74  (include "SXML-t
0a70: 72 65 65 2d 74 72 61 6e 73 2e 73 63 6d 22 29 0a  ree-trans.scm").
0a80: 3b 20 09 28 69 6e 63 6c 75 64 65 20 22 53 58 4d  ; .(include "SXM
0a90: 4c 2d 74 6f 2d 48 54 4d 4c 2e 73 63 6d 22 29 0a  L-to-HTML.scm").
0aa0: 3b 20 09 28 69 6e 63 6c 75 64 65 20 22 53 58 4d  ; .(include "SXM
0ab0: 4c 2d 74 6f 2d 48 54 4d 4c 2d 65 78 74 2e 73 63  L-to-HTML-ext.sc
0ac0: 6d 22 29 0a 3b 20 09 29 0a 3b 20 53 65 65 20 74  m").; .).; See t
0ad0: 68 65 20 4d 61 6b 65 66 69 6c 65 20 66 6f 72 20  he Makefile for 
0ae0: 74 68 65 20 72 75 6c 65 73 20 74 6f 20 72 75 6e  the rules to run
0af0: 20 74 68 69 73 20 65 78 61 6d 70 6c 65 20 6f 6e   this example on
0b00: 20 42 69 67 6c 6f 6f 2c 20 53 43 4d 0a 3b 20 61   Bigloo, SCM.; a
0b10: 6e 64 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73  nd other systems
0b20: 0a 0a 3b 20 41 20 73 61 6d 70 6c 65 20 73 6f 75  ..; A sample sou
0b30: 72 63 65 20 64 6f 63 75 6d 65 6e 74 2c 20 6d 61  rce document, ma
0b40: 72 6b 65 64 20 75 70 20 69 6e 20 53 58 4d 4c 2e  rked up in SXML.
0b50: 0a 3b 20 4e 6f 74 65 20 74 68 65 20 61 6d 70 65  .; Note the ampe
0b60: 72 73 61 6e 64 20 63 68 61 72 61 63 74 65 72 20  rsand character 
0b70: 69 6e 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  in the text of t
0b80: 68 65 20 66 69 72 73 74 20 53 65 63 74 69 6f 6e  he first Section
0b90: 0a 3b 20 70 61 72 61 67 72 61 70 68 2e 20 57 65  .; paragraph. We
0ba0: 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 70 6f 69   should also poi
0bb0: 6e 74 20 6f 75 74 20 74 68 65 20 73 70 61 63 65  nt out the space
0bc0: 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
0bd0: 65 20 6e 61 6d 65 0a 3b 20 6f 66 20 74 68 65 20  e name.; of the 
0be0: 64 6f 63 75 6d 65 6e 74 20 66 69 6c 65 20 28 69  document file (i
0bf0: 6e 20 74 68 65 20 68 72 65 66 20 61 74 74 72 69  n the href attri
0c00: 62 75 74 65 20 6f 66 20 74 68 65 20 6c 69 6e 6b  bute of the link
0c10: 29 2e 20 54 68 65 73 65 0a 3b 20 63 68 61 72 61  ). These.; chara
0c20: 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 70 72  cters must be pr
0c30: 6f 70 65 72 6c 79 20 65 73 63 61 70 65 64 20 69  operly escaped i
0c40: 6e 20 74 68 65 20 74 61 72 67 65 74 20 48 54 4d  n the target HTM
0c50: 4c 20 64 6f 63 75 6d 65 6e 74 2e 0a 0a 28 64 65  L document...(de
0c60: 66 69 6e 65 20 64 6f 63 0a 20 20 27 28 68 74 6d  fine doc.  '(htm
0c70: 6c 20 28 68 65 61 64 20 28 74 69 74 6c 65 20 22  l (head (title "
0c80: 44 6f 63 75 6d 65 6e 74 22 29 29 0a 20 20 20 28  Document")).   (
0c90: 62 6f 64 79 0a 20 20 20 20 20 28 73 65 63 74 69  body.     (secti
0ca0: 6f 6e 20 22 46 69 72 73 74 20 53 65 63 74 69 6f  on "First Sectio
0cb0: 6e 22 0a 20 20 20 20 20 20 20 28 70 20 22 54 68  n".       (p "Th
0cc0: 69 73 20 69 73 20 74 68 65 20 69 6e 74 72 6f 20  is is the intro 
0cd0: 73 65 63 74 69 6f 6e 2e 22 29 0a 20 20 20 20 20  section.").     
0ce0: 20 20 28 70 20 22 50 61 72 61 67 72 61 70 68 20    (p "Paragraph 
0cf0: 26 63 22 29 29 0a 20 20 20 20 20 28 73 65 63 74  &c")).     (sect
0d00: 69 6f 6e 20 22 53 65 63 6f 6e 64 20 53 65 63 74  ion "Second Sect
0d10: 69 6f 6e 22 0a 20 20 20 20 20 20 20 28 73 65 63  ion".       (sec
0d20: 74 69 6f 6e 20 22 41 20 73 75 62 2d 73 65 63 74  tion "A sub-sect
0d30: 69 6f 6e 22 0a 09 20 28 70 20 22 54 68 69 73 20  ion".. (p "This 
0d40: 69 73 20 73 65 63 74 69 6f 6e 20 32 2e 31 2e 22  is section 2.1."
0d50: 0a 09 20 20 20 28 62 72 29 0a 09 20 20 20 28 61  ..   (br)..   (a
0d60: 20 28 40 20 28 68 72 65 66 20 22 61 6e 6f 74 68   (@ (href "anoth
0d70: 65 72 20 64 6f 63 2e 68 74 6d 6c 22 29 29 0a 09  er doc.html"))..
0d80: 20 20 20 20 20 22 6c 69 6e 6b 22 29 29 29 0a 20       "link"))). 
0d90: 20 20 20 20 20 20 28 73 65 63 74 69 6f 6e 20 22        (section "
0da0: 41 6e 6f 74 68 65 72 20 73 75 62 2d 73 65 63 74  Another sub-sect
0db0: 69 6f 6e 22 0a 09 20 28 70 20 22 54 68 69 73 20  ion".. (p "This 
0dc0: 69 73 20 73 65 63 74 69 6f 6e 20 32 2e 32 2e 22  is section 2.2."
0dd0: 29 29 29 0a 20 20 20 20 20 28 73 65 63 74 69 6f  ))).     (sectio
0de0: 6e 20 22 4c 61 73 74 20 6d 61 6a 6f 72 20 73 65  n "Last major se
0df0: 63 74 69 6f 6e 22 0a 20 20 20 20 20 20 20 28 70  ction".       (p
0e00: 20 22 54 68 69 73 20 69 73 20 74 68 65 20 74 68   "This is the th
0e10: 69 72 64 20 73 65 63 74 69 6f 6e 22 29 29 29 29  ird section"))))
0e20: 29 0a 0a 0a 3b 20 41 75 78 69 6c 69 61 72 79 20  )...; Auxiliary 
0e30: 70 72 6f 63 65 64 75 72 65 73 0a 0a 3b 20 28 61  procedures..; (a
0e40: 20 62 20 63 20 2e 2e 2e 29 20 3d 3e 20 28 31 20   b c ...) => (1 
0e50: 32 20 33 20 2e 2e 2e 29 0a 28 64 65 66 69 6e 65  2 3 ...).(define
0e60: 20 28 6c 69 73 74 2d 6e 75 6d 62 65 72 69 6e 67   (list-numbering
0e70: 20 6c 73 74 29 0a 20 20 28 6c 65 74 20 6c 6f 6f   lst).  (let loo
0e80: 70 20 28 28 69 20 31 29 20 28 6c 73 74 20 6c 73  p ((i 1) (lst ls
0e90: 74 29 29 0a 20 20 20 20 28 69 66 20 28 6e 75 6c  t)).    (if (nul
0ea0: 6c 3f 20 6c 73 74 29 20 27 28 29 0a 20 20 20 20  l? lst) '().    
0eb0: 20 20 28 63 6f 6e 73 20 69 20 28 6c 6f 6f 70 20    (cons i (loop 
0ec0: 28 2b 20 31 20 69 29 20 28 63 64 72 20 6c 73 74  (+ 1 i) (cdr lst
0ed0: 29 29 29 29 29 29 0a 0a 3b 20 54 68 65 20 6f 72  ))))))..; The or
0ee0: 64 69 6e 61 72 79 20 66 69 6c 74 65 72 20 63 6f  dinary filter co
0ef0: 6d 62 69 6e 61 74 6f 72 0a 28 64 65 66 69 6e 65  mbinator.(define
0f00: 20 28 66 69 6c 74 65 72 20 70 72 65 64 20 6c 73   (filter pred ls
0f10: 74 29 0a 20 20 28 63 6f 6e 64 0a 20 20 20 20 28  t).  (cond.    (
0f20: 28 6e 75 6c 6c 3f 20 6c 73 74 29 20 6c 73 74 29  (null? lst) lst)
0f30: 0a 20 20 20 20 28 28 70 72 65 64 20 28 63 61 72  .    ((pred (car
0f40: 20 6c 73 74 29 29 20 28 63 6f 6e 73 20 28 63 61   lst)) (cons (ca
0f50: 72 20 6c 73 74 29 20 28 66 69 6c 74 65 72 20 70  r lst) (filter p
0f60: 72 65 64 20 28 63 64 72 20 6c 73 74 29 29 29 29  red (cdr lst))))
0f70: 0a 20 20 20 20 28 65 6c 73 65 20 28 66 69 6c 74  .    (else (filt
0f80: 65 72 20 70 72 65 64 20 28 63 64 72 20 6c 73 74  er pred (cdr lst
0f90: 29 29 29 29 29 0a 0a 3b 20 28 33 20 32 20 31 29  )))))..; (3 2 1)
0fa0: 20 3d 3e 20 22 31 2e 32 2e 33 22 0a 28 64 65 66   => "1.2.3".(def
0fb0: 69 6e 65 20 28 6e 75 6d 62 65 72 73 2d 3e 73 74  ine (numbers->st
0fc0: 72 69 6e 67 20 6c 73 74 29 0a 20 20 28 61 70 70  ring lst).  (app
0fd0: 6c 79 20 73 74 72 69 6e 67 2d 61 70 70 65 6e 64  ly string-append
0fe0: 0a 20 20 20 20 28 6c 69 73 74 2d 69 6e 74 65 72  .    (list-inter
0ff0: 73 70 65 72 73 65 20 0a 20 20 20 20 20 20 28 6d  sperse .      (m
1000: 61 70 20 6e 75 6d 62 65 72 2d 3e 73 74 72 69 6e  ap number->strin
1010: 67 20 28 72 65 76 65 72 73 65 20 6c 73 74 29 29  g (reverse lst))
1020: 20 22 2e 22 29 29 29 0a 0a 0a 0a 3b 20 52 65 63   ".")))....; Rec
1030: 75 72 73 69 76 65 20 6e 75 6d 62 65 72 69 6e 67  ursive numbering
1040: 20 6f 66 20 73 65 63 74 69 6f 6e 73 3a 20 67 69   of sections: gi
1050: 76 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 27 73  ven a list of 's
1060: 65 63 74 69 6f 6e 73 27 2c 20 72 65 74 75 72 6e  ections', return
1070: 0a 3b 20 74 68 65 20 6c 69 73 74 20 6f 66 20 27  .; the list of '
1080: 2a 73 65 63 74 69 6f 6e 73 27 3a 0a 3b 0a 3b 20  *sections':.;.; 
1090: 20 20 28 28 73 65 63 74 69 6f 6e 20 74 69 74 6c    ((section titl
10a0: 65 20 28 73 65 63 74 69 6f 6e 20 74 69 74 6c 65  e (section title
10b0: 20 65 6c 20 2e 2e 2e 29 20 2e 2e 2e 29 20 0a 3b   el ...) ...) .;
10c0: 20 20 20 20 28 73 65 63 74 69 6f 6e 20 74 69 74      (section tit
10d0: 6c 65 20 28 73 65 63 74 69 6f 6e 20 74 69 74 6c  le (section titl
10e0: 65 20 65 6c 20 2e 2e 2e 29 20 2e 2e 2e 29 20 2e  e el ...) ...) .
10f0: 2e 2e 29 0a 3b 20 3d 3e 0a 3b 20 20 20 28 28 2a  ..).; =>.;   ((*
1100: 73 65 63 74 69 6f 6e 20 28 31 29 20 74 69 74 6c  section (1) titl
1110: 65 20 28 2a 73 65 63 74 69 6f 6e 20 28 31 20 31  e (*section (1 1
1120: 29 20 74 69 74 6c 65 20 65 6c 20 2e 2e 2e 29 20  ) title el ...) 
1130: 65 6c 20 2e 2e 2e 29 0a 3b 20 20 20 20 28 2a 73  el ...).;    (*s
1140: 65 63 74 69 6f 6e 20 28 32 29 20 74 69 74 6c 65  ection (2) title
1150: 20 28 2a 73 65 63 74 69 6f 6e 20 28 31 20 32 29   (*section (1 2)
1160: 20 74 69 74 6c 65 20 65 6c 20 2e 2e 2e 29 20 65   title el ...) e
1170: 6c 20 2e 2e 2e 29 20 2e 2e 2e 29 0a 3b 0a 3b 20  l ...) ...).;.; 
1180: 54 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 28 74  The numbering (t
1190: 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
11a0: 20 6f 66 20 61 20 2a 73 65 63 74 69 6f 6e 29 20   of a *section) 
11b0: 69 73 20 61 20 6c 69 73 74 20 6f 66 20 73 65 63  is a list of sec
11c0: 74 69 6f 6e 0a 3b 20 6e 75 6d 62 65 72 73 20 69  tion.; numbers i
11d0: 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 3a  n reverse order:
11e0: 20 74 68 65 20 6e 75 6d 65 72 69 63 20 6c 61 62   the numeric lab
11f0: 65 6c 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  el of the curren
1200: 74 20 73 65 63 74 69 6f 6e 0a 3b 20 66 6f 6c 6c  t section.; foll
1210: 6f 77 65 64 20 62 79 20 74 68 65 20 6c 61 62 65  owed by the labe
1220: 6c 73 20 6f 66 20 69 74 73 20 61 6e 63 65 73 74  ls of its ancest
1230: 6f 72 73 2e 0a 3b 0a 3b 20 41 20 27 73 65 63 74  ors..;.; A 'sect
1240: 69 6f 6e 27 20 6f 66 20 74 68 65 20 73 6f 75 72  ion' of the sour
1250: 63 65 20 64 6f 63 75 6d 65 6e 74 20 6d 61 79 20  ce document may 
1260: 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20 28  contain either (
1270: 73 75 62 29 73 65 63 74 69 6f 6e 73 2c 0a 3b 20  sub)sections,.; 
1280: 6f 72 20 6f 74 68 65 72 20 53 58 4d 4c 20 6e 6f  or other SXML no
1290: 64 65 73 20 73 75 63 68 20 61 73 20 73 74 72 69  des such as stri
12a0: 6e 67 73 20 6f 72 20 70 61 72 61 67 72 61 70 68  ngs or paragraph
12b0: 73 2e 20 54 68 65 20 6e 75 6d 62 65 72 69 6e 67  s. The numbering
12c0: 0a 3b 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f  .; transformatio
12d0: 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 66 66  n should not aff
12e0: 65 63 74 20 74 68 65 20 6c 61 74 74 65 72 20 6e  ect the latter n
12f0: 6f 64 65 73 2e 0a 3b 0a 3b 20 54 68 65 20 66 75  odes..;.; The fu
1300: 6e 63 74 69 6f 6e 20 27 6e 75 6d 62 65 72 2d 73  nction 'number-s
1310: 65 63 74 69 6f 6e 73 27 20 69 6c 6c 75 73 74 72  ections' illustr
1320: 61 74 65 73 20 61 20 74 79 70 69 63 61 6c 20 58  ates a typical X
1330: 53 4c 54 2d 6c 69 6b 65 0a 3b 20 70 72 6f 63 65  SLT-like.; proce
1340: 73 73 69 6e 67 2e 20 57 65 20 74 72 61 6e 73 66  ssing. We transf
1350: 6f 72 6d 20 61 6e 20 53 58 4d 4c 20 74 72 65 65  orm an SXML tree
1360: 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65   by invoking the
1370: 20 74 72 61 76 65 72 73 61 6c 0a 3b 20 63 6f 6d   traversal.; com
1380: 62 69 6e 61 74 6f 72 20 70 72 65 2d 70 6f 73 74  binator pre-post
1390: 2d 6f 72 64 65 72 20 61 6e 64 20 70 61 73 73 69  -order and passi
13a0: 6e 67 20 69 74 20 74 68 65 20 73 6f 75 72 63 65  ng it the source
13b0: 20 74 72 65 65 20 61 6e 64 20 61 0a 3b 20 73 74   tree and a.; st
13c0: 79 6c 65 73 68 65 65 74 2e 20 49 6e 20 74 68 65  ylesheet. In the
13d0: 20 63 6f 64 65 20 62 65 6c 6f 77 2c 20 74 68 65   code below, the
13e0: 20 73 74 79 6c 65 73 68 65 65 74 20 68 61 73 20   stylesheet has 
13f0: 6f 6e 6c 79 20 74 68 72 65 65 0a 3b 20 72 75 6c  only three.; rul
1400: 65 73 2e 20 54 68 65 20 2a 74 65 78 74 2a 20 72  es. The *text* r
1410: 75 6c 65 20 69 73 20 74 68 65 20 69 64 65 6e 74  ule is the ident
1420: 69 74 79 20 72 75 6c 65 2e 20 49 74 20 70 61 73  ity rule. It pas
1430: 73 65 73 20 74 68 65 20 63 68 61 72 61 63 74 65  ses the characte
1440: 72 0a 3b 20 64 61 74 61 20 66 72 6f 6d 20 74 68  r.; data from th
1450: 65 20 73 6f 75 72 63 65 20 53 58 4d 4c 20 74 72  e source SXML tr
1460: 65 65 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74  ee to the result
1470: 20 53 58 4d 4c 20 74 72 65 65 20 61 73 20 74 68   SXML tree as th
1480: 65 79 20 61 72 65 2e 0a 3b 20 54 68 65 20 2a 64  ey are..; The *d
1490: 65 66 61 75 6c 74 2a 20 72 75 6c 65 20 69 73 20  efault* rule is 
14a0: 61 6c 73 6f 20 61 6e 20 69 64 65 6e 74 69 74 79  also an identity
14b0: 20 72 75 6c 65 2e 20 41 20 2a 70 72 65 6f 72 64   rule. A *preord
14c0: 65 72 2a 20 6c 61 62 65 6c 20 62 79 0a 3b 20 74  er* label by.; t
14d0: 68 65 20 72 75 6c 65 20 74 65 6c 6c 73 20 70 72  he rule tells pr
14e0: 65 2d 70 6f 73 74 2d 6f 72 64 65 72 20 74 6f 20  e-post-order to 
14f0: 72 65 74 75 72 6e 20 61 20 6e 6f 6e 2d 27 73 65  return a non-'se
1500: 63 74 69 6f 6e 27 20 62 72 61 6e 63 68 20 61 73  ction' branch as
1510: 20 69 74 0a 3b 20 69 73 2c 20 5f 77 69 74 68 6f   it.; is, _witho
1520: 75 74 5f 20 72 65 63 75 72 73 69 6e 67 20 69 6e  ut_ recursing in
1530: 74 6f 20 69 74 2e 20 20 41 20 27 73 65 63 74 69  to it.  A 'secti
1540: 6f 6e 27 20 72 75 6c 65 20 74 65 6c 6c 73 20 77  on' rule tells w
1550: 68 61 74 20 74 6f 20 64 6f 0a 3b 20 77 68 65 6e  hat to do.; when
1560: 20 77 65 20 65 6e 63 6f 75 6e 74 65 72 20 61 20   we encounter a 
1570: 73 65 63 74 69 6f 6e 3a 20 77 65 20 6d 61 6b 65  section: we make
1580: 20 61 20 28 2a 73 65 63 74 69 6f 6e 20 2e 2e 2e   a (*section ...
1590: 29 20 65 6c 65 6d 65 6e 74 20 6f 75 74 20 6f 66  ) element out of
15a0: 0a 3b 20 74 68 65 20 74 69 74 6c 65 20 61 6e 64  .; the title and
15b0: 20 74 68 65 20 5f 6e 75 6d 62 65 72 65 64 5f 20   the _numbered_ 
15c0: 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 65 20  children of the 
15d0: 73 65 63 74 69 6f 6e 20 69 6e 20 71 75 65 73 74  section in quest
15e0: 69 6f 6e 2e 0a 3b 0a 3b 20 57 65 20 73 68 6f 75  ion..;.; We shou
15f0: 6c 64 20 70 6f 69 6e 74 20 6f 75 74 20 74 68 61  ld point out tha
1600: 74 20 74 68 65 20 74 72 61 76 65 72 73 61 6c 20  t the traversal 
1610: 63 6f 6d 62 69 6e 61 74 6f 72 20 70 72 65 2d 70  combinator pre-p
1620: 6f 73 74 2d 6f 72 64 65 72 20 69 73 0a 3b 20 61  ost-order is.; a
1630: 6e 20 6f 72 64 69 6e 61 72 79 20 53 63 68 65 6d  n ordinary Schem
1640: 65 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64 20  e function, and 
1650: 63 61 6e 20 62 65 20 5f 6d 61 70 70 65 64 5f 2e  can be _mapped_.
1660: 20 54 68 65 20 73 74 79 6c 65 73 68 65 65 74 0a   The stylesheet.
1670: 3b 20 68 61 6e 64 6c 65 72 73 20 61 72 65 20 6c  ; handlers are l
1680: 69 6b 65 77 69 73 65 20 6f 72 64 69 6e 61 72 79  ikewise ordinary
1690: 20 53 63 68 65 6d 65 20 66 75 6e 63 74 69 6f 6e   Scheme function
16a0: 73 2c 20 77 68 69 63 68 20 63 61 6e 20 69 6e 76  s, which can inv
16b0: 6f 6b 65 0a 3b 20 6f 74 68 65 72 20 53 63 68 65  oke.; other Sche
16c0: 6d 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 69 6e  me functions, in
16d0: 63 6c 75 64 69 6e 67 20 70 72 65 2d 70 6f 73 74  cluding pre-post
16e0: 2d 6f 72 64 65 72 20 69 74 73 65 6c 66 2e 0a 0a  -order itself...
16f0: 28 64 65 66 69 6e 65 20 28 6e 75 6d 62 65 72 2d  (define (number-
1700: 73 65 63 74 69 6f 6e 73 20 61 6e 63 65 73 74 6f  sections ancesto
1710: 72 2d 6e 75 6d 62 65 72 73 20 73 65 63 74 69 6f  r-numbers sectio
1720: 6e 73 29 0a 20 20 28 6d 61 70 20 28 6c 61 6d 62  ns).  (map (lamb
1730: 64 61 20 28 65 6c 20 69 29 20 0a 09 20 28 70 72  da (el i) .. (pr
1740: 65 2d 70 6f 73 74 2d 6f 72 64 65 72 20 65 6c 0a  e-post-order el.
1750: 09 20 20 20 3b 20 74 68 65 20 73 74 79 6c 65 73  .   ; the styles
1760: 68 65 65 74 0a 09 20 20 20 60 28 28 73 65 63 74  heet..   `((sect
1770: 69 6f 6e 20 2a 70 72 65 6f 72 64 65 72 2a 0a 09  ion *preorder*..
1780: 20 20 20 20 20 20 20 2e 20 2c 28 6c 61 6d 62 64         . ,(lambd
1790: 61 20 28 74 61 67 20 74 69 74 6c 65 20 2e 20 65  a (tag title . e
17a0: 6c 65 6d 73 29 0a 09 09 20 20 20 20 28 6c 65 74  lems)...    (let
17b0: 20 28 28 6d 79 2d 6e 75 6d 62 65 72 20 28 63 6f   ((my-number (co
17c0: 6e 73 20 69 20 61 6e 63 65 73 74 6f 72 2d 6e 75  ns i ancestor-nu
17d0: 6d 62 65 72 73 29 29 29 0a 09 09 20 20 20 20 28  mbers)))...    (
17e0: 63 6f 6e 73 2a 20 27 2a 73 65 63 74 69 6f 6e 20  cons* '*section 
17f0: 6d 79 2d 6e 75 6d 62 65 72 20 74 69 74 6c 65 0a  my-number title.
1800: 09 09 20 20 20 20 20 20 28 6e 75 6d 62 65 72 2d  ..      (number-
1810: 73 65 63 74 69 6f 6e 73 20 6d 79 2d 6e 75 6d 62  sections my-numb
1820: 65 72 20 65 6c 65 6d 73 29 29 29 29 29 0a 09 20  er elems))))).. 
1830: 20 20 20 20 20 28 2a 64 65 66 61 75 6c 74 2a 20       (*default* 
1840: 2a 70 72 65 6f 72 64 65 72 2a 20 2e 20 2c 28 6c  *preorder* . ,(l
1850: 61 6d 62 64 61 20 78 20 78 29 29 0a 09 20 20 20  ambda x x))..   
1860: 20 20 20 28 2a 74 65 78 74 2a 20 2e 20 2c 28 6c     (*text* . ,(l
1870: 61 6d 62 64 61 20 28 74 61 67 20 73 74 72 29 20  ambda (tag str) 
1880: 73 74 72 29 29 29 29 29 0a 20 20 20 20 73 65 63  str))))).    sec
1890: 74 69 6f 6e 73 20 28 6c 69 73 74 2d 6e 75 6d 62  tions (list-numb
18a0: 65 72 69 6e 67 20 73 65 63 74 69 6f 6e 73 29 29  ering sections))
18b0: 29 0a 0a 0a 0a 3b 20 42 75 69 6c 64 69 6e 67 20  )....; Building 
18c0: 61 20 68 69 65 72 61 72 63 68 69 63 61 6c 20 74  a hierarchical t
18d0: 61 62 6c 65 20 6f 66 20 63 6f 6e 74 65 6e 74 73  able of contents
18e0: 0a 3b 0a 3b 20 49 74 20 69 73 20 6d 6f 72 65 20  .;.; It is more 
18f0: 6c 75 63 69 64 20 74 6f 20 62 75 69 6c 64 20 74  lucid to build t
1900: 68 65 20 54 4f 43 20 69 6e 20 61 20 73 65 70 61  he TOC in a sepa
1910: 72 61 74 65 20 70 61 73 73 2c 20 62 79 20 74 72  rate pass, by tr
1920: 61 76 65 72 73 69 6e 67 0a 3b 20 74 68 65 20 70  aversing.; the p
1930: 72 65 76 69 6f 75 73 6c 79 20 6e 75 6d 62 65 72  reviously number
1940: 65 64 20 73 65 63 74 69 6f 6e 73 2e 20 49 6e 20  ed sections. In 
1950: 74 68 69 73 20 70 61 73 73 2c 20 77 65 20 74 75  this pass, we tu
1960: 72 6e 20 27 2a 73 65 63 74 69 6f 6e 27 0a 3b 20  rn '*section'.; 
1970: 65 6c 65 6d 65 6e 74 73 20 69 6e 74 6f 20 54 4f  elements into TO
1980: 43 20 65 6e 74 72 69 65 73 2c 20 61 6e 64 20 72  C entries, and r
1990: 65 77 72 69 74 65 20 65 76 65 72 79 74 68 69 6e  ewrite everythin
19a0: 67 20 65 6c 73 65 20 74 6f 20 6e 6f 74 68 69 6e  g else to nothin
19b0: 67 2e 0a 3b 0a 3b 20 54 68 65 20 66 75 6e 63 74  g..;.; The funct
19c0: 69 6f 6e 20 6d 61 6b 65 2d 74 6f 63 2d 65 6e 74  ion make-toc-ent
19d0: 72 69 65 73 20 74 61 6b 65 73 20 74 68 65 20 6f  ries takes the o
19e0: 75 74 70 75 74 20 6f 66 20 74 68 65 20 70 72 6f  utput of the pro
19f0: 63 65 64 75 72 65 0a 3b 20 27 6e 75 6d 62 65 72  cedure.; 'number
1a00: 2d 73 65 63 74 69 6f 6e 73 27 20 61 6e 64 20 79  -sections' and y
1a10: 69 65 6c 64 73 20 74 68 65 20 6c 69 73 74 20 6f  ields the list o
1a20: 66 20 54 4f 43 20 65 6e 74 72 69 65 73 2e 20 54  f TOC entries. T
1a30: 6f 20 62 65 20 6d 6f 72 65 0a 3b 20 70 72 65 63  o be more.; prec
1a40: 69 73 65 2c 20 77 65 20 64 6f 20 74 68 65 20 66  ise, we do the f
1a50: 6f 6c 6c 6f 77 69 6e 67 20 74 72 61 6e 73 66 6f  ollowing transfo
1a60: 72 6d 61 74 69 6f 6e 3a 0a 3b 0a 3b 20 20 20 28  rmation:.;.;   (
1a70: 28 2a 73 65 63 74 69 6f 6e 20 28 31 29 20 74 69  (*section (1) ti
1a80: 74 6c 65 31 20 6e 6f 6e 2d 73 65 63 74 69 6f 6e  tle1 non-section
1a90: 2d 65 6c 20 2e 2e 2e 29 0a 3b 20 20 20 20 28 2a  -el ...).;    (*
1aa0: 73 65 63 74 69 6f 6e 20 28 32 29 20 74 69 74 6c  section (2) titl
1ab0: 65 32 20 28 2a 73 65 63 74 69 6f 6e 20 28 31 20  e2 (*section (1 
1ac0: 32 29 20 74 69 74 6c 65 32 31 20 65 6c 20 2e 2e  2) title21 el ..
1ad0: 2e 29 20 65 6c 20 2e 2e 2e 29 20 2e 2e 2e 29 0a  .) el ...) ...).
1ae0: 3b 20 3d 3e 0a 3b 20 20 20 28 28 6c 69 20 22 31  ; =>.;   ((li "1
1af0: 2e 20 22 20 74 69 74 6c 65 31 29 0a 3b 20 20 20  . " title1).;   
1b00: 20 28 6c 69 20 22 32 2e 20 22 20 74 69 74 6c 65   (li "2. " title
1b10: 32 20 28 75 6c 20 28 6c 69 20 22 32 2e 31 2e 20  2 (ul (li "2.1. 
1b20: 22 20 74 69 74 6c 65 32 31 29 20 2e 2e 2e 29 29  " title21) ...))
1b30: 20 2e 2e 2e 29 0a 3b 0a 3b 20 41 67 61 69 6e 2c   ...).;.; Again,
1b40: 20 77 65 20 65 78 65 63 75 74 65 20 74 68 65 20   we execute the 
1b50: 70 72 65 2d 70 6f 73 74 2d 6f 72 64 65 72 20 74  pre-post-order t
1b60: 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 20 77 69  ransformation wi
1b70: 74 68 20 61 0a 3b 20 74 68 72 65 65 2d 72 75 6c  th a.; three-rul
1b80: 65 20 73 74 79 6c 65 73 68 65 65 74 2e 20 20 41  e stylesheet.  A
1b90: 73 20 62 65 66 6f 72 65 2c 20 74 68 65 20 63 68  s before, the ch
1ba0: 61 72 61 63 74 65 72 20 64 61 74 61 20 61 72 65  aracter data are
1bb0: 20 6e 6f 74 0a 3b 20 61 66 66 65 63 74 65 64 3a   not.; affected:
1bc0: 20 73 65 65 20 74 68 65 20 2a 74 65 78 74 2a 20   see the *text* 
1bd0: 72 75 6c 65 2e 20 20 54 68 65 20 2a 64 65 66 61  rule.  The *defa
1be0: 75 6c 74 2a 20 72 75 6c 65 20 69 73 20 64 69 66  ult* rule is dif
1bf0: 66 65 72 65 6e 74 20 6e 6f 77 3a 0a 3b 20 69 74  ferent now:.; it
1c00: 20 74 72 61 6e 73 66 6f 72 6d 73 20 61 20 6e 6f   transforms a no
1c10: 6e 2d 27 73 65 63 74 69 6f 6e 27 20 62 72 61 6e  n-'section' bran
1c20: 63 68 20 74 6f 20 6e 6f 74 68 69 6e 67 20 61 74  ch to nothing at
1c30: 20 61 6c 6c 2e 20 54 6f 20 62 65 20 6d 6f 72 65   all. To be more
1c40: 0a 3b 20 70 72 65 63 69 73 65 2c 20 61 6e 20 53  .; precise, an S
1c50: 58 4d 4c 20 65 6c 65 6d 65 6e 74 20 6f 74 68 65  XML element othe
1c60: 72 20 74 68 61 6e 20 2a 73 65 63 74 69 6f 6e 20  r than *section 
1c70: 69 73 20 74 75 72 6e 65 64 20 69 6e 74 6f 20 27  is turned into '
1c80: 28 29 2c 0a 3b 20 77 68 69 63 68 20 77 69 6c 6c  (),.; which will
1c90: 20 62 65 20 66 69 6c 74 65 72 65 64 20 6f 75 74   be filtered out
1ca0: 20 6c 61 74 65 72 2e 20 41 73 20 61 20 6d 61 74   later. As a mat
1cb0: 74 65 72 20 6f 66 20 66 61 63 74 2c 20 77 65 20  ter of fact, we 
1cc0: 64 6f 20 6e 6f 74 0a 3b 20 68 61 76 65 20 74 6f  do not.; have to
1cd0: 20 66 69 6c 74 65 72 20 6f 75 74 20 27 28 29 20   filter out '() 
1ce0: 62 65 63 61 75 73 65 20 74 68 65 79 20 77 69 6c  because they wil
1cf0: 6c 20 62 65 20 64 69 73 72 65 67 61 72 64 65 64  l be disregarded
1d00: 20 62 79 20 74 68 65 0a 3b 20 53 52 56 3a 73 65   by the.; SRV:se
1d10: 6e 64 2d 72 65 70 6c 79 20 66 75 6e 63 74 69 6f  nd-reply functio
1d20: 6e 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e  n at the very en
1d30: 64 2e 20 49 6e 20 61 6e 79 20 63 61 73 65 2c 20  d. In any case, 
1d40: 77 65 20 63 61 6e 20 72 65 67 61 72 64 0a 3b 20  we can regard.; 
1d50: 74 68 65 20 65 6d 70 74 79 20 6c 69 73 74 20 61  the empty list a
1d60: 73 20 62 65 69 6e 67 20 6e 6f 74 68 69 6e 67 2e  s being nothing.
1d70: 0a 3b 0a 3b 20 54 68 65 20 73 74 79 6c 65 73 68  .;.; The stylesh
1d80: 65 65 74 20 61 6c 6d 6f 73 74 20 6c 69 74 65 72  eet almost liter
1d90: 61 6c 6c 79 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  ally looks like 
1da0: 74 68 65 20 61 62 6f 76 65 20 72 65 2d 77 72 69  the above re-wri
1db0: 74 69 6e 67 0a 3b 20 65 78 61 6d 70 6c 65 2e 20  ting.; example. 
1dc0: 20 57 65 20 73 68 6f 75 6c 64 20 6e 6f 74 65 20   We should note 
1dd0: 74 68 61 74 20 74 68 65 20 73 74 79 6c 65 73 68  that the stylesh
1de0: 65 65 74 20 72 75 6c 65 73 20 61 72 65 20 61 70  eet rules are ap
1df0: 70 6c 69 65 64 20 74 6f 0a 3b 20 61 6c 6c 20 65  plied to.; all e
1e00: 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74  lements of the t
1e10: 72 65 65 2c 20 72 65 63 75 72 73 69 76 65 6c 79  ree, recursively
1e20: 2e 20 57 65 20 69 6e 64 65 65 64 20 70 72 6f 63  . We indeed proc
1e30: 65 73 73 20 74 68 65 0a 3b 20 61 72 62 69 74 72  ess the.; arbitr
1e40: 61 72 79 20 6e 65 73 74 69 6e 67 20 6f 66 20 2a  ary nesting of *
1e50: 73 65 63 74 69 6f 6e 73 20 77 69 74 68 6f 75 74  sections without
1e60: 20 6d 75 63 68 20 61 64 6f 2e 20 57 65 20 64 6f   much ado. We do
1e70: 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 3b 20 77   not need to.; w
1e80: 72 69 74 65 20 73 6f 6d 65 74 68 69 6e 67 20 6c  rite something l
1e90: 69 6b 65 20 3c 61 70 70 6c 79 2d 74 65 6d 70 6c  ike <apply-templ
1ea0: 61 74 65 73 2f 3e 2e 20 20 55 6e 6c 69 6b 65 20  ates/>.  Unlike 
1eb0: 58 53 4c 54 2c 20 62 75 74 20 6c 69 6b 65 20 74  XSLT, but like t
1ec0: 72 65 65 0a 3b 20 66 6f 6c 64 2c 20 74 68 65 20  ree.; fold, the 
1ed0: 70 72 65 2d 70 6f 73 74 2d 6f 72 64 65 72 20 63  pre-post-order c
1ee0: 6f 6d 62 69 6e 61 74 6f 72 20 74 72 61 76 65 72  ombinator traver
1ef0: 73 65 73 20 74 68 65 20 74 72 65 65 20 69 6e 20  ses the tree in 
1f00: 70 6f 73 74 2d 6f 72 64 65 72 0a 3b 20 62 79 20  post-order.; by 
1f10: 64 65 66 61 75 6c 74 2e 20 54 68 61 74 20 69 73  default. That is
1f20: 2c 20 74 68 65 20 68 61 6e 64 6c 65 72 20 66 6f  , the handler fo
1f30: 72 20 74 68 65 20 2a 73 65 63 74 69 6f 6e 20 72  r the *section r
1f40: 75 6c 65 20 62 65 6c 6f 77 20 28 6c 61 6d 62 64  ule below (lambd
1f50: 61 0a 3b 20 28 74 61 67 20 6e 75 6d 62 65 72 69  a.; (tag numberi
1f60: 6e 67 20 74 69 74 6c 65 20 2e 20 65 6c 65 6d 73  ng title . elems
1f70: 29 20 2e 2e 2e 29 20 72 65 63 65 69 76 65 73 2c  ) ...) receives,
1f80: 20 61 73 20 27 65 6c 65 6d 73 27 2c 20 74 68 65   as 'elems', the
1f90: 20 6c 69 73 74 20 6f 66 0a 3b 20 74 68 65 20 5f   list of.; the _
1fa0: 74 72 61 6e 73 66 6f 72 6d 65 64 5f 20 63 68 69  transformed_ chi
1fb0: 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 2a 73 65  ldren of the *se
1fc0: 63 74 69 6f 6e 20 69 6e 20 71 75 65 73 74 69 6f  ction in questio
1fd0: 6e 2e 20 54 6f 20 62 65 20 6d 6f 72 65 0a 3b 20  n. To be more.; 
1fe0: 70 72 65 63 69 73 65 2c 20 27 65 6c 65 6d 73 27  precise, 'elems'
1ff0: 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6c 69 73   will be the lis
2000: 74 20 6f 66 20 54 4f 43 20 65 6c 65 6d 65 6e 74  t of TOC element
2010: 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 3b  s for internal.;
2020: 20 73 75 62 73 65 63 74 69 6f 6e 73 20 2d 2d 20   subsections -- 
2030: 6f 72 20 74 68 65 20 6c 69 73 74 20 6f 66 20 6e  or the list of n
2040: 6f 74 68 69 6e 67 2e 0a 3b 0a 3b 20 57 65 20 75  othing..;.; We u
2050: 73 65 20 61 6e 20 61 75 78 69 6c 69 61 72 79 20  se an auxiliary 
2060: 66 75 6e 63 74 69 6f 6e 20 27 6e 75 6d 62 65 72  function 'number
2070: 73 2d 3e 73 74 72 69 6e 67 27 20 74 6f 20 63 6f  s->string' to co
2080: 6e 76 65 72 74 20 74 68 65 20 6c 69 73 74 0a 3b  nvert the list.;
2090: 20 6f 66 20 6e 75 6d 65 72 69 63 61 6c 20 6c 61   of numerical la
20a0: 62 65 6c 73 20 73 75 63 68 20 61 73 20 28 31 20  bels such as (1 
20b0: 32 20 33 29 20 69 6e 74 6f 20 61 20 73 74 72 69  2 3) into a stri
20c0: 6e 67 20 6c 61 62 65 6c 20 22 33 2e 32 2e 31 22  ng label "3.2.1"
20d0: 0a 0a 28 64 65 66 69 6e 65 20 28 6d 61 6b 65 2d  ..(define (make-
20e0: 74 6f 63 2d 65 6e 74 72 69 65 73 20 6e 75 6d 62  toc-entries numb
20f0: 65 72 65 64 2d 73 65 63 74 69 6f 6e 73 29 0a 20  ered-sections). 
2100: 20 28 70 72 65 2d 70 6f 73 74 2d 6f 72 64 65 72   (pre-post-order
2110: 20 6e 75 6d 62 65 72 65 64 2d 73 65 63 74 69 6f   numbered-sectio
2120: 6e 73 0a 20 20 20 20 3b 20 74 68 65 20 73 74 79  ns.    ; the sty
2130: 6c 65 73 68 65 65 74 0a 20 20 20 20 60 28 28 2a  lesheet.    `((*
2140: 73 65 63 74 69 6f 6e 0a 09 2e 20 2c 28 6c 61 6d  section... ,(lam
2150: 62 64 61 20 28 74 61 67 20 6e 75 6d 62 65 72 69  bda (tag numberi
2160: 6e 67 20 74 69 74 6c 65 20 2e 20 65 6c 65 6d 73  ng title . elems
2170: 29 0a 09 20 20 20 20 20 28 6c 65 74 20 28 28 65  )..     (let ((e
2180: 6c 65 6d 73 20 28 66 69 6c 74 65 72 20 70 61 69  lems (filter pai
2190: 72 3f 20 65 6c 65 6d 73 29 29 29 0a 09 20 20 20  r? elems)))..   
21a0: 20 20 20 20 60 28 6c 69 20 2c 28 6e 75 6d 62 65      `(li ,(numbe
21b0: 72 73 2d 3e 73 74 72 69 6e 67 20 6e 75 6d 62 65  rs->string numbe
21c0: 72 69 6e 67 29 20 22 2e 20 22 20 2c 74 69 74 6c  ring) ". " ,titl
21d0: 65 0a 09 09 20 20 2c 28 61 6e 64 20 28 70 61 69  e...  ,(and (pai
21e0: 72 3f 20 65 6c 65 6d 73 29 20 28 6c 69 73 74 20  r? elems) (list 
21f0: 27 75 6c 20 65 6c 65 6d 73 29 29 29 29 29 29 0a  'ul elems)))))).
2200: 20 20 20 20 20 20 20 28 2a 64 65 66 61 75 6c 74         (*default
2210: 2a 20 2e 20 2c 28 6c 61 6d 62 64 61 20 5f 20 27  * . ,(lambda _ '
2220: 28 29 29 29 0a 20 20 20 20 20 20 20 28 2a 74 65  ())).       (*te
2230: 78 74 2a 20 2e 20 2c 28 6c 61 6d 62 64 61 20 28  xt* . ,(lambda (
2240: 74 61 67 20 73 74 72 29 20 73 74 72 29 29 29 29  tag str) str))))
2250: 29 0a 0a 0a 0a 3b 20 54 68 65 20 6d 61 69 6e 20  )....; The main 
2260: 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 20 73  transformation s
2270: 74 79 6c 65 73 68 65 65 74 3a 20 66 72 6f 6d 20  tylesheet: from 
2280: 74 68 65 20 73 6f 75 72 63 65 20 53 58 4d 4c 20  the source SXML 
2290: 64 6f 63 75 6d 65 6e 74 0a 3b 20 74 6f 20 74 68  document.; to th
22a0: 65 20 74 72 65 65 20 6f 66 20 48 54 4d 4c 20 66  e tree of HTML f
22b0: 72 61 67 6d 65 6e 74 73 2e 0a 3b 0a 3b 20 4e 6f  ragments..;.; No
22c0: 74 65 20 5b 31 5d 0a 3b 20 54 68 65 20 67 65 6e  te [1].; The gen
22d0: 65 72 61 6c 20 53 58 4d 4c 2d 74 6f 2d 48 54 4d  eral SXML-to-HTM
22e0: 4c 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  L conversion is 
22f0: 74 61 6b 65 6e 20 63 61 72 65 20 62 79 20 74 68  taken care by th
2300: 65 20 2a 64 65 66 61 75 6c 74 2a 0a 3b 20 61 6e  e *default*.; an
2310: 64 20 2a 74 65 78 74 2a 20 72 75 6c 65 73 20 69  d *text* rules i
2320: 6e 20 27 75 6e 69 76 65 72 73 61 6c 2d 63 6f 6e  n 'universal-con
2330: 76 65 72 73 69 6f 6e 2d 72 75 6c 65 73 27 2c 20  version-rules', 
2340: 77 68 69 63 68 20 61 72 65 20 64 65 66 69 6e 65  which are define
2350: 64 0a 3b 20 69 6e 20 53 58 4d 4c 2d 74 6f 2d 48  d.; in SXML-to-H
2360: 54 4d 4c 2d 65 78 74 2e 73 63 6d 2e 20 78 54 68  TML-ext.scm. xTh
2370: 65 20 2a 74 65 78 74 2a 20 72 75 6c 65 20 63 68  e *text* rule ch
2380: 65 63 6b 73 20 74 65 78 74 20 73 74 72 69 6e 67  ecks text string
2390: 73 20 66 6f 72 0a 3b 20 64 61 6e 67 65 72 6f 75  s for.; dangerou
23a0: 73 20 63 68 61 72 61 63 74 65 72 73 20 73 75 63  s characters suc
23b0: 68 20 61 73 20 61 6e 67 75 6c 61 72 20 62 72 61  h as angular bra
23c0: 63 6b 65 74 73 20 61 6e 64 20 74 68 65 20 61 6d  ckets and the am
23d0: 70 65 72 73 61 6e 64 2e 20 54 68 65 0a 3b 20 72  persand. The.; r
23e0: 75 6c 65 20 65 6e 63 6f 64 65 73 20 74 68 65 73  ule encodes thes
23f0: 65 20 63 68 61 72 61 63 74 65 72 73 20 61 73 20  e characters as 
2400: 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
2410: 67 20 48 54 4d 4c 20 65 6e 74 69 74 69 65 73 2e  g HTML entities.
2420: 0a 3b 20 54 68 65 20 2a 64 65 66 61 75 6c 74 2a  .; The *default*
2430: 20 72 75 6c 65 20 74 75 72 6e 73 20 61 6e 20 53   rule turns an S
2440: 58 4d 4c 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f  XML element into
2450: 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
2460: 20 48 54 4d 4c 0a 3b 20 65 6c 65 6d 65 6e 74 2e   HTML.; element.
2470: 20 54 68 65 73 65 20 74 77 6f 20 74 72 61 6e 73   These two trans
2480: 66 6f 72 6d 61 74 69 6f 6e 73 20 77 69 6c 6c 20  formations will 
2490: 62 65 20 75 6e 69 66 6f 72 6d 6c 79 20 61 70 70  be uniformly app
24a0: 6c 69 65 64 20 74 6f 20 61 6c 6c 0a 3b 20 6e 6f  lied to all.; no
24b0: 64 65 73 20 6f 66 20 74 68 65 20 73 6f 75 72 63  des of the sourc
24c0: 65 20 53 58 4d 4c 20 74 72 65 65 2e 20 57 65 20  e SXML tree. We 
24d0: 5f 6f 6e 6c 79 5f 20 6e 65 65 64 20 74 6f 20 61  _only_ need to a
24e0: 64 64 20 72 75 6c 65 73 20 66 6f 72 20 74 68 65  dd rules for the
24f0: 0a 3b 20 53 58 4d 4c 20 65 6c 65 6d 65 6e 74 73  .; SXML elements
2500: 20 74 68 61 74 20 68 61 76 65 20 74 6f 20 62 65   that have to be
2510: 20 74 72 65 61 74 65 64 20 69 6e 20 61 20 73 70   treated in a sp
2520: 65 63 69 61 6c 20 77 61 79 2e 0a 3b 0a 3b 20 4e  ecial way..;.; N
2530: 6f 74 65 20 5b 32 5d 0a 3b 20 54 68 65 20 27 62  ote [2].; The 'b
2540: 6f 64 79 27 20 6f 66 20 74 68 65 20 64 6f 63 75  ody' of the docu
2550: 6d 65 6e 74 2c 20 61 20 63 6f 6c 6c 65 63 74 69  ment, a collecti
2560: 6f 6e 20 6f 66 20 73 65 63 74 69 6f 6e 73 2c 20  on of sections, 
2570: 68 61 73 20 74 6f 20 62 65 0a 3b 20 70 72 6f 63  has to be.; proc
2580: 65 73 73 65 64 20 73 70 65 63 69 61 6c 6c 79 2e  essed specially.
2590: 20 20 46 69 72 73 74 20 77 65 20 72 65 63 75 72    First we recur
25a0: 73 69 76 65 6c 79 20 6e 75 6d 62 65 72 20 74 68  sively number th
25b0: 65 20 73 65 63 74 69 6f 6e 73 20 61 6e 64 0a 3b  e sections and.;
25c0: 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 77 69   replace them wi
25d0: 74 68 20 2a 73 65 63 74 69 6f 6e 73 2e 20 20 46  th *sections.  F
25e0: 72 61 6e 6b 6c 79 2c 20 74 68 65 72 65 20 69 73  rankly, there is
25f0: 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 74 72   no need to intr
2600: 6f 64 75 63 65 0a 3b 20 74 68 65 20 61 75 78 69  oduce.; the auxi
2610: 6c 69 61 72 79 20 65 6c 65 6d 65 6e 74 20 2a 73  liary element *s
2620: 65 63 74 69 6f 6e 2e 20 20 44 6f 69 6e 67 20 73  ection.  Doing s
2630: 6f 20 68 6f 77 65 76 65 72 20 73 65 65 6d 73 20  o however seems 
2640: 74 6f 20 6d 61 6b 65 20 74 68 65 0a 3b 20 65 78  to make the.; ex
2650: 61 6d 70 6c 65 20 63 6c 65 61 72 65 72 2e 20 20  ample clearer.  
2660: 57 65 20 70 61 73 73 20 74 68 65 20 72 65 6e 75  We pass the renu
2670: 6d 62 65 72 65 64 20 73 65 63 74 69 6f 6e 73 20  mbered sections 
2680: 74 6f 0a 3b 20 6d 61 6b 65 2d 74 6f 63 2d 65 6e  to.; make-toc-en
2690: 74 72 69 65 73 20 61 6e 64 20 67 65 74 20 74 68  tries and get th
26a0: 65 20 6c 69 73 74 20 6f 66 20 54 4f 43 20 65 6e  e list of TOC en
26b0: 74 72 69 65 73 20 28 61 73 20 27 6c 6f 77 2d 6c  tries (as 'low-l
26c0: 65 76 65 6c 27 0a 3b 20 53 58 4d 4c 20 65 6c 65  evel'.; SXML ele
26d0: 6d 65 6e 74 73 29 2e 0a 3b 0a 3b 20 4e 6f 74 65  ments)..;.; Note
26e0: 20 5b 33 5d 0a 3b 20 57 65 20 69 6e 73 65 72 74   [3].; We insert
26f0: 20 74 68 65 20 54 4f 43 20 65 6c 65 6d 65 6e 74   the TOC element
2700: 73 20 62 65 66 6f 72 65 20 74 68 65 20 6e 75 6d  s before the num
2710: 62 65 72 65 64 20 73 65 63 74 69 6f 6e 73 2c 20  bered sections, 
2720: 61 6e 64 0a 3b 20 72 65 2d 61 70 70 6c 79 20 6d  and.; re-apply m
2730: 61 69 6e 2d 73 73 2e 20 41 63 74 75 61 6c 6c 79  ain-ss. Actually
2740: 2c 20 77 65 20 72 65 2d 61 70 70 6c 79 20 61 20  , we re-apply a 
2750: 73 6c 69 67 68 74 6c 79 20 22 6d 6f 64 69 66 69  slightly "modifi
2760: 65 64 22 0a 3b 20 73 74 79 6c 65 73 68 65 65 74  ed".; stylesheet
2770: 3a 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 27 62  : the element 'b
2780: 6f 64 79 27 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e  ody' no longer n
2790: 65 65 64 73 20 74 6f 20 62 65 20 70 72 6f 63 65  eeds to be proce
27a0: 73 73 65 64 0a 3b 20 73 70 65 63 69 61 6c 6c 79  ssed.; specially
27b0: 2e 20 20 49 6e 20 74 68 65 20 70 72 65 73 65 6e  .  In the presen
27c0: 74 20 65 78 61 6d 70 6c 65 2c 20 74 68 69 73 20  t example, this 
27d0: 27 73 77 69 74 63 68 69 6e 67 20 6f 66 66 27 20  'switching off' 
27e0: 6f 66 20 61 20 72 75 6c 65 0a 3b 20 69 73 20 61  of a rule.; is a
27f0: 20 62 69 74 20 63 6f 6e 74 72 69 76 65 64 2e 20   bit contrived. 
2800: 20 57 65 20 77 61 6e 74 65 64 20 74 6f 20 69 6c   We wanted to il
2810: 6c 75 73 74 72 61 74 65 20 68 6f 77 65 76 65 72  lustrate however
2820: 20 74 68 65 20 61 62 69 6c 69 74 79 0a 3b 20 74   the ability.; t
2830: 6f 20 72 65 2d 61 70 70 6c 79 20 61 20 73 74 79  o re-apply a sty
2840: 6c 65 73 68 65 65 74 20 77 69 74 68 20 73 6f 6d  lesheet with som
2850: 65 20 64 79 6e 61 6d 69 63 20 27 6d 6f 64 69 66  e dynamic 'modif
2860: 69 63 61 74 69 6f 6e 73 27 2e 20 20 58 53 4c 54  ications'.  XSLT
2870: 0a 3b 20 63 61 6e 20 61 63 63 6f 6d 70 6c 69 73  .; can accomplis
2880: 68 20 73 6f 6d 65 74 68 69 6e 67 20 73 69 6d 69  h something simi
2890: 6c 61 72 2c 20 77 69 74 68 20 74 68 65 20 68 65  lar, with the he
28a0: 6c 70 20 6f 66 20 6d 6f 64 65 73 2e 20 53 58 53  lp of modes. SXS
28b0: 4c 54 0a 3b 20 67 69 76 65 73 20 66 61 72 20 73  LT.; gives far s
28c0: 69 6d 70 6c 65 72 20 74 6f 6f 6c 73 20 74 6f 20  impler tools to 
28d0: 64 79 6e 61 6d 69 63 61 6c 6c 79 20 27 6d 6f 64  dynamically 'mod
28e0: 69 66 79 27 20 74 68 65 20 65 66 66 65 63 74 69  ify' the effecti
28f0: 76 65 0a 3b 20 72 75 6c 65 73 65 74 2e 20 20 54  ve.; ruleset.  T
2900: 68 65 72 65 20 61 72 65 2c 20 6f 66 20 63 6f 75  here are, of cou
2910: 72 73 65 2c 20 6e 6f 20 6d 75 74 61 74 69 6f 6e  rse, no mutation
2920: 73 2e 20 57 65 20 6d 65 72 65 6c 79 20 72 65 2d  s. We merely re-
2930: 69 6e 76 6f 6b 65 0a 3b 20 70 72 65 2d 70 6f 73  invoke.; pre-pos
2940: 74 2d 6f 72 64 65 72 20 61 6e 64 20 70 61 73 73  t-order and pass
2950: 20 69 74 20 6d 61 69 6e 2d 73 73 20 77 69 74 68   it main-ss with
2960: 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20 72 75   the modified ru
2970: 6c 65 73 0a 3b 20 70 72 65 70 65 6e 64 65 64 2e  les.; prepended.
2980: 0a 3b 0a 3b 20 54 68 65 20 27 6f 76 65 72 72 69  .;.; The 'overri
2990: 64 64 65 6e 27 20 72 75 6c 65 20 66 6f 72 20 74  dden' rule for t
29a0: 68 65 20 27 62 6f 64 79 27 20 65 6c 65 6d 65 6e  he 'body' elemen
29b0: 74 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 68  t has the same h
29c0: 61 6e 64 6c 65 72 20 61 73 0a 3b 20 74 68 61 74  andler as.; that
29d0: 20 6f 66 20 74 68 65 20 2a 64 65 66 61 75 6c 74   of the *default
29e0: 2a 20 72 75 6c 65 2e 20 54 6f 20 66 69 6e 64 20  * rule. To find 
29f0: 74 68 65 20 6c 61 74 74 65 72 2c 20 77 65 20 5f  the latter, we _
2a00: 71 75 65 72 79 5f 20 74 68 65 0a 3b 20 73 74 79  query_ the.; sty
2a10: 6c 65 73 68 65 65 74 20 69 74 73 65 6c 66 21 20  lesheet itself! 
2a20: 20 49 6e 64 65 65 64 2c 20 74 68 65 20 73 74 79   Indeed, the sty
2a30: 6c 65 73 68 65 65 74 20 69 73 20 61 20 73 69 6d  lesheet is a sim
2a40: 70 6c 65 20 64 61 74 61 0a 3b 20 73 74 72 75 63  ple data.; struc
2a50: 74 75 72 65 2c 20 61 6e 20 61 73 73 6f 63 69 61  ture, an associa
2a60: 74 69 76 65 20 6c 69 73 74 2c 20 61 6e 64 20 63  tive list, and c
2a70: 61 6e 20 62 65 20 6d 61 6e 69 70 75 6c 61 74 65  an be manipulate
2a80: 64 20 61 73 20 73 75 63 68 2e 20 54 68 69 73 0a  d as such. This.
2a90: 3b 20 65 78 61 6d 70 6c 65 20 64 65 6d 6f 6e 73  ; example demons
2aa0: 74 72 61 74 65 73 20 72 65 66 6c 65 78 69 76 65  trates reflexive
2ab0: 20 61 62 69 6c 69 74 69 65 73 20 6f 66 20 53 58   abilities of SX
2ac0: 53 4c 54 2e 20 41 20 73 74 79 6c 65 73 68 65 65  SLT. A styleshee
2ad0: 74 20 63 61 6e 0a 3b 20 61 6e 61 6c 79 7a 65 20  t can.; analyze 
2ae0: 69 74 73 65 6c 66 2e 0a 3b 0a 3b 20 4e 6f 74 65  itself..;.; Note
2af0: 20 5b 34 5d 0a 3b 20 4f 6e 20 74 68 65 20 72 65   [4].; On the re
2b00: 2d 61 70 70 6c 69 63 61 74 69 6f 6e 20 70 61 73  -application pas
2b10: 73 2c 20 74 68 65 20 74 72 61 76 65 72 73 61 6c  s, the traversal
2b20: 20 63 6f 6d 62 69 6e 61 74 6f 72 20 74 72 65 61   combinator trea
2b30: 74 73 20 27 62 6f 64 79 27 0a 3b 20 61 73 20 61  ts 'body'.; as a
2b40: 6e 79 20 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74  ny other element
2b50: 2e 20 54 68 65 20 63 6f 6d 62 69 6e 61 74 6f 72  . The combinator
2b60: 20 70 72 6f 63 65 73 73 65 73 20 69 74 73 20 63   processes its c
2b70: 68 69 6c 64 72 65 6e 20 66 69 72 73 74 2c 0a 3b  hildren first,.;
2b80: 20 61 6e 64 20 6e 6f 77 20 6e 6f 74 69 63 65 73   and now notices
2b90: 20 2a 73 65 63 74 69 6f 6e 20 65 6c 65 6d 65 6e   *section elemen
2ba0: 74 73 2e 20 57 65 20 74 72 61 6e 73 66 6f 72 6d  ts. We transform
2bb0: 20 61 20 2a 73 65 63 74 69 6f 6e 20 61 73 0a 3b   a *section as.;
2bc0: 20 66 6f 6c 6c 6f 77 73 3a 0a 3b 20 20 20 28 2a   follows:.;   (*
2bd0: 73 65 63 74 69 6f 6e 20 28 31 20 32 20 33 29 20  section (1 2 3) 
2be0: 22 74 69 74 6c 65 33 32 31 22 20 65 6c 65 6d 20  "title321" elem 
2bf0: 2e 2e 2e 29 0a 3b 20 3d 3e 0a 3b 20 20 20 28 28  ...).; =>.;   ((
2c00: 68 34 20 22 33 2e 32 2e 31 2e 20 22 20 22 74 69  h4 "3.2.1. " "ti
2c10: 74 6c 65 33 32 31 22 29 20 65 6c 65 6d 20 2e 2e  tle321") elem ..
2c20: 2e 29 0a 3b 20 61 6e 64 20 72 65 2d 61 70 70 6c  .).; and re-appl
2c30: 79 20 74 68 65 20 6d 61 69 6e 20 73 74 79 6c 65  y the main style
2c40: 73 68 65 65 74 2e 20 4f 75 72 20 61 75 78 69 6c  sheet. Our auxil
2c50: 69 61 72 79 20 66 75 6e 63 74 69 6f 6e 0a 3b 20  iary function.; 
2c60: 6e 75 6d 62 65 72 73 2d 3e 73 74 72 69 6e 67 20  numbers->string 
2c70: 68 65 6c 70 73 20 75 73 20 74 6f 20 63 6f 6e 76  helps us to conv
2c80: 65 72 74 20 74 68 65 20 6c 69 73 74 20 6f 66 20  ert the list of 
2c90: 6c 61 62 65 6c 73 20 74 6f 20 61 0a 3b 20 73 74  labels to a.; st
2ca0: 72 69 6e 67 2e 20 57 65 20 75 73 65 20 74 68 65  ring. We use the
2cb0: 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6c   length of the l
2cc0: 69 73 74 20 28 74 68 61 74 20 69 73 2c 20 74 68  ist (that is, th
2cd0: 65 20 64 65 70 74 68 20 6f 66 20 74 68 65 0a 3b  e depth of the.;
2ce0: 20 73 65 63 74 69 6f 6e 20 69 6e 20 71 75 65 73   section in ques
2cf0: 74 69 6f 6e 29 20 74 6f 20 63 68 6f 6f 73 65 20  tion) to choose 
2d00: 74 68 65 20 48 54 4d 4c 20 74 61 67 20 66 6f 72  the HTML tag for
2d10: 20 74 68 65 20 73 65 63 74 69 6f 6e 3a 20 68 32   the section: h2
2d20: 2c 20 68 33 2c 0a 3b 20 68 34 2c 20 65 74 63 2e  , h3,.; h4, etc.
2d30: 0a 3b 0a 3b 20 57 65 20 73 68 6f 75 6c 64 20 70  .;.; We should p
2d40: 6f 69 6e 74 20 6f 75 74 20 74 68 61 74 20 2a 73  oint out that *s
2d50: 65 63 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 73 20  ection elements 
2d60: 61 72 65 20 70 72 6f 63 65 73 73 65 64 20 74 77  are processed tw
2d70: 69 63 65 2c 20 77 69 74 68 0a 3b 20 74 77 6f 20  ice, with.; two 
2d80: 5f 64 69 66 66 65 72 65 6e 74 5f 20 73 74 79 6c  _different_ styl
2d90: 65 73 68 65 65 74 73 2e 20 46 69 72 73 74 20 77  esheets. First w
2da0: 65 20 73 63 61 6e 20 2a 73 65 63 74 69 6f 6e 73  e scan *sections
2db0: 20 61 6e 64 20 74 75 72 6e 20 74 68 65 6d 0a 3b   and turn them.;
2dc0: 20 69 6e 74 6f 20 54 4f 43 20 65 6e 74 72 69 65   into TOC entrie
2dd0: 73 2e 20 4c 61 74 65 72 20 77 65 20 74 75 72 6e  s. Later we turn
2de0: 20 74 68 65 20 73 61 6d 65 20 2a 73 65 63 74 69   the same *secti
2df0: 6f 6e 73 20 69 6e 74 6f 20 48 54 4d 4c 0a 3b 20  ons into HTML.; 
2e00: 68 65 61 64 65 72 73 2e 0a 3b 0a 3b 20 54 68 65  headers..;.; The
2e10: 20 65 6c 65 6d 65 6e 74 73 20 27 73 65 63 74 69   elements 'secti
2e20: 6f 6e 27 20 61 6e 64 20 27 62 6f 64 79 27 20 6f  on' and 'body' o
2e30: 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 6f 63  f the source doc
2e40: 75 6d 65 6e 74 20 61 63 74 20 61 73 0a 3b 20 68  ument act as.; h
2e50: 69 67 68 65 72 2d 6c 65 76 65 6c 20 53 58 4d 4c  igher-level SXML
2e60: 20 65 6c 65 6d 65 6e 74 73 2e 20 54 68 65 79 20   elements. They 
2e70: 61 72 65 20 72 65 63 75 72 73 69 76 65 6c 79 20  are recursively 
2e80: 72 65 2d 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a  re-written into.
2e90: 3b 20 6d 6f 72 65 20 70 72 69 6d 69 74 69 76 65  ; more primitive
2ea0: 20 53 58 4d 4c 20 65 6c 65 6d 65 6e 74 73 20 75   SXML elements u
2eb0: 6e 74 69 6c 20 74 68 65 79 20 61 72 65 20 66 69  ntil they are fi
2ec0: 6e 61 6c 6c 79 20 74 75 72 6e 65 64 20 69 6e 74  nally turned int
2ed0: 6f 20 48 54 4d 4c 0a 3b 20 74 65 78 74 20 66 72  o HTML.; text fr
2ee0: 61 67 6d 65 6e 74 73 2e 20 45 73 73 65 6e 74 69  agments. Essenti
2ef0: 61 6c 6c 79 2c 20 77 65 20 63 6f 6d 70 75 74 65  ally, we compute
2f00: 20 74 68 65 20 66 69 78 70 6f 69 6e 74 20 6f 66   the fixpoint of
2f10: 20 74 68 65 0a 3b 20 72 65 2d 77 72 69 74 69 6e   the.; re-writin
2f20: 67 20 73 74 79 6c 65 73 68 65 65 74 2e 20 57 65  g stylesheet. We
2f30: 20 64 6f 20 6e 6f 74 20 69 74 65 72 61 74 65 20   do not iterate 
2f40: 6f 6e 20 74 68 65 20 77 68 6f 6c 65 20 64 6f 63  on the whole doc
2f50: 75 6d 65 6e 74 0a 3b 20 68 6f 77 65 76 65 72 2c  ument.; however,
2f60: 20 6f 6e 6c 79 20 6f 6e 20 74 68 65 20 62 72 61   only on the bra
2f70: 6e 63 68 65 73 20 74 68 61 74 20 6e 65 65 64 20  nches that need 
2f80: 69 74 65 72 61 74 69 6e 67 2e 20 54 68 65 20 77  iterating. The w
2f90: 68 6f 6c 65 0a 3b 20 61 70 70 72 6f 61 63 68 20  hole.; approach 
2fa0: 69 73 20 72 61 74 68 65 72 20 73 69 6d 69 6c 61  is rather simila
2fb0: 72 20 74 6f 20 74 68 61 74 20 6f 66 20 53 63 68  r to that of Sch
2fc0: 65 6d 65 20 6d 61 63 72 6f 73 2e 20 20 53 63 68  eme macros.  Sch
2fd0: 65 6d 65 20 6d 61 63 72 6f 73 0a 3b 20 64 6f 20  eme macros.; do 
2fe0: 6e 6f 74 20 68 61 76 65 20 27 2a 64 65 66 61 75  not have '*defau
2ff0: 6c 74 2a 27 20 72 75 6c 65 73 20 68 6f 77 65 76  lt*' rules howev
3000: 65 72 2e 20 52 35 52 53 20 6d 61 63 72 6f 73 20  er. R5RS macros 
3010: 63 61 6e 6e 6f 74 20 74 72 61 6e 73 66 6f 72 6d  cannot transform
3020: 0a 3b 20 69 6e 20 70 6f 73 74 2d 6f 72 64 65 72  .; in post-order
3030: 20 61 6e 64 20 63 61 6e 6e 6f 74 20 65 78 70 6c   and cannot expl
3040: 69 63 69 74 6c 79 20 72 65 2d 69 6e 76 6f 6b 65  icitly re-invoke
3050: 20 74 68 65 20 6d 61 63 72 6f 2d 65 78 70 61 6e   the macro-expan
3060: 64 65 72 2e 0a 0a 3b 20 4e 6f 74 65 20 5b 35 5d  der...; Note [5]
3070: 0a 3b 20 57 65 20 68 61 76 65 20 6f 6e 65 20 6d  .; We have one m
3080: 6f 72 65 20 74 68 69 6e 67 20 74 6f 20 74 61 6b  ore thing to tak
3090: 65 20 63 61 72 65 20 6f 66 2e 20 54 68 65 20 73  e care of. The s
30a0: 6f 75 72 63 65 20 64 6f 63 75 6d 65 6e 74 20 68  ource document h
30b0: 61 64 20 61 6e 0a 3b 20 61 6e 63 68 6f 72 20 65  ad an.; anchor e
30c0: 6c 65 6d 65 6e 74 20 27 28 61 20 28 40 20 28 68  lement '(a (@ (h
30d0: 72 65 66 20 22 61 6e 6f 74 68 65 72 20 64 6f 63  ref "another doc
30e0: 2e 68 74 6d 6c 22 29 29 20 22 6c 69 6e 6b 22 29  .html")) "link")
30f0: 27 20 77 69 74 68 20 74 68 65 0a 3b 20 6e 61 6d  ' with the.; nam
3100: 65 20 6f 66 20 61 20 6c 6f 63 61 6c 20 66 69 6c  e of a local fil
3110: 65 20 69 6e 20 74 68 65 20 27 68 72 65 66 27 20  e in the 'href' 
3120: 61 74 74 72 69 62 75 74 65 2e 20 49 6e 20 74 68  attribute. In th
3130: 65 20 6f 75 74 70 75 74 20 48 54 4d 4c 0a 3b 20  e output HTML.; 
3140: 64 6f 63 75 6d 65 6e 74 2c 20 74 68 61 74 20 6e  document, that n
3150: 61 6d 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  ame will become 
3160: 61 20 55 52 4c 2e 20 46 69 6c 65 20 6e 61 6d 65  a URL. File name
3170: 73 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 70  s may contain sp
3180: 61 63 65 73 0a 3b 20 2d 2d 20 62 75 74 20 55 52  aces.; -- but UR
3190: 4c 73 20 6d 61 79 20 6e 6f 74 2e 20 54 68 65 72  Ls may not. Ther
31a0: 65 66 6f 72 65 2c 20 77 65 20 6e 65 65 64 20 74  efore, we need t
31b0: 6f 20 65 6e 63 6f 64 65 20 74 68 65 20 73 70 61  o encode the spa
31c0: 63 65 0a 3b 20 63 68 61 72 61 63 74 65 72 2e 20  ce.; character. 
31d0: 57 65 20 73 68 6f 75 6c 64 20 55 52 4c 2d 65 6e  We should URL-en
31e0: 63 6f 64 65 20 74 68 65 20 73 70 61 63 65 20 63  code the space c
31f0: 68 61 72 61 63 74 65 72 20 6f 6e 6c 79 20 69 6e  haracter only in
3200: 20 74 68 65 0a 3b 20 63 6f 6e 74 65 78 74 20 6f   the.; context o
3210: 66 20 74 68 65 20 27 68 72 65 66 27 20 6e 6f 64  f the 'href' nod
3220: 65 2c 20 61 6e 64 20 6e 6f 77 68 65 72 65 20 65  e, and nowhere e
3230: 6c 73 65 2e 20 54 68 65 20 77 68 69 74 65 20 73  lse. The white s
3240: 70 61 63 65 0a 3b 20 65 6c 73 65 77 68 65 72 65  pace.; elsewhere
3250: 20 69 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   in the document
3260: 20 6d 75 73 74 20 72 65 6d 61 69 6e 20 74 68 65   must remain the
3270: 20 77 68 69 74 65 20 73 70 61 63 65 2e 20 20 48   white space.  H
3280: 65 6e 63 65 20 77 65 0a 3b 20 6e 65 65 64 20 61  ence we.; need a
3290: 20 73 70 65 63 69 61 6c 20 72 75 6c 65 20 66 6f   special rule fo
32a0: 72 20 27 68 72 65 66 27 2c 20 77 69 74 68 20 69  r 'href', with i
32b0: 74 73 20 6f 77 6e 20 68 61 6e 64 6c 65 72 20 66  ts own handler f
32c0: 6f 72 20 63 68 61 72 61 63 74 65 72 0a 3b 20 64  or character.; d
32d0: 61 74 61 2e 20 54 68 69 73 20 2a 74 65 78 74 2a  ata. This *text*
32e0: 20 68 61 6e 64 6c 65 72 20 69 73 20 5f 6c 6f 63   handler is _loc
32f0: 61 6c 5f 3a 20 69 74 20 61 63 74 73 20 6f 6e 6c  al_: it acts onl
3300: 79 20 69 6e 20 73 63 6f 70 65 20 6f 66 20 74 68  y in scope of th
3310: 65 0a 3b 20 27 68 72 65 66 27 20 6e 6f 64 65 2e  e.; 'href' node.
3320: 20 54 68 65 20 6c 6f 63 61 6c 20 74 65 78 74 20   The local text 
3330: 68 61 6e 64 6c 65 72 20 6c 6f 6f 6b 73 20 66 6f  handler looks fo
3340: 72 20 74 68 65 20 73 70 61 63 65 20 63 68 61 72  r the space char
3350: 61 63 74 65 72 0a 3b 20 61 6e 64 20 55 52 4c 2d  acter.; and URL-
3360: 65 6e 63 6f 64 65 73 20 69 74 2e 20 57 65 20 68  encodes it. We h
3370: 61 76 65 20 6a 75 73 74 20 73 68 6f 77 6e 20 61  ave just shown a
3380: 20 63 6f 6e 74 65 78 74 2d 73 65 6e 73 69 74 69   context-sensiti
3390: 76 65 0a 3b 20 61 70 70 6c 69 63 61 74 69 6f 6e  ve.; application
33a0: 20 6f 66 20 72 65 2d 77 72 69 74 69 6e 67 20 72   of re-writing r
33b0: 75 6c 65 73 2e 20 49 74 20 73 74 69 6c 6c 20 61  ules. It still a
33c0: 70 70 65 61 72 73 20 63 6c 65 61 72 20 61 6e 64  ppears clear and
33d0: 0a 3b 20 69 6e 74 75 69 74 69 76 65 2e 0a 3b 0a  .; intuitive..;.
33e0: 3b 20 4e 6f 74 65 20 5b 36 5d 0a 3b 20 4a 6f 65  ; Note [6].; Joe
33f0: 72 67 2d 43 79 72 69 6c 20 48 6f 65 68 6c 65 20  rg-Cyril Hoehle 
3400: 6f 62 73 65 72 76 65 64 20 74 68 61 74 20 74 68  observed that th
3410: 65 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 74 68  e handler for th
3420: 65 20 27 62 6f 64 79 27 20 63 61 6e 20 62 65 0a  e 'body' can be.
3430: 3b 20 77 72 69 74 74 65 6e 20 61 73 20 61 20 2a  ; written as a *
3440: 6d 61 63 72 6f 2a 2e 20 54 68 65 20 6d 61 63 72  macro*. The macr
3450: 6f 20 72 65 2d 77 72 69 74 65 73 20 28 62 6f 64  o re-writes (bod
3460: 79 20 73 65 63 74 69 6f 6e 73 29 20 69 6e 74 6f  y sections) into
3470: 0a 3b 20 28 2a 62 6f 64 79 20 74 6f 63 20 6e 75  .; (*body toc nu
3480: 6d 62 65 72 65 64 2d 73 65 63 74 69 6f 6e 73 29  mbered-sections)
3490: 2c 20 77 68 65 72 65 20 74 68 65 20 61 75 78 69  , where the auxi
34a0: 6c 69 61 72 79 20 53 58 4d 4c 20 65 6c 65 6d 65  liary SXML eleme
34b0: 6e 74 20 2a 62 6f 64 79 0a 3b 20 65 78 70 61 6e  nt *body.; expan
34c0: 64 73 20 69 6e 74 6f 20 61 6e 20 48 54 4d 4c 20  ds into an HTML 
34d0: 65 6c 65 6d 65 6e 74 20 27 62 6f 64 79 27 2e 20  element 'body'. 
34e0: 57 65 20 6e 65 65 64 20 74 68 69 73 20 65 78 74  We need this ext
34f0: 72 61 20 69 6e 64 69 72 65 63 74 69 6f 6e 0a 3b  ra indirection.;
3500: 20 74 6f 20 61 76 6f 69 64 20 65 6e 64 6c 65 73   to avoid endles
3510: 73 20 72 65 63 75 72 73 69 6f 6e 2e 20 48 69 73  s recursion. His
3520: 20 73 75 62 6d 69 73 73 69 6f 6e 20 66 6f 6c 6c   submission foll
3530: 6f 77 73 20 28 73 65 65 20 68 69 73 20 6d 65 73  ows (see his mes
3540: 73 61 67 65 0a 3b 20 6f 6e 20 74 68 65 20 53 53  sage.; on the SS
3550: 41 58 2d 53 58 4d 4c 20 6d 61 69 6c 69 6e 67 20  AX-SXML mailing 
3560: 6c 69 73 74 20 6f 6e 20 41 75 67 20 31 2c 20 32  list on Aug 1, 2
3570: 30 30 33 29 2e 0a 3b 20 20 20 20 20 20 28 62 6f  003)..;      (bo
3580: 64 79 20 2a 6d 61 63 72 6f 2a 0a 3b 20 20 20 20  dy *macro*.;    
3590: 20 20 20 20 2e 20 2c 28 6c 61 6d 62 64 61 20 28      . ,(lambda (
35a0: 74 61 67 20 2e 20 65 6c 65 6d 73 29 0a 3b 20 20  tag . elems).;  
35b0: 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 74 20             (let 
35c0: 28 28 6e 75 6d 62 65 72 65 64 2d 73 65 63 74 69  ((numbered-secti
35d0: 6f 6e 73 0a 3b 20 20 20 20 20 20 20 20 20 20 20  ons.;           
35e0: 20 20 20 20 20 20 20 20 20 20 28 6e 75 6d 62 65            (numbe
35f0: 72 2d 73 65 63 74 69 6f 6e 73 20 27 28 29 20 65  r-sections '() e
3600: 6c 65 6d 73 29 29 29 0a 3b 20 20 20 20 20 20 20  lems))).;       
3610: 20 20 20 20 20 20 20 20 28 64 69 73 70 6c 61 79          (display
3620: 20 6e 75 6d 62 65 72 65 64 2d 73 65 63 74 69 6f   numbered-sectio
3630: 6e 73 29 0a 3b 20 20 20 20 20 20 20 20 20 20 20  ns).;           
3640: 20 20 20 20 28 6c 65 74 2a 0a 3b 20 20 20 20 20      (let*.;     
3650: 20 20 20 20 20 20 20 20 20 20 20 20 28 28 74 6f              ((to
3660: 63 0a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c.;             
3670: 20 20 20 20 20 20 20 28 6d 61 6b 65 2d 74 6f 63         (make-toc
3680: 2d 65 6e 74 72 69 65 73 20 6e 75 6d 62 65 72 65  -entries numbere
3690: 64 2d 73 65 63 74 69 6f 6e 73 29 29 29 0a 3b 20  d-sections))).; 
36a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b0: 3b 20 4e 6f 77 20 74 68 65 20 62 6f 64 79 20 63  ; Now the body c
36c0: 6f 6e 74 61 69 6e 20 74 68 65 20 54 4f 43 20 65  ontain the TOC e
36d0: 6e 74 72 69 65 73 20 61 6e 64 20 74 68 65 0a 3b  ntries and the.;
36e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f0: 20 3b 20 6e 75 6d 62 65 72 65 64 20 73 65 63 74   ; numbered sect
3700: 69 6f 6e 73 2e 20 53 65 65 20 4e 6f 74 65 20 5b  ions. See Note [
3710: 33 5d 20 61 62 6f 76 65 0a 3b 20 20 20 20 20 20  3] above.;      
3720: 20 20 20 20 20 20 20 20 20 20 20 60 28 2a 62 6f             `(*bo
3730: 64 79 20 28 75 6c 20 2c 74 6f 63 29 20 2c 6e 75  dy (ul ,toc) ,nu
3740: 6d 62 65 72 65 64 2d 73 65 63 74 69 6f 6e 73 29  mbered-sections)
3750: 29 29 20 29 29 0a 3b 20 20 20 20 20 20 28 2a 62  )) )).;      (*b
3760: 6f 64 79 0a 3b 20 20 20 20 20 20 20 20 3b 20 70  ody.;        ; p
3770: 72 65 76 65 6e 74 20 65 6e 64 6c 65 73 73 20 72  revent endless r
3780: 65 63 75 72 73 69 6f 6e 20 6f 6e 63 65 20 54 4f  ecursion once TO
3790: 43 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 0a  C was generated.
37a0: 3b 20 20 20 20 20 20 20 20 2e 20 2c 28 6c 61 6d  ;        . ,(lam
37b0: 62 64 61 20 28 74 61 67 20 2e 20 65 6c 65 6d 73  bda (tag . elems
37c0: 29 20 28 65 6e 74 61 67 20 27 62 6f 64 79 20 65  ) (entag 'body e
37d0: 6c 65 6d 73 29 29 29 0a 0a 28 64 65 66 69 6e 65  lems)))..(define
37e0: 20 6d 61 69 6e 2d 73 73 20 0a 20 20 60 28 0a 20   main-ss .  `(. 
37f0: 20 20 20 20 3b 20 73 65 65 20 4e 6f 74 65 73 20      ; see Notes 
3800: 5b 32 2c 36 5d 0a 20 20 20 20 20 28 62 6f 64 79  [2,6].     (body
3810: 20 2a 70 72 65 6f 72 64 65 72 2a 0a 20 20 20 20   *preorder*.    
3820: 20 20 20 2e 20 2c 28 6c 61 6d 62 64 61 20 28 74     . ,(lambda (t
3830: 61 67 20 2e 20 65 6c 65 6d 73 29 0a 09 20 20 20  ag . elems)..   
3840: 20 28 6c 65 74 20 28 28 6e 75 6d 62 65 72 65 64   (let ((numbered
3850: 2d 73 65 63 74 69 6f 6e 73 0a 09 09 20 20 20 20  -sections...    
3860: 28 6e 75 6d 62 65 72 2d 73 65 63 74 69 6f 6e 73  (number-sections
3870: 20 27 28 29 20 65 6c 65 6d 73 29 29 29 0a 09 20   '() elems))).. 
3880: 20 20 20 20 20 3b 28 70 70 20 6e 75 6d 62 65 72       ;(pp number
3890: 65 64 2d 73 65 63 74 69 6f 6e 73 29 0a 09 20 20  ed-sections)..  
38a0: 20 20 20 20 28 6c 65 74 2a 0a 09 09 28 28 74 6f      (let*...((to
38b0: 63 0a 09 09 20 20 20 28 6d 61 6b 65 2d 74 6f 63  c...   (make-toc
38c0: 2d 65 6e 74 72 69 65 73 20 6e 75 6d 62 65 72 65  -entries numbere
38d0: 64 2d 73 65 63 74 69 6f 6e 73 29 29 29 0a 09 09  d-sections)))...
38e0: 3b 20 72 65 2d 61 70 70 6c 79 20 74 68 65 20 6d  ; re-apply the m
38f0: 61 69 6e 2d 73 73 2e 0a 09 09 3b 20 4e 6f 77 20  ain-ss....; Now 
3900: 74 68 65 20 62 6f 64 79 20 63 6f 6e 74 61 69 6e  the body contain
3910: 20 74 68 65 20 54 4f 43 20 65 6e 74 72 69 65 73   the TOC entries
3920: 20 61 6e 64 20 74 68 65 0a 09 09 3b 20 6e 75 6d   and the...; num
3930: 62 65 72 65 64 20 73 65 63 74 69 6f 6e 73 2e 20  bered sections. 
3940: 53 65 65 20 4e 6f 74 65 20 5b 33 5d 20 61 62 6f  See Note [3] abo
3950: 76 65 0a 09 09 28 70 72 65 2d 70 6f 73 74 2d 6f  ve...(pre-post-o
3960: 72 64 65 72 0a 09 09 20 20 60 28 62 6f 64 79 20  rder...  `(body 
3970: 28 75 6c 20 2c 74 6f 63 29 20 2c 6e 75 6d 62 65  (ul ,toc) ,numbe
3980: 72 65 64 2d 73 65 63 74 69 6f 6e 73 29 0a 09 09  red-sections)...
3990: 20 20 3b 20 6e 6f 77 20 70 72 6f 63 65 73 73 20    ; now process 
39a0: 27 62 6f 64 79 27 20 77 69 74 68 6f 75 74 20 61  'body' without a
39b0: 6e 20 65 78 63 65 70 74 69 6f 6e 0a 09 09 20 20  n exception...  
39c0: 3b 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  ; an example of 
39d0: 61 20 64 79 6e 61 6d 69 63 20 22 61 6d 65 6e 64  a dynamic "amend
39e0: 69 6e 67 22 20 61 20 73 74 79 6c 65 73 68 65 65  ing" a styleshee
39f0: 74 0a 09 09 20 20 28 61 70 70 65 6e 64 0a 09 09  t...  (append...
3a00: 20 20 20 20 60 28 28 62 6f 64 79 20 2e 20 2c 28      `((body . ,(
3a10: 63 64 72 20 28 61 73 73 71 20 27 2a 64 65 66 61  cdr (assq '*defa
3a20: 75 6c 74 2a 20 6d 61 69 6e 2d 73 73 29 29 29 29  ult* main-ss))))
3a30: 0a 09 09 20 20 20 20 20 20 20 6d 61 69 6e 2d 73  ...       main-s
3a40: 73 29 29 29 0a 09 20 20 20 20 20 20 29 29 29 0a  s)))..      ))).
3a50: 20 20 20 20 20 3b 20 73 65 65 20 4e 6f 74 65 20       ; see Note 
3a60: 5b 34 5d 0a 20 20 20 20 20 28 2a 73 65 63 74 69  [4].     (*secti
3a70: 6f 6e 20 2a 70 72 65 6f 72 64 65 72 2a 0a 20 20  on *preorder*.  
3a80: 20 20 20 20 20 2e 20 2c 28 6c 61 6d 62 64 61 20       . ,(lambda 
3a90: 28 74 61 67 20 6e 75 6d 62 65 72 69 6e 67 20 74  (tag numbering t
3aa0: 69 74 6c 65 20 2e 20 65 6c 65 6d 73 29 0a 09 20  itle . elems).. 
3ab0: 20 20 20 28 6c 65 74 20 28 28 68 65 61 64 65 72     (let ((header
3ac0: 2d 74 61 67 0a 09 09 20 20 20 20 28 6c 69 73 74  -tag...    (list
3ad0: 2d 72 65 66 20 27 28 68 31 20 68 32 20 68 33 20  -ref '(h1 h2 h3 
3ae0: 68 34 20 68 35 20 68 36 29 0a 09 09 20 20 20 20  h4 h5 h6)...    
3af0: 20 20 28 6c 65 6e 67 74 68 20 6e 75 6d 62 65 72    (length number
3b00: 69 6e 67 29 29 29 29 0a 09 20 20 20 20 20 20 28  ing))))..      (
3b10: 70 72 65 2d 70 6f 73 74 2d 6f 72 64 65 72 0a 09  pre-post-order..
3b20: 09 60 28 28 2c 68 65 61 64 65 72 2d 74 61 67 20  .`((,header-tag 
3b30: 2c 28 6e 75 6d 62 65 72 73 2d 3e 73 74 72 69 6e  ,(numbers->strin
3b40: 67 20 6e 75 6d 62 65 72 69 6e 67 29 20 22 2e 20  g numbering) ". 
3b50: 22 20 2c 74 69 74 6c 65 29 0a 09 09 20 20 2c 40  " ,title)...  ,@
3b60: 20 65 6c 65 6d 73 29 0a 09 09 6d 61 69 6e 2d 73   elems)...main-s
3b70: 73 29 29 29 29 0a 20 20 20 20 20 3b 20 73 65 65  s)))).     ; see
3b80: 20 4e 6f 74 65 20 5b 35 5d 0a 20 20 20 20 20 28   Note [5].     (
3b90: 68 72 65 66 0a 20 20 20 20 20 20 20 28 28 2a 74  href.       ((*t
3ba0: 65 78 74 2a 20 2e 20 2c 28 6c 61 6d 62 64 61 20  ext* . ,(lambda 
3bb0: 28 74 61 67 20 73 74 72 29 0a 09 09 20 20 20 20  (tag str)...    
3bc0: 20 28 69 66 20 28 73 74 72 69 6e 67 3f 20 73 74   (if (string? st
3bd0: 72 29 0a 09 09 20 20 20 20 20 20 20 28 28 6d 61  r)...       ((ma
3be0: 6b 65 2d 63 68 61 72 2d 71 75 6f 74 61 74 6f 72  ke-char-quotator
3bf0: 20 27 28 28 23 5c 73 70 61 63 65 20 2e 20 22 25   '((#\space . "%
3c00: 32 30 22 29 29 29 20 73 74 72 29 20 73 74 72 29  20"))) str) str)
3c10: 29 29 29 0a 20 20 20 20 20 20 20 2e 20 2c 28 6c  ))).       . ,(l
3c20: 61 6d 62 64 61 20 28 61 74 74 72 2d 6b 65 79 20  ambda (attr-key 
3c30: 2e 20 76 61 6c 75 65 29 20 28 65 6e 61 74 74 72  . value) (enattr
3c40: 20 61 74 74 72 2d 6b 65 79 20 76 61 6c 75 65 29   attr-key value)
3c50: 29 29 0a 20 20 20 20 20 3b 20 73 65 65 20 4e 6f  )).     ; see No
3c60: 74 65 20 5b 31 5d 0a 20 20 20 20 20 2c 40 75 6e  te [1].     ,@un
3c70: 69 76 65 72 73 61 6c 2d 63 6f 6e 76 65 72 73 69  iversal-conversi
3c80: 6f 6e 2d 72 75 6c 65 73 29 29 0a 0a 0a 0a 3b 20  on-rules))....; 
3c90: 54 68 65 20 6d 61 69 6e 20 66 75 6e 63 74 69 6f  The main functio
3ca0: 6e 0a 3b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  n.; The followin
3cb0: 67 20 65 78 70 72 65 73 73 69 6f 6e 20 65 78 65  g expression exe
3cc0: 63 75 74 65 73 20 74 68 65 20 74 72 61 6e 73 66  cutes the transf
3cd0: 6f 72 6d 61 74 69 6f 6e 20 6f 66 20 27 64 6f 63  ormation of 'doc
3ce0: 27 20 69 6e 74 6f 0a 3b 20 61 20 74 61 72 67 65  ' into.; a targe
3cf0: 74 20 53 58 4d 4c 20 74 72 65 65 3a 20 61 20 74  t SXML tree: a t
3d00: 72 65 65 20 6f 66 20 48 54 4d 4c 20 66 72 61 67  ree of HTML frag
3d10: 6d 65 6e 74 73 2e 20 54 68 65 20 65 78 70 72 65  ments. The expre
3d20: 73 73 69 6f 6e 20 74 68 65 6e 0a 3b 20 77 72 69  ssion then.; wri
3d30: 74 65 73 20 6f 75 74 20 74 68 61 74 20 74 72 65  tes out that tre
3d40: 65 20 6f 6e 20 74 68 65 20 73 74 61 6e 64 61 72  e on the standar
3d50: 64 20 6f 75 74 70 75 74 2e 20 59 6f 75 20 6d 61  d output. You ma
3d60: 79 20 77 61 6e 74 20 74 6f 20 73 61 76 65 0a 3b  y want to save.;
3d70: 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 61   the result in a
3d80: 20 66 69 6c 65 20 61 6e 64 20 6c 6f 61 64 20 74   file and load t
3d90: 68 65 20 66 69 6c 65 20 69 6e 20 61 20 77 65 62  he file in a web
3da0: 20 62 72 6f 77 73 65 72 2e 0a 0a 28 53 52 56 3a   browser...(SRV:
3db0: 73 65 6e 64 2d 72 65 70 6c 79 20 28 70 72 65 2d  send-reply (pre-
3dc0: 70 6f 73 74 2d 6f 72 64 65 72 20 64 6f 63 20 6d  post-order doc m
3dd0: 61 69 6e 2d 73 73 29 29 0a                       ain-ss)).