Hex Artifact Content
Not logged in

Artifact 543034b283979359c5acb66910eedc406309b385:


0000: 5f 73 78 6d 6c 2d 6d 61 74 63 68 5f 3a 20 50 61  _sxml-match_: Pa
0010: 74 74 65 72 6e 20 4d 61 74 63 68 69 6e 67 20 6f  ttern Matching o
0020: 66 20 53 58 4d 4c 0a 3d 3d 3d 3d 3d 3d 3d 3d 3d  f SXML.=========
0030: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
0040: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 0a 0a 41  =============..A
0050: 75 74 68 6f 72 3a 20 4a 69 6d 20 42 65 6e 64 65  uthor: Jim Bende
0060: 72 0a 4c 61 73 74 20 55 70 64 61 74 65 64 3a 20  r.Last Updated: 
0070: 31 35 20 4d 61 72 63 68 20 32 30 30 35 0a 0a 4c  15 March 2005..L
0080: 69 62 72 61 72 79 3a 20 73 78 6d 6c 2d 6d 61 74  ibrary: sxml-mat
0090: 63 68 0a 56 65 72 73 69 6f 6e 3a 20 31 2e 30 61  ch.Version: 1.0a
00a0: 2c 20 62 75 69 6c 64 20 32 30 30 35 30 33 31 35  , build 20050315
00b0: 0a 0a 54 68 69 73 20 53 63 68 65 6d 65 20 6c 69  ..This Scheme li
00c0: 62 72 61 72 79 20 70 72 6f 76 69 64 65 73 20 73  brary provides s
00d0: 79 6e 74 61 63 74 69 63 20 66 6f 72 6d 73 20 66  yntactic forms f
00e0: 6f 72 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68  or pattern match
00f0: 69 6e 67 20 6f 66 20 53 58 4d 4c 2c 20 69 6e 20  ing of SXML, in 
0100: 61 20 22 62 79 20 65 78 61 6d 70 6c 65 22 20 73  a "by example" s
0110: 74 79 6c 65 20 72 65 6d 69 6e 69 73 63 65 6e 74  tyle reminiscent
0120: 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20   of the pattern 
0130: 6d 61 74 63 68 69 6e 67 20 6f 66 20 74 68 65 20  matching of the 
0140: 73 79 6e 74 61 78 2d 72 75 6c 65 73 20 61 6e 64  syntax-rules and
0150: 20 73 79 6e 74 61 78 2d 63 61 73 65 20 6d 61 63   syntax-case mac
0160: 72 6f 20 73 79 73 74 65 6d 73 2e 20 54 68 65 20  ro systems. The 
0170: 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 61 6d 70 6c  following exampl
0180: 65 20 5b 6e 6f 74 65 20 31 5d 20 70 72 6f 76 69  e [note 1] provi
0190: 64 65 73 20 61 20 62 72 69 65 66 20 69 6c 6c 75  des a brief illu
01a0: 73 74 72 61 74 69 6f 6e 2c 20 74 72 61 6e 73 66  stration, transf
01b0: 6f 72 6d 69 6e 67 20 61 6e 20 6d 75 73 69 63 20  orming an music 
01c0: 61 6c 62 75 6d 20 63 61 74 61 6c 6f 67 20 6c 61  album catalog la
01d0: 6e 67 75 61 67 65 20 69 6e 74 6f 20 48 54 4d 4c  nguage into HTML
01e0: 2e 0a 28 64 65 66 69 6e 65 20 28 61 6c 62 75 6d  ..(define (album
01f0: 2d 3e 68 74 6d 6c 20 78 29 0a 20 20 28 73 78 6d  ->html x).  (sxm
0200: 6c 2d 6d 61 74 63 68 20 78 0a 20 20 20 20 5b 28  l-match x.    [(
0210: 61 6c 62 75 6d 20 28 40 20 28 74 69 74 6c 65 20  album (@ (title 
0220: 2c 74 29 29 20 28 63 61 74 61 6c 6f 67 20 28 6e  ,t)) (catalog (n
0230: 75 6d 20 2c 6e 29 20 28 66 6d 74 20 2c 66 29 29  um ,n) (fmt ,f))
0240: 20 2e 2e 2e 29 0a 20 20 20 20 20 60 28 75 6c 20   ...).     `(ul 
0250: 28 6c 69 20 2c 74 29 0a 20 20 20 20 20 20 20 20  (li ,t).        
0260: 20 20 28 6c 69 20 28 62 20 2c 6e 29 20 28 69 20    (li (b ,n) (i 
0270: 2c 66 29 29 20 2e 2e 2e 29 5d 29 29 0a 0a 0a 54  ,f)) ...)]))...T
0280: 68 72 65 65 20 6d 61 63 72 6f 73 20 61 72 65 20  hree macros are 
0290: 70 72 6f 76 69 64 65 64 3a 20 73 78 6d 6c 2d 6d  provided: sxml-m
02a0: 61 74 63 68 2c 20 73 78 6d 6c 2d 6d 61 74 63 68  atch, sxml-match
02b0: 2d 6c 65 74 2c 20 61 6e 64 20 73 78 6d 6c 2d 6d  -let, and sxml-m
02c0: 61 74 63 68 2d 6c 65 74 2a 2e 0a 0a 20 43 6f 6d  atch-let*... Com
02d0: 70 61 72 65 64 20 74 6f 20 61 20 73 74 61 6e 64  pared to a stand
02e0: 61 72 64 20 73 2d 65 78 70 72 65 73 73 69 6f 6e  ard s-expression
02f0: 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68 65 72   pattern matcher
0300: 2c 20 73 78 6d 6c 2d 6d 61 74 63 68 20 70 72 6f  , sxml-match pro
0310: 76 69 64 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  vides the follow
0320: 69 6e 67 20 62 65 6e 65 66 69 74 73 3a 0a 20 2d  ing benefits:. -
0330: 20 6d 61 74 63 68 69 6e 67 20 6f 66 20 53 58 4d   matching of SXM
0340: 4c 20 65 6c 65 6d 65 6e 74 73 20 64 6f 65 73 20  L elements does 
0350: 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e  not depend on an
0360: 79 20 64 65 67 72 65 65 20 6f 66 20 6e 6f 72 6d  y degree of norm
0370: 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65  alization of the
0380: 20 53 58 4d 4c 0a 20 2d 20 6d 61 74 63 68 69 6e   SXML. - matchin
0390: 67 20 6f 66 20 53 58 4d 4c 20 61 74 74 72 69 62  g of SXML attrib
03a0: 75 74 65 73 20 28 77 69 74 68 69 6e 20 61 6e 20  utes (within an 
03b0: 65 6c 65 6d 65 6e 74 29 20 69 73 20 75 6e 64 65  element) is unde
03c0: 72 2d 6f 72 64 65 72 65 64 3b 20 74 68 65 20 6f  r-ordered; the o
03d0: 72 64 65 72 20 6f 66 20 74 68 65 20 61 74 74 72  rder of the attr
03e0: 69 62 75 74 65 73 20 73 70 65 63 69 66 69 65 64  ibutes specified
03f0: 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 74 74   within the patt
0400: 65 72 6e 20 6e 65 65 64 20 6e 6f 74 20 6d 61 74  ern need not mat
0410: 63 68 20 74 68 65 20 6f 72 64 65 72 69 6e 67 20  ch the ordering 
0420: 77 69 74 68 20 74 68 65 20 65 6c 65 6d 65 6e 74  with the element
0430: 20 62 65 69 6e 67 20 6d 61 74 63 68 65 64 0a 20   being matched. 
0440: 2d 20 61 6c 6c 20 61 74 74 72 69 62 75 74 65 73  - all attributes
0450: 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
0460: 65 20 70 61 74 74 65 72 6e 20 6d 75 73 74 20 62  e pattern must b
0470: 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65  e present in the
0480: 20 65 6c 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d   element being m
0490: 61 74 63 68 65 64 3b 20 69 6e 20 74 68 65 20 73  atched; in the s
04a0: 70 69 72 69 74 20 74 68 61 74 20 58 4d 4c 20 69  pirit that XML i
04b0: 73 20 27 65 78 74 65 6e 73 69 62 6c 65 27 2c 20  s 'extensible', 
04c0: 74 68 65 20 65 6c 65 6d 65 6e 74 20 62 65 69 6e  the element bein
04d0: 67 20 6d 61 74 63 68 65 64 20 6d 61 79 20 69 6e  g matched may in
04e0: 63 6c 75 64 65 20 61 64 64 69 74 69 6f 6e 61 6c  clude additional
04f0: 20 61 74 74 72 69 62 75 74 65 73 20 6e 6f 74 20   attributes not 
0500: 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
0510: 20 70 61 74 74 65 72 6e 2e 0a 0a 54 68 65 20 69   pattern...The i
0520: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
0530: 20 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72   supported under
0540: 20 50 4c 54 20 53 63 68 65 6d 65 2c 20 28 50 65   PLT Scheme, (Pe
0550: 74 69 74 65 29 20 43 68 65 7a 20 53 63 68 65 6d  tite) Chez Schem
0560: 65 20 61 6e 64 20 53 49 53 43 2e 20 49 74 20 69  e and SISC. It i
0570: 73 20 65 78 70 65 63 74 65 64 20 74 68 61 74 20  s expected that 
0580: 74 68 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c  the library will
0590: 20 77 6f 72 6b 20 75 6e 64 65 72 20 6f 74 68 65   work under othe
05a0: 72 20 53 63 68 65 6d 65 73 20 73 75 70 70 6f 72  r Schemes suppor
05b0: 74 69 6e 67 20 74 68 65 20 50 6f 72 74 61 62 6c  ting the Portabl
05c0: 65 20 53 79 6e 74 61 78 2d 63 61 73 65 20 69 6d  e Syntax-case im
05d0: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 0a 54  plementation...T
05e0: 6f 20 75 73 65 20 74 68 65 20 6c 69 62 72 61 72  o use the librar
05f0: 79 20 75 6e 64 65 72 20 50 4c 54 20 53 63 68 65  y under PLT Sche
0600: 6d 65 2c 20 69 6e 63 6c 75 64 65 20 74 68 65 20  me, include the 
0610: 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 3a 0a  following code:.
0620: 28 72 65 71 75 69 72 65 20 28 6c 69 62 20 22 73  (require (lib "s
0630: 78 6d 6c 2d 6d 61 74 63 68 2e 73 73 22 20 22 73  xml-match.ss" "s
0640: 78 6d 6c 2d 6d 61 74 63 68 22 29 29 0a 0a 54 6f  xml-match"))..To
0650: 20 75 73 65 20 74 68 65 20 6c 69 62 72 61 72 79   use the library
0660: 20 75 6e 64 65 72 20 28 50 65 74 69 74 65 29 20   under (Petite) 
0670: 43 68 65 7a 20 53 63 68 65 6d 65 20 61 6e 64 20  Chez Scheme and 
0680: 53 49 53 43 2c 20 69 6e 63 6c 75 64 65 20 74 68  SISC, include th
0690: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
06a0: 3a 0a 28 6c 6f 61 64 20 22 73 78 6d 6c 2d 6d 61  :.(load "sxml-ma
06b0: 74 63 68 2e 73 73 22 29 0a 28 69 6d 70 6f 72 74  tch.ss").(import
06c0: 20 73 78 6d 6c 2d 6d 61 74 63 68 65 72 29 0a 0a   sxml-matcher)..
06d0: 54 68 65 20 70 72 65 73 65 6e 74 20 6c 69 62 72  The present libr
06e0: 61 72 79 20 69 73 20 61 20 64 65 73 63 65 6e 64  ary is a descend
06f0: 61 6e 74 20 6f 66 20 57 65 62 49 74 21 2c 20 61  ant of WebIt!, a
0700: 6e 64 20 77 61 73 20 69 6e 73 70 69 72 65 64 20  nd was inspired 
0710: 62 79 20 61 6e 20 73 2d 65 78 70 72 65 73 73 69  by an s-expressi
0720: 6f 6e 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68  on pattern match
0730: 65 72 20 5b 6e 6f 74 65 20 32 5d 20 64 65 76 65  er [note 2] deve
0740: 6c 6f 70 65 64 20 62 79 20 45 72 69 6b 20 48 69  loped by Erik Hi
0750: 6c 73 64 61 6c 65 2c 20 44 61 6e 20 46 72 69 65  lsdale, Dan Frie
0760: 64 6d 61 6e 2c 20 61 6e 64 20 4b 65 6e 74 20 44  dman, and Kent D
0770: 79 62 76 69 67 20 61 74 20 49 6e 64 69 61 6e 61  ybvig at Indiana
0780: 20 55 6e 69 76 65 72 73 69 74 79 2e 0a 0a 31 c3   University...1.
0790: 8a 5f 73 78 6d 6c 2d 6d 61 74 63 68 5f 0a 3d 3d  ._sxml-match_.==
07a0: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 0a 0a 73 79  ============..sy
07b0: 6e 74 61 78 0a 0a 73 78 6d 6c 2d 6d 61 74 63 68  ntax..sxml-match
07c0: 20 70 72 6f 76 69 64 65 73 20 63 61 73 65 2d 6c   provides case-l
07d0: 69 6b 65 20 66 6f 72 6d 20 66 6f 72 20 70 61 74  ike form for pat
07e0: 74 65 72 6e 20 6d 61 74 63 68 69 6e 67 20 6f 66  tern matching of
07f0: 20 58 4d 4c 20 6e 6f 64 65 73 2e 20 54 68 65 20   XML nodes. The 
0800: 73 78 6d 6c 2d 6d 61 74 63 68 20 66 6f 72 6d 20  sxml-match form 
0810: 69 6e 63 6c 75 64 65 73 20 6f 6e 65 20 6f 72 20  includes one or 
0820: 6d 6f 72 65 20 63 6c 61 75 73 65 73 2c 20 65 61  more clauses, ea
0830: 63 68 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f 66  ch consisting of
0840: 20 61 20 70 61 74 74 65 72 6e 20 61 6e 64 20 6f   a pattern and o
0850: 6e 65 20 6f 72 20 6d 6f 72 65 20 65 78 70 72 65  ne or more expre
0860: 73 73 69 6f 6e 73 20 74 6f 20 62 65 20 65 76 61  ssions to be eva
0870: 6c 75 61 74 65 64 20 69 66 20 74 68 65 20 70 61  luated if the pa
0880: 74 74 65 72 6e 20 6d 61 74 63 68 20 73 75 63 63  ttern match succ
0890: 65 65 64 73 2e 20 4f 70 74 69 6f 6e 61 6c 6c 79  eeds. Optionally
08a0: 2c 20 65 61 63 68 20 63 6c 61 75 73 65 20 77 69  , each clause wi
08b0: 74 68 69 6e 20 73 78 6d 6c 2d 6d 61 74 63 68 20  thin sxml-match 
08c0: 6d 61 79 20 69 6e 63 6c 75 64 65 20 61 20 67 75  may include a gu
08d0: 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  ard expression..
08e0: 0a 54 68 65 20 70 61 74 74 65 72 6e 20 6e 6f 74  .The pattern not
08f0: 61 74 69 6f 6e 20 69 73 20 62 61 73 65 64 20 6f  ation is based o
0900: 6e 20 74 68 61 74 20 6f 66 20 53 63 68 65 6d 65  n that of Scheme
0910: 27 73 20 73 79 6e 74 61 78 2d 72 75 6c 65 73 2f  's syntax-rules/
0920: 73 79 6e 74 61 78 2d 63 61 73 65 20 6d 61 63 72  syntax-case macr
0930: 6f 20 73 79 73 74 65 6d 73 2e 0a 0a 54 68 65 20  o systems...The 
0940: 67 72 61 6d 6d 61 72 20 66 6f 72 20 74 68 65 20  grammar for the 
0950: 73 78 6d 6c 2d 6d 61 74 63 68 20 73 79 6e 74 61  sxml-match synta
0960: 78 20 69 73 20 67 69 76 65 6e 20 62 65 6c 6f 77  x is given below
0970: 3a 0a 6d 61 74 63 68 2d 66 6f 72 6d 20 3a 3a 3d  :.match-form ::=
0980: 20 28 73 78 6d 6c 2d 6d 61 74 63 68 20 69 6e 70   (sxml-match inp
0990: 75 74 2d 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  ut-expression.  
09a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
09b0: 6c 61 75 73 65 2b 29 0a 0a 63 6c 61 75 73 65 20  lause+)..clause 
09c0: 3a 3a 3d 20 5b 6e 6f 64 65 2d 70 61 74 74 65 72  ::= [node-patter
09d0: 6e 20 61 63 74 69 6f 6e 2d 65 78 70 72 65 73 73  n action-express
09e0: 69 6f 6e 2b 5d 0a 20 20 20 20 20 20 20 20 20 7c  ion+].         |
09f0: 20 5b 6e 6f 64 65 2d 70 61 74 74 65 72 6e 20 28   [node-pattern (
0a00: 67 75 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e  guard expression
0a10: 2a 29 20 61 63 74 69 6f 6e 2d 65 78 70 72 65 73  *) action-expres
0a20: 73 69 6f 6e 2b 5d 0a 0a 6e 6f 64 65 2d 70 61 74  sion+]..node-pat
0a30: 74 65 72 6e 20 3a 3a 3d 20 6c 69 74 65 72 61 6c  tern ::= literal
0a40: 2d 70 61 74 74 65 72 6e 0a 20 20 20 20 20 20 20  -pattern.       
0a50: 20 20 20 20 20 20 20 20 7c 20 70 61 74 2d 76 61          | pat-va
0a60: 72 2d 6f 72 2d 63 61 74 61 0a 20 20 20 20 20 20  r-or-cata.      
0a70: 20 20 20 20 20 20 20 20 20 7c 20 65 6c 65 6d 65           | eleme
0a80: 6e 74 2d 70 61 74 74 65 72 6e 0a 20 20 20 20 20  nt-pattern.     
0a90: 20 20 20 20 20 20 20 20 20 20 7c 20 6c 69 73 74            | list
0aa0: 2d 70 61 74 74 65 72 6e 0a 0a 6c 69 74 65 72 61  -pattern..litera
0ab0: 6c 2d 70 61 74 74 65 72 6e 20 3a 3a 3d 20 73 74  l-pattern ::= st
0ac0: 72 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 20  ring.           
0ad0: 20 20 20 20 20 20 20 7c 20 63 68 61 72 61 63 74         | charact
0ae0: 65 72 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  er.             
0af0: 20 20 20 20 20 7c 20 6e 75 6d 62 65 72 0a 20 20       | number.  
0b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0b10: 7c 20 23 74 0a 20 20 20 20 20 20 20 20 20 20 20  | #t.           
0b20: 20 20 20 20 20 20 20 7c 20 23 66 0a 0a 61 74 74         | #f..att
0b30: 72 2d 6c 69 73 74 2d 70 61 74 74 65 72 6e 20 3a  r-list-pattern :
0b40: 3a 3d 20 28 40 20 61 74 74 72 69 62 75 74 65 2d  := (@ attribute-
0b50: 70 61 74 74 65 72 6e 2a 29 0a 20 20 20 20 20 20  pattern*).      
0b60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
0b70: 28 40 20 61 74 74 72 69 62 75 74 65 2d 70 61 74  (@ attribute-pat
0b80: 74 65 72 6e 2a 20 2e 20 70 61 74 2d 76 61 72 2d  tern* . pat-var-
0b90: 6f 72 2d 63 61 74 61 29 0a 0a 61 74 74 72 69 62  or-cata)..attrib
0ba0: 75 74 65 2d 70 61 74 74 65 72 6e 20 3a 3a 3d 20  ute-pattern ::= 
0bb0: 28 74 61 67 2d 73 79 6d 62 6f 6c 20 61 74 74 72  (tag-symbol attr
0bc0: 2d 76 61 6c 2d 70 61 74 74 65 72 6e 29 0a 0a 61  -val-pattern)..a
0bd0: 74 74 72 2d 76 61 6c 2d 70 61 74 74 65 72 6e 20  ttr-val-pattern 
0be0: 3a 3a 3d 20 6c 69 74 65 72 61 6c 2d 70 61 74 74  ::= literal-patt
0bf0: 65 72 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20  ern.            
0c00: 20 20 20 20 20 20 20 7c 20 70 61 74 2d 76 61 72         | pat-var
0c10: 2d 6f 72 2d 63 61 74 61 0a 20 20 20 20 20 20 20  -or-cata.       
0c20: 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 28 70              | (p
0c30: 61 74 2d 76 61 72 2d 6f 72 2d 63 61 74 61 20 64  at-var-or-cata d
0c40: 65 66 61 75 6c 74 2d 76 61 6c 75 65 2d 65 78 70  efault-value-exp
0c50: 72 29 0a 0a 65 6c 65 6d 65 6e 74 2d 70 61 74 74  r)..element-patt
0c60: 65 72 6e 20 3a 3a 3d 20 28 74 61 67 2d 73 79 6d  ern ::= (tag-sym
0c70: 62 6f 6c 20 61 74 74 72 2d 6c 69 73 74 2d 70 61  bol attr-list-pa
0c80: 74 74 65 72 6e 3f 29 0a 20 20 20 20 20 20 20 20  ttern?).        
0c90: 20 20 20 20 20 20 20 20 20 20 7c 20 28 74 61 67            | (tag
0ca0: 2d 73 79 6d 62 6f 6c 20 61 74 74 72 2d 6c 69 73  -symbol attr-lis
0cb0: 74 2d 70 61 74 74 65 72 6e 3f 20 6e 6f 64 65 73  t-pattern? nodes
0cc0: 65 74 2d 70 61 74 74 65 72 6e 29 0a 20 20 20 20  et-pattern).    
0cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
0ce0: 28 74 61 67 2d 73 79 6d 62 6f 6c 20 61 74 74 72  (tag-symbol attr
0cf0: 2d 6c 69 73 74 2d 70 61 74 74 65 72 6e 3f 0a 20  -list-pattern?. 
0d00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0d10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
0d20: 6f 64 65 73 65 74 2d 70 61 74 74 65 72 6e 3f 20  odeset-pattern? 
0d30: 2e 20 70 61 74 2d 76 61 72 2d 6f 72 2d 63 61 74  . pat-var-or-cat
0d40: 61 29 0a 0a 6c 69 73 74 2d 70 61 74 74 65 72 6e  a)..list-pattern
0d50: 20 3a 3a 3d 20 28 6c 69 73 74 20 6e 6f 64 65 73   ::= (list nodes
0d60: 65 74 2d 70 61 74 74 65 72 6e 29 0a 20 20 20 20  et-pattern).    
0d70: 20 20 20 20 20 20 20 20 20 20 20 7c 20 28 6c 69             | (li
0d80: 73 74 20 6e 6f 64 65 73 65 74 2d 70 61 74 74 65  st nodeset-patte
0d90: 72 6e 3f 20 2e 20 70 61 74 2d 76 61 72 2d 6f 72  rn? . pat-var-or
0da0: 2d 63 61 74 61 29 0a 20 20 20 20 20 20 20 20 20  -cata).         
0db0: 20 20 20 20 20 20 7c 20 28 6c 69 73 74 29 0a 0a        | (list)..
0dc0: 6e 6f 64 65 73 65 74 2d 70 61 74 74 65 72 6e 20  nodeset-pattern 
0dd0: 3a 3a 3d 20 6e 6f 64 65 2d 70 61 74 74 65 72 6e  ::= node-pattern
0de0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
0df0: 20 20 20 7c 20 6e 6f 64 65 2d 70 61 74 74 65 72     | node-patter
0e00: 6e 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 20 20  n ....          
0e10: 20 20 20 20 20 20 20 20 7c 20 6e 6f 64 65 2d 70          | node-p
0e20: 61 74 74 65 72 6e 20 6e 6f 64 65 73 65 74 2d 70  attern nodeset-p
0e30: 61 74 74 65 72 6e 0a 20 20 20 20 20 20 20 20 20  attern.         
0e40: 20 20 20 20 20 20 20 20 20 7c 20 6e 6f 64 65 2d           | node-
0e50: 70 61 74 74 65 72 6e 20 2e 2e 2e 20 6e 6f 64 65  pattern ... node
0e60: 73 65 74 2d 70 61 74 74 65 72 6e 0a 0a 70 61 74  set-pattern..pat
0e70: 2d 76 61 72 2d 6f 72 2d 63 61 74 61 20 3a 3a 3d  -var-or-cata ::=
0e80: 20 28 75 6e 71 75 6f 74 65 20 76 61 72 2d 73 79   (unquote var-sy
0e90: 6d 62 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 20  mbol).          
0ea0: 20 20 20 20 20 20 20 20 7c 20 28 75 6e 71 75 6f          | (unquo
0eb0: 74 65 20 5b 76 61 72 2d 73 79 6d 62 6f 6c 2a 5d  te [var-symbol*]
0ec0: 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
0ed0: 20 20 20 20 7c 20 28 75 6e 71 75 6f 74 65 20 5b      | (unquote [
0ee0: 63 61 74 61 2d 65 78 70 72 65 73 73 69 6f 6e 20  cata-expression 
0ef0: 2d 3e 20 76 61 72 2d 73 79 6d 62 6f 6c 2a 5d 29  -> var-symbol*])
0f00: 0a 0a 0a 57 69 74 68 69 6e 20 61 20 6c 69 73 74  ...Within a list
0f10: 20 6f 72 20 65 6c 65 6d 65 6e 74 20 62 6f 64 79   or element body
0f20: 20 70 61 74 74 65 72 6e 2c 20 65 6c 6c 69 70 73   pattern, ellips
0f30: 65 73 20 6d 61 79 20 61 70 70 65 61 72 20 6f 6e  es may appear on
0f40: 6c 79 20 6f 6e 63 65 2c 20 62 75 74 20 6d 61 79  ly once, but may
0f50: 20 62 65 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   be followed by 
0f60: 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6e 6f 64  zero or more nod
0f70: 65 20 70 61 74 74 65 72 6e 73 2e 0a 0a 47 75 61  e patterns...Gua
0f80: 72 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63  rd expressions c
0f90: 61 6e 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74  annot refer to t
0fa0: 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  he return values
0fb0: 20 6f 66 20 63 61 74 61 6d 6f 72 70 68 69 73 6d   of catamorphism
0fc0: 73 2e 0a 0a 45 6c 6c 69 70 73 65 73 20 69 6e 20  s...Ellipses in 
0fd0: 74 68 65 20 6f 75 74 70 75 74 20 65 78 70 72 65  the output expre
0fe0: 73 73 69 6f 6e 73 20 6d 75 73 74 20 61 70 70 65  ssions must appe
0ff0: 61 72 20 6f 6e 6c 79 20 69 6e 20 61 6e 20 65 78  ar only in an ex
1000: 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65 78 74  pression context
1010: 3b 20 65 6c 6c 69 70 73 65 73 20 61 72 65 20 6e  ; ellipses are n
1020: 6f 74 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 20  ot allowed in a 
1030: 73 79 6e 74 61 63 74 69 63 20 66 6f 72 6d 2e 0a  syntactic form..
1040: 0a 54 68 65 20 73 65 63 74 69 6f 6e 73 20 62 65  .The sections be
1050: 6c 6f 77 20 69 6c 6c 75 73 74 72 61 74 65 20 73  low illustrate s
1060: 70 65 63 69 66 69 63 20 61 73 70 65 63 74 73 20  pecific aspects 
1070: 6f 66 20 74 68 65 20 73 78 6d 6c 2d 6d 61 74 63  of the sxml-matc
1080: 68 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68 65  h pattern matche
1090: 72 2e 0a 0a 31 2e 31 20 4d 61 74 63 68 69 6e 67  r...1.1 Matching
10a0: 20 58 4d 4c 20 45 6c 65 6d 65 6e 74 73 0a 0a 54   XML Elements..T
10b0: 68 65 20 65 78 61 6d 70 6c 65 20 62 65 6c 6f 77  he example below
10c0: 20 69 6c 6c 75 73 74 72 61 74 65 73 20 74 68 65   illustrates the
10d0: 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68 69 6e   pattern matchin
10e0: 67 20 6f 66 20 61 6e 20 58 4d 4c 20 65 6c 65 6d  g of an XML elem
10f0: 65 6e 74 3a 0a 28 73 78 6d 6c 2d 6d 61 74 63 68  ent:.(sxml-match
1100: 20 27 28 65 20 28 40 20 28 69 20 31 29 29 20 33   '(e (@ (i 1)) 3
1110: 20 34 20 35 29 0a 20 20 5b 28 65 20 28 40 20 28   4 5).  [(e (@ (
1120: 69 20 2c 64 29 29 20 2c 61 20 2c 62 20 2c 63 29  i ,d)) ,a ,b ,c)
1130: 20 28 6c 69 73 74 20 64 20 61 20 62 20 63 29 5d   (list d a b c)]
1140: 0a 20 20 5b 2c 6f 74 68 65 72 77 69 73 65 20 23  .  [,otherwise #
1150: 66 5d 29 0a 0a 0a 45 61 63 68 20 63 6c 61 75 73  f])...Each claus
1160: 65 20 69 6e 20 73 78 6d 6c 2d 6d 61 74 63 68 20  e in sxml-match 
1170: 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 70 61 72  contains two par
1180: 74 73 3a 20 61 20 70 61 74 74 65 72 6e 20 61 6e  ts: a pattern an
1190: 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 78  d one or more ex
11a0: 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20  pressions which 
11b0: 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 69 66  are evaluated if
11c0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 73 20   the pattern is 
11d0: 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6d 61 74  successfully mat
11e0: 63 68 2e 20 54 68 65 20 65 78 61 6d 70 6c 65 20  ch. The example 
11f0: 61 62 6f 76 65 20 6d 61 74 63 68 65 73 20 61 6e  above matches an
1200: 20 65 6c 65 6d 65 6e 74 20 27 65 27 20 77 69 74   element 'e' wit
1210: 68 20 61 6e 20 61 74 74 72 69 62 75 74 65 20 27  h an attribute '
1220: 69 27 20 61 6e 64 20 74 68 72 65 65 20 63 68 69  i' and three chi
1230: 6c 64 72 65 6e 2e 0a 0a 50 61 74 74 65 72 6e 20  ldren...Pattern 
1240: 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6d 75  variables are mu
1250: 73 74 20 62 65 20 22 75 6e 71 75 6f 74 65 64 22  st be "unquoted"
1260: 20 69 6e 20 74 68 65 20 70 61 74 74 65 72 6e 2e   in the pattern.
1270: 20 54 68 65 20 61 62 6f 76 65 20 65 78 70 72 65   The above expre
1280: 73 73 69 6f 6e 20 62 69 6e 64 73 20 64 20 74 6f  ssion binds d to
1290: 20 31 2c 20 61 20 74 6f 20 33 2c 20 62 20 74 6f   1, a to 3, b to
12a0: 20 34 2c 20 61 6e 64 20 63 20 74 6f 20 35 2e 0a   4, and c to 5..
12b0: 0a 31 2e 32 20 45 6c 6c 69 70 73 65 73 20 69 6e  .1.2 Ellipses in
12c0: 20 50 61 74 74 65 72 6e 73 0a 0a 41 73 20 69 6e   Patterns..As in
12d0: 20 73 79 6e 74 61 78 2d 72 75 6c 65 73 2c 20 65   syntax-rules, e
12e0: 6c 6c 69 70 73 65 73 20 6d 61 79 20 62 65 20 75  llipses may be u
12f0: 73 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 61  sed to specify a
1300: 20 72 65 70 65 61 74 65 64 20 70 61 74 74 65 72   repeated patter
1310: 6e 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  n. Note that the
1320: 20 70 61 74 74 65 72 6e 20 22 69 74 65 6d 20 2e   pattern "item .
1330: 2e 2e 22 20 73 70 65 63 69 66 69 65 73 20 7a 65  .." specifies ze
1340: 72 6f 2d 6f 72 2d 6d 6f 72 65 20 6d 61 74 63 68  ro-or-more match
1350: 65 73 20 6f 66 20 74 68 65 20 70 61 74 74 65 72  es of the patter
1360: 6e 20 22 69 74 65 6d 22 2e 0a 0a 54 68 65 20 75  n "item"...The u
1370: 73 65 20 6f 66 20 65 6c 6c 69 70 73 65 73 20 69  se of ellipses i
1380: 6e 20 61 20 70 61 74 74 65 72 6e 20 69 73 20 69  n a pattern is i
1390: 6c 6c 75 73 74 72 61 74 65 64 20 69 6e 20 74 68  llustrated in th
13a0: 65 20 63 6f 64 65 20 66 72 61 67 6d 65 6e 74 20  e code fragment 
13b0: 62 65 6c 6f 77 2c 20 77 68 65 72 65 20 6e 65 73  below, where nes
13c0: 74 65 64 20 65 6c 6c 69 70 73 65 73 20 61 72 65  ted ellipses are
13d0: 20 75 73 65 64 20 74 6f 20 6d 61 74 63 68 20 74   used to match t
13e0: 68 65 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 72  he children of r
13f0: 65 70 65 61 74 65 64 20 69 6e 73 74 61 6e 63 65  epeated instance
1400: 73 20 6f 66 20 61 6e 20 27 61 27 20 65 6c 65 6d  s of an 'a' elem
1410: 65 6e 74 2c 20 77 69 74 68 69 6e 20 61 6e 20 65  ent, within an e
1420: 6c 65 6d 65 6e 74 20 27 64 27 2e 0a 28 64 65 66  lement 'd'..(def
1430: 69 6e 65 20 78 20 27 28 64 20 28 61 20 31 20 32  ine x '(d (a 1 2
1440: 20 33 29 20 28 61 20 34 20 35 29 20 28 61 20 36   3) (a 4 5) (a 6
1450: 20 37 20 38 29 20 28 61 20 39 20 31 30 29 29 29   7 8) (a 9 10)))
1460: 0a 0a 28 73 78 6d 6c 2d 6d 61 74 63 68 20 78 0a  ..(sxml-match x.
1470: 20 20 5b 28 64 20 28 61 20 2c 62 20 2e 2e 2e 29    [(d (a ,b ...)
1480: 20 2e 2e 2e 29 0a 20 20 20 28 6c 69 73 74 20 28   ...).   (list (
1490: 6c 69 73 74 20 62 20 2e 2e 2e 29 20 2e 2e 2e 29  list b ...) ...)
14a0: 5d 29 0a 0a 0a 54 68 65 20 61 62 6f 76 65 20 65  ])...The above e
14b0: 78 70 72 65 73 73 69 6f 6e 20 72 65 74 75 72 6e  xpression return
14c0: 73 20 61 20 76 61 6c 75 65 20 6f 66 20 28 28 31  s a value of ((1
14d0: 20 32 20 33 29 20 28 34 20 35 29 20 28 36 20 37   2 3) (4 5) (6 7
14e0: 20 38 29 20 28 39 20 31 30 29 29 2e 0a 0a 31 2e   8) (9 10))...1.
14f0: 33 20 45 6c 6c 69 70 73 65 73 20 69 6e 20 51 75  3 Ellipses in Qu
1500: 61 73 69 71 75 6f 74 65 27 64 20 4f 75 74 70 75  asiquote'd Outpu
1510: 74 0a 0a 57 69 74 68 69 6e 20 74 68 65 20 62 6f  t..Within the bo
1520: 64 79 20 6f 66 20 61 6e 20 73 78 6d 6c 2d 6d 61  dy of an sxml-ma
1530: 74 63 68 20 66 6f 72 6d 2c 20 61 20 73 6c 69 67  tch form, a slig
1540: 68 74 6c 79 20 65 78 74 65 6e 64 65 64 20 76 65  htly extended ve
1550: 72 73 69 6f 6e 20 6f 66 20 71 75 61 73 69 71 75  rsion of quasiqu
1560: 6f 74 65 20 69 73 20 70 72 6f 76 69 64 65 64 2c  ote is provided,
1570: 20 77 68 69 63 68 20 61 6c 6c 6f 77 73 20 74 68   which allows th
1580: 65 20 75 73 65 20 6f 66 20 65 6c 6c 69 70 73 65  e use of ellipse
1590: 73 2e 20 54 68 69 73 20 69 73 20 69 6c 6c 75 73  s. This is illus
15a0: 74 72 61 74 65 64 20 69 6e 20 74 68 65 20 65 78  trated in the ex
15b0: 61 6d 70 6c 65 20 62 65 6c 6f 77 2e 0a 28 73 78  ample below..(sx
15c0: 6d 6c 2d 6d 61 74 63 68 20 27 28 65 20 33 20 34  ml-match '(e 3 4
15d0: 20 35 20 36 20 37 29 0a 20 20 5b 28 65 20 2c 69   5 6 7).  [(e ,i
15e0: 20 2e 2e 2e 20 36 20 37 29 20 60 28 22 73 74 61   ... 6 7) `("sta
15f0: 72 74 22 20 2c 28 6c 69 73 74 20 27 77 72 61 70  rt" ,(list 'wrap
1600: 20 69 29 20 2e 2e 2e 20 22 65 6e 64 22 29 5d 0a   i) ... "end")].
1610: 20 20 5b 2c 6f 74 68 65 72 77 69 73 65 20 23 66    [,otherwise #f
1620: 5d 29 0a 0a 0a 54 68 65 20 67 65 6e 65 72 61 6c  ])...The general
1630: 20 70 61 74 74 65 72 6e 20 69 73 20 74 68 61 74   pattern is that
1640: 20 60 28 73 6f 6d 65 74 68 69 6e 67 20 2c 69 20   `(something ,i 
1650: 2e 2e 2e 29 20 69 73 20 72 65 77 72 69 74 74 65  ...) is rewritte
1660: 6e 20 61 73 20 60 28 73 6f 6d 65 74 68 69 6e 67  n as `(something
1670: 20 2c 40 69 29 2e 0a 0a 31 2e 34 20 4d 61 74 63   ,@i)...1.4 Matc
1680: 68 69 6e 67 20 4e 6f 64 65 73 65 74 73 0a 0a 41  hing Nodesets..A
1690: 20 6e 6f 64 65 73 65 74 20 70 61 74 74 65 72 6e   nodeset pattern
16a0: 20 69 73 20 64 65 73 69 67 6e 61 74 65 64 20 62   is designated b
16b0: 79 20 61 20 6c 69 73 74 20 69 6e 20 74 68 65 20  y a list in the 
16c0: 70 61 74 74 65 72 6e 2c 20 62 65 67 69 6e 6e 69  pattern, beginni
16d0: 6e 67 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  ng the identifie
16e0: 72 20 6c 69 73 74 2e 20 54 68 65 20 65 78 61 6d  r list. The exam
16f0: 70 6c 65 20 62 65 6c 6f 77 20 69 6c 6c 75 73 74  ple below illust
1700: 72 61 74 65 73 20 6d 61 74 63 68 69 6e 67 20 61  rates matching a
1710: 20 6e 6f 64 65 73 65 74 2e 0a 28 73 78 6d 6c 2d   nodeset..(sxml-
1720: 6d 61 74 63 68 20 27 28 22 69 22 20 22 6a 22 20  match '("i" "j" 
1730: 22 6b 22 20 22 6c 22 20 22 6d 22 29 0a 20 20 5b  "k" "l" "m").  [
1740: 28 6c 69 73 74 20 2c 61 20 2c 62 20 2c 63 20 2c  (list ,a ,b ,c ,
1750: 64 20 2c 65 29 0a 20 20 20 60 28 28 70 20 2c 61  d ,e).   `((p ,a
1760: 29 20 28 70 20 2c 62 29 20 28 70 20 2c 63 29 20  ) (p ,b) (p ,c) 
1770: 28 70 20 2c 64 29 20 28 70 20 2c 65 29 29 5d 29  (p ,d) (p ,e))])
1780: 0a 0a 0a 54 68 69 73 20 65 78 61 6d 70 6c 65 20  ...This example 
1790: 77 72 61 70 73 20 65 61 63 68 20 6e 6f 64 65 73  wraps each nodes
17a0: 65 74 20 69 74 65 6d 20 69 6e 20 61 6e 20 48 54  et item in an HT
17b0: 4d 4c 20 70 61 72 61 67 72 61 70 68 20 65 6c 65  ML paragraph ele
17c0: 6d 65 6e 74 2e 20 54 68 69 73 20 65 78 61 6d 70  ment. This examp
17d0: 6c 65 20 63 61 6e 20 62 65 20 72 65 77 72 69 74  le can be rewrit
17e0: 74 65 6e 20 61 6e 64 20 73 69 6d 70 6c 69 66 69  ten and simplifi
17f0: 65 64 20 74 68 72 6f 75 67 68 20 75 73 69 6e 67  ed through using
1800: 20 65 6c 6c 69 70 73 69 73 3a 0a 28 73 78 6d 6c   ellipsis:.(sxml
1810: 2d 6d 61 74 63 68 20 27 28 22 69 22 20 22 6a 22  -match '("i" "j"
1820: 20 22 6b 22 20 22 6c 22 20 22 6d 22 29 0a 20 20   "k" "l" "m").  
1830: 5b 28 6c 69 73 74 20 2c 69 20 2e 2e 2e 29 0a 20  [(list ,i ...). 
1840: 20 20 60 28 28 70 20 2c 69 29 20 2e 2e 2e 29 5d    `((p ,i) ...)]
1850: 29 0a 0a 0a 54 68 69 73 20 76 65 72 73 69 6f 6e  )...This version
1860: 20 77 69 6c 6c 20 6d 61 74 63 68 20 6e 6f 64 65   will match node
1870: 73 65 74 73 20 6f 66 20 61 6e 79 20 6c 65 6e 67  sets of any leng
1880: 74 68 2c 20 61 6e 64 20 77 72 61 70 20 65 61 63  th, and wrap eac
1890: 68 20 69 74 65 6d 20 69 6e 20 74 68 65 20 6e 6f  h item in the no
18a0: 64 65 73 65 74 20 69 6e 20 61 6e 20 48 54 4d 4c  deset in an HTML
18b0: 20 70 61 72 61 67 72 61 70 68 20 65 6c 65 6d 65   paragraph eleme
18c0: 6e 74 2e 0a 0a 31 2e 35 20 4d 61 74 63 68 69 6e  nt...1.5 Matchin
18d0: 67 20 74 68 65 20 27 52 65 73 74 27 20 6f 66 20  g the 'Rest' of 
18e0: 61 20 4e 6f 64 65 73 65 74 0a 0a 4d 61 74 63 68  a Nodeset..Match
18f0: 69 6e 67 20 74 68 65 20 27 72 65 73 74 27 20 6f  ing the 'rest' o
1900: 66 20 61 20 6e 6f 64 65 73 65 74 20 69 73 20 61  f a nodeset is a
1910: 63 68 69 65 76 65 64 20 62 79 20 75 73 69 6e 67  chieved by using
1920: 20 61 20 22 2e 20 72 65 73 74 29 22 20 70 61 74   a ". rest)" pat
1930: 74 65 72 6e 20 61 74 20 74 68 65 20 65 6e 64 20  tern at the end 
1940: 6f 66 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 72  of an element or
1950: 20 6e 6f 64 65 73 65 74 20 70 61 74 74 65 72 6e   nodeset pattern
1960: 2e 0a 0a 54 68 69 73 20 69 73 20 69 6c 6c 75 73  ...This is illus
1970: 74 72 61 74 65 64 20 69 6e 20 74 68 65 20 65 78  trated in the ex
1980: 61 6d 70 6c 65 20 62 65 6c 6f 77 3a 0a 28 73 78  ample below:.(sx
1990: 6d 6c 2d 6d 61 74 63 68 20 27 28 65 20 33 20 28  ml-match '(e 3 (
19a0: 66 20 34 20 35 20 36 29 20 37 29 0a 20 20 5b 28  f 4 5 6) 7).  [(
19b0: 65 20 2c 61 20 28 66 20 2e 20 2c 79 29 20 2c 64  e ,a (f . ,y) ,d
19c0: 29 0a 20 20 20 28 6c 69 73 74 20 61 20 79 20 64  ).   (list a y d
19d0: 29 5d 29 0a 0a 0a 54 68 65 20 61 62 6f 76 65 20  )])...The above 
19e0: 65 78 70 72 65 73 73 69 6f 6e 20 72 65 74 75 72  expression retur
19f0: 6e 73 20 28 33 20 28 34 20 35 20 36 29 20 37 29  ns (3 (4 5 6) 7)
1a00: 2e 0a 0a 31 2e 36 20 4d 61 74 63 68 69 6e 67 20  ...1.6 Matching 
1a10: 74 68 65 20 27 55 6e 6d 61 74 63 68 65 64 20 41  the 'Unmatched A
1a20: 74 74 72 69 62 75 74 65 73 0a 0a 53 6f 6d 65 74  ttributes..Somet
1a30: 69 6d 65 73 20 69 74 20 69 73 20 75 73 65 66 75  imes it is usefu
1a40: 6c 20 74 6f 20 62 69 6e 64 20 61 20 6c 69 73 74  l to bind a list
1a50: 20 6f 66 20 61 74 74 72 69 62 75 74 65 73 20 70   of attributes p
1a60: 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 65 6c  resent in the el
1a70: 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d 61 74 63  ement being matc
1a80: 68 65 64 2c 20 62 75 74 20 77 68 69 63 68 20 64  hed, but which d
1a90: 6f 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20  o not appear in 
1aa0: 74 68 65 20 70 61 74 74 65 72 6e 2e 20 54 68 69  the pattern. Thi
1ab0: 73 20 69 73 20 61 63 68 69 65 76 65 64 20 62 79  s is achieved by
1ac0: 20 75 73 69 6e 67 20 61 20 22 2e 20 72 65 73 74   using a ". rest
1ad0: 29 22 20 70 61 74 74 65 72 6e 20 61 74 20 74 68  )" pattern at th
1ae0: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 61 74 74  e end of the att
1af0: 72 69 62 75 74 65 20 6c 69 73 74 20 70 61 74 74  ribute list patt
1b00: 65 72 6e 2e 0a 0a 54 68 69 73 20 69 73 20 69 6c  ern...This is il
1b10: 6c 75 73 74 72 61 74 65 64 20 69 6e 20 74 68 65  lustrated in the
1b20: 20 65 78 61 6d 70 6c 65 20 62 65 6c 6f 77 3a 0a   example below:.
1b30: 28 73 78 6d 6c 2d 6d 61 74 63 68 20 27 28 61 20  (sxml-match '(a 
1b40: 28 40 20 28 7a 20 31 29 20 28 79 20 32 29 20 28  (@ (z 1) (y 2) (
1b50: 78 20 33 29 29 20 34 20 35 20 36 29 0a 20 20 5b  x 3)) 4 5 6).  [
1b60: 28 61 20 28 40 20 28 79 20 2c 77 77 77 29 20 2e  (a (@ (y ,www) .
1b70: 20 2c 71 71 71 29 20 2c 74 20 2c 75 20 2c 76 29   ,qqq) ,t ,u ,v)
1b80: 0a 20 20 20 28 6c 69 73 74 20 77 77 77 20 71 71  .   (list www qq
1b90: 71 20 74 20 75 20 76 29 5d 29 0a 0a 0a 54 68 65  q t u v)])...The
1ba0: 20 61 62 6f 76 65 20 65 78 70 72 65 73 73 69 6f   above expressio
1bb0: 6e 20 6d 61 74 63 68 65 73 20 74 68 65 20 61 74  n matches the at
1bc0: 74 72 69 62 75 74 65 20 27 79 27 20 61 6e 64 20  tribute 'y' and 
1bd0: 62 69 6e 64 73 20 61 20 6c 69 73 74 20 6f 66 20  binds a list of 
1be0: 74 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 61 74  the remaining at
1bf0: 74 72 69 62 75 74 65 73 20 74 6f 20 74 68 65 20  tributes to the 
1c00: 76 61 72 69 61 62 6c 65 20 27 71 71 71 27 2e 20  variable 'qqq'. 
1c10: 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  The result of th
1c20: 65 20 61 62 6f 76 65 20 65 78 70 72 65 73 73 69  e above expressi
1c30: 6f 6e 20 69 73 20 28 32 20 28 28 7a 20 31 29 20  on is (2 ((z 1) 
1c40: 28 78 20 33 29 29 20 34 20 35 20 36 29 2e 0a 0a  (x 3)) 4 5 6)...
1c50: 54 68 69 73 20 74 79 70 65 20 6f 66 20 70 61 74  This type of pat
1c60: 74 65 72 6e 20 61 6c 73 6f 20 61 6c 6c 6f 77 73  tern also allows
1c70: 20 74 68 65 20 62 69 6e 64 69 6e 67 20 6f 66 20   the binding of 
1c80: 61 6c 6c 20 61 74 74 72 69 62 75 74 65 73 3a 0a  all attributes:.
1c90: 28 73 78 6d 6c 2d 6d 61 74 63 68 20 27 28 61 20  (sxml-match '(a 
1ca0: 28 40 20 28 7a 20 31 29 20 28 79 20 32 29 20 28  (@ (z 1) (y 2) (
1cb0: 78 20 33 29 29 29 0a 20 20 5b 28 61 20 28 40 20  x 3))).  [(a (@ 
1cc0: 2e 20 2c 71 71 71 29 29 0a 20 20 20 71 71 71 5d  . ,qqq)).   qqq]
1cd0: 29 0a 0a 0a 31 2e 37 20 44 65 66 61 75 6c 74 20  )...1.7 Default 
1ce0: 56 61 6c 75 65 73 20 69 6e 20 41 74 74 72 69 62  Values in Attrib
1cf0: 75 74 65 20 50 61 74 74 65 72 6e 73 0a 0a 49 74  ute Patterns..It
1d00: 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   is possible to 
1d10: 73 70 65 63 69 66 79 20 61 20 64 65 66 61 75 6c  specify a defaul
1d20: 74 20 76 61 6c 75 65 20 66 6f 72 20 61 6e 20 61  t value for an a
1d30: 74 74 72 69 62 75 74 65 20 77 68 69 63 68 20 69  ttribute which i
1d40: 73 20 75 73 65 64 20 69 66 20 74 68 65 20 61 74  s used if the at
1d50: 74 72 69 62 75 74 65 20 69 73 20 6e 6f 74 20 70  tribute is not p
1d60: 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 65 6c  resent in the el
1d70: 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d 61 74 63  ement being matc
1d80: 68 65 64 2e 20 54 68 69 73 20 69 73 20 69 6c 6c  hed. This is ill
1d90: 75 73 74 72 61 74 65 64 20 69 6e 20 74 68 65 20  ustrated in the 
1da0: 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 61 6d 70 6c  following exampl
1db0: 65 3a 0a 28 73 78 6d 6c 2d 6d 61 74 63 68 20 27  e:.(sxml-match '
1dc0: 28 65 20 33 20 34 20 35 29 0a 20 20 5b 28 65 20  (e 3 4 5).  [(e 
1dd0: 28 40 20 28 7a 20 28 2c 64 20 31 29 29 29 20 2c  (@ (z (,d 1))) ,
1de0: 61 20 2c 62 20 2c 63 29 20 28 6c 69 73 74 20 64  a ,b ,c) (list d
1df0: 20 61 20 62 20 63 29 5d 29 0a 0a 0a 54 68 65 20   a b c)])...The 
1e00: 76 61 6c 75 65 20 31 20 69 73 20 75 73 65 64 20  value 1 is used 
1e10: 77 68 65 6e 20 74 68 65 20 61 74 74 72 69 62 75  when the attribu
1e20: 74 65 20 27 7a 27 20 69 73 20 61 62 73 65 6e 74  te 'z' is absent
1e30: 20 66 72 6f 6d 20 74 68 65 20 65 6c 65 6d 65 6e   from the elemen
1e40: 74 20 27 65 27 2e 0a 0a 31 2e 38 20 47 75 61 72  t 'e'...1.8 Guar
1e50: 64 73 20 69 6e 20 50 61 74 74 65 72 6e 73 0a 0a  ds in Patterns..
1e60: 47 75 61 72 64 73 20 6d 61 79 20 62 65 20 61 64  Guards may be ad
1e70: 64 65 64 20 74 6f 20 61 20 70 61 74 74 65 72 6e  ded to a pattern
1e80: 20 63 6c 61 75 73 65 20 76 69 61 20 74 68 65 20   clause via the 
1e90: 67 75 61 72 64 20 6b 65 79 77 6f 72 64 2e 20 41  guard keyword. A
1ea0: 20 67 75 61 72 64 20 65 78 70 72 65 73 73 69 6f   guard expressio
1eb0: 6e 20 6d 61 79 20 69 6e 63 6c 75 64 65 20 7a 65  n may include ze
1ec0: 72 6f 20 6f 72 20 6d 6f 72 65 20 65 78 70 72 65  ro or more expre
1ed0: 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65  ssions which are
1ee0: 20 65 76 61 6c 75 61 74 65 64 20 6f 6e 6c 79 20   evaluated only 
1ef0: 69 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 69  if the pattern i
1f00: 73 20 6d 61 74 63 68 65 64 2e 20 54 68 65 20 62  s matched. The b
1f10: 6f 64 79 20 6f 66 20 74 68 65 20 63 6c 61 75 73  ody of the claus
1f20: 65 20 69 73 20 6f 6e 6c 79 20 65 76 61 6c 75 61  e is only evalua
1f30: 74 65 64 20 69 66 20 74 68 65 20 67 75 61 72 64  ted if the guard
1f40: 20 65 78 70 72 65 73 73 69 6f 6e 73 20 65 76 61   expressions eva
1f50: 6c 75 61 74 65 20 74 6f 20 23 74 2e 0a 0a 54 68  luate to #t...Th
1f60: 65 20 75 73 65 20 6f 66 20 67 75 61 72 64 20 65  e use of guard e
1f70: 78 70 72 65 73 73 69 6f 6e 73 20 69 73 20 69 6c  xpressions is il
1f80: 6c 75 73 74 72 61 74 65 64 20 62 65 6c 6f 77 3a  lustrated below:
1f90: 0a 28 73 78 6d 6c 2d 6d 61 74 63 68 20 27 28 61  .(sxml-match '(a
1fa0: 20 32 20 33 29 0a 20 20 28 28 61 20 2c 6e 29 20   2 3).  ((a ,n) 
1fb0: 28 67 75 61 72 64 20 28 6e 75 6d 62 65 72 3f 20  (guard (number? 
1fc0: 6e 29 29 20 6e 29 0a 20 20 28 28 61 20 2c 6d 20  n)) n).  ((a ,m 
1fd0: 2c 6e 29 20 28 67 75 61 72 64 20 28 6e 75 6d 62  ,n) (guard (numb
1fe0: 65 72 3f 20 6d 29 20 28 6e 75 6d 62 65 72 3f 20  er? m) (number? 
1ff0: 6e 29 29 20 28 2b 20 6d 20 6e 29 29 29 0a 0a 31  n)) (+ m n)))..1
2000: 2e 39 20 43 61 74 61 6d 6f 72 70 68 69 73 6d 73  .9 Catamorphisms
2010: 0a 0a 54 68 65 20 65 78 61 6d 70 6c 65 20 62 65  ..The example be
2020: 6c 6f 77 20 69 6c 6c 75 73 74 72 61 74 65 73 20  low illustrates 
2030: 74 68 65 20 75 73 65 20 6f 66 20 65 78 70 6c 69  the use of expli
2040: 63 69 74 20 72 65 63 75 72 73 69 6f 6e 20 77 69  cit recursion wi
2050: 74 68 69 6e 20 61 6e 20 73 78 6d 6c 2d 6d 61 74  thin an sxml-mat
2060: 63 68 20 66 6f 72 6d 2e 20 54 68 69 73 20 65 78  ch form. This ex
2070: 61 6d 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73  ample implements
2080: 20 61 20 73 69 6d 70 6c 65 20 63 61 6c 63 75 6c   a simple calcul
2090: 61 74 6f 72 20 66 6f 72 20 74 68 65 20 62 61 73  ator for the bas
20a0: 69 63 20 61 72 69 74 68 6d 65 74 69 63 20 6f 70  ic arithmetic op
20b0: 65 72 61 74 69 6f 6e 73 2c 20 77 68 69 63 68 20  erations, which 
20c0: 61 72 65 20 72 65 70 72 65 73 65 6e 74 65 64 20  are represented 
20d0: 62 79 20 74 68 65 20 58 4d 4c 20 65 6c 65 6d 65  by the XML eleme
20e0: 6e 74 73 20 70 6c 75 73 2c 20 6d 69 6e 75 73 2c  nts plus, minus,
20f0: 20 74 69 6d 65 73 2c 20 61 6e 64 20 64 69 76 2e   times, and div.
2100: 0a 28 64 65 66 69 6e 65 20 73 69 6d 70 6c 65 2d  .(define simple-
2110: 65 76 61 6c 0a 20 20 28 6c 61 6d 62 64 61 20 28  eval.  (lambda (
2120: 78 29 0a 20 20 20 20 28 73 78 6d 6c 2d 6d 61 74  x).    (sxml-mat
2130: 63 68 20 78 0a 20 20 20 20 20 20 5b 2c 69 20 28  ch x.      [,i (
2140: 67 75 61 72 64 20 28 69 6e 74 65 67 65 72 3f 20  guard (integer? 
2150: 69 29 29 20 69 5d 0a 20 20 20 20 20 20 5b 28 70  i)) i].      [(p
2160: 6c 75 73 20 2c 78 20 2c 79 29 20 28 2b 20 28 73  lus ,x ,y) (+ (s
2170: 69 6d 70 6c 65 2d 65 76 61 6c 20 78 29 20 28 73  imple-eval x) (s
2180: 69 6d 70 6c 65 2d 65 76 61 6c 20 79 29 29 5d 0a  imple-eval y))].
2190: 20 20 20 20 20 20 5b 28 74 69 6d 65 73 20 2c 78        [(times ,x
21a0: 20 2c 79 29 20 28 2a 20 28 73 69 6d 70 6c 65 2d   ,y) (* (simple-
21b0: 65 76 61 6c 20 78 29 20 28 73 69 6d 70 6c 65 2d  eval x) (simple-
21c0: 65 76 61 6c 20 79 29 29 5d 0a 20 20 20 20 20 20  eval y))].      
21d0: 5b 28 6d 69 6e 75 73 20 2c 78 20 2c 79 29 20 28  [(minus ,x ,y) (
21e0: 2d 20 28 73 69 6d 70 6c 65 2d 65 76 61 6c 20 78  - (simple-eval x
21f0: 29 20 28 73 69 6d 70 6c 65 2d 65 76 61 6c 20 79  ) (simple-eval y
2200: 29 29 5d 0a 20 20 20 20 20 20 5b 28 64 69 76 20  ))].      [(div 
2210: 2c 78 20 2c 79 29 20 28 2f 20 28 73 69 6d 70 6c  ,x ,y) (/ (simpl
2220: 65 2d 65 76 61 6c 20 78 29 20 28 73 69 6d 70 6c  e-eval x) (simpl
2230: 65 2d 65 76 61 6c 20 79 29 29 5d 0a 20 20 20 20  e-eval y))].    
2240: 20 20 5b 2c 6f 74 68 65 72 77 69 73 65 20 28 65    [,otherwise (e
2250: 72 72 6f 72 20 22 73 69 6d 70 6c 65 2d 65 76 61  rror "simple-eva
2260: 6c 3a 20 69 6e 76 61 6c 69 64 20 65 78 70 72 65  l: invalid expre
2270: 73 73 69 6f 6e 22 20 78 29 5d 29 29 29 0a 0a 0a  ssion" x)])))...
2280: 55 73 69 6e 67 20 74 68 65 20 63 61 74 61 6d 6f  Using the catamo
2290: 72 70 68 69 73 6d 20 66 65 61 74 75 72 65 20 6f  rphism feature o
22a0: 66 20 73 78 6d 6c 2d 6d 61 74 63 68 20 2c 20 61  f sxml-match , a
22b0: 20 6d 6f 72 65 20 63 6f 6e 63 69 73 65 20 76 65   more concise ve
22c0: 72 73 69 6f 6e 20 6f 66 20 73 69 6d 70 6c 65 2d  rsion of simple-
22d0: 65 76 61 6c 20 63 61 6e 20 62 65 20 77 72 69 74  eval can be writ
22e0: 74 65 6e 2e 20 54 68 65 20 70 61 74 74 65 72 6e  ten. The pattern
22f0: 20 2c 5b 78 5d 20 72 65 63 75 73 69 76 65 6c 79   ,[x] recusively
2300: 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 74   invokes the pat
2310: 74 65 72 6e 20 6d 61 74 63 68 65 72 20 6f 6e 20  tern matcher on 
2320: 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64 20  the value bound 
2330: 69 6e 20 74 68 69 73 20 70 6f 73 69 74 69 6f 6e  in this position
2340: 2e 0a 28 64 65 66 69 6e 65 20 73 69 6d 70 6c 65  ..(define simple
2350: 2d 65 76 61 6c 0a 20 20 28 6c 61 6d 62 64 61 20  -eval.  (lambda 
2360: 28 78 29 0a 20 20 20 20 28 73 78 6d 6c 2d 6d 61  (x).    (sxml-ma
2370: 74 63 68 20 78 0a 20 20 20 20 20 20 5b 2c 69 20  tch x.      [,i 
2380: 28 67 75 61 72 64 20 28 69 6e 74 65 67 65 72 3f  (guard (integer?
2390: 20 69 29 29 20 69 5d 0a 20 20 20 20 20 20 5b 28   i)) i].      [(
23a0: 70 6c 75 73 20 2c 5b 78 5d 20 2c 5b 79 5d 29 20  plus ,[x] ,[y]) 
23b0: 28 2b 20 78 20 79 29 5d 0a 20 20 20 20 20 20 5b  (+ x y)].      [
23c0: 28 74 69 6d 65 73 20 2c 5b 78 5d 20 2c 5b 79 5d  (times ,[x] ,[y]
23d0: 29 20 28 2a 20 78 20 79 29 5d 0a 20 20 20 20 20  ) (* x y)].     
23e0: 20 5b 28 6d 69 6e 75 73 20 2c 5b 78 5d 20 2c 5b   [(minus ,[x] ,[
23f0: 79 5d 29 20 28 2d 20 78 20 79 29 5d 0a 20 20 20  y]) (- x y)].   
2400: 20 20 20 5b 28 64 69 76 20 2c 5b 78 5d 20 2c 5b     [(div ,[x] ,[
2410: 79 5d 29 20 28 2f 20 78 20 79 29 5d 0a 20 20 20  y]) (/ x y)].   
2420: 20 20 20 5b 2c 6f 74 68 65 72 77 69 73 65 20 28     [,otherwise (
2430: 65 72 72 6f 72 20 22 73 69 6d 70 6c 65 2d 65 76  error "simple-ev
2440: 61 6c 3a 20 69 6e 76 61 6c 69 64 20 65 78 70 72  al: invalid expr
2450: 65 73 73 69 6f 6e 22 20 78 29 5d 29 29 29 0a 0a  ession" x)])))..
2460: 0a 31 2e 31 30 20 4e 61 6d 65 64 2d 43 61 74 61  .1.10 Named-Cata
2470: 6d 6f 72 70 68 69 73 6d 73 0a 0a 49 74 20 69 73  morphisms..It is
2480: 20 61 6c 73 6f 20 70 6f 73 73 69 62 6c 65 20 74   also possible t
2490: 6f 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d  o explicitly nam
24a0: 65 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 69  e the operator i
24b0: 6e 20 74 68 65 20 27 63 61 74 61 27 20 70 6f 73  n the 'cata' pos
24c0: 69 74 69 6f 6e 2e 20 57 68 65 72 65 20 2c 5b 69  ition. Where ,[i
24d0: 64 2a 5d 20 72 65 63 75 72 73 20 74 6f 20 74 68  d*] recurs to th
24e0: 65 20 74 6f 70 20 6f 66 20 74 68 65 20 63 75 72  e top of the cur
24f0: 72 65 6e 74 20 73 78 6d 6c 2d 6d 61 74 63 68 2c  rent sxml-match,
2500: 20 2c 5b 63 61 74 61 20 2d 3e 20 69 64 2a 5d 20   ,[cata -> id*] 
2510: 72 65 63 75 72 73 20 74 6f 20 63 61 74 61 2e 20  recurs to cata. 
2520: 63 61 74 61 20 6d 75 73 74 20 65 76 61 6c 75 61  cata must evalua
2530: 74 65 20 74 6f 20 61 20 70 72 6f 63 65 64 75 72  te to a procedur
2540: 65 20 77 68 69 63 68 20 74 61 6b 65 73 20 6f 6e  e which takes on
2550: 65 20 61 72 67 75 6d 65 6e 74 2c 20 61 6e 64 20  e argument, and 
2560: 72 65 74 75 72 6e 73 20 61 73 20 6d 61 6e 79 20  returns as many 
2570: 76 61 6c 75 65 73 20 61 73 20 74 68 65 72 65 20  values as there 
2580: 61 72 65 20 69 64 65 6e 74 69 66 69 65 72 73 20  are identifiers 
2590: 66 6f 6c 6c 6f 77 69 6e 67 20 2d 3e 2e 0a 0a 4e  following ->...N
25a0: 61 6d 65 64 20 63 61 74 61 6d 6f 72 70 68 69 73  amed catamorphis
25b0: 6d 20 70 61 74 74 65 72 6e 73 20 61 6c 6c 6f 77  m patterns allow
25c0: 20 70 72 6f 63 65 73 73 69 6e 67 20 74 6f 20 62   processing to b
25d0: 65 20 73 70 6c 69 74 20 69 6e 74 6f 20 6d 75 6c  e split into mul
25e0: 74 69 70 6c 65 2c 20 6d 75 74 75 61 6c 6c 79 20  tiple, mutually 
25f0: 72 65 63 75 72 73 69 76 65 20 70 72 6f 63 65 64  recursive proced
2600: 75 72 65 73 2e 20 54 68 69 73 20 69 73 20 69 6c  ures. This is il
2610: 6c 75 73 74 72 61 74 65 64 20 69 6e 20 74 68 65  lustrated in the
2620: 20 65 78 61 6d 70 6c 65 20 62 65 6c 6f 77 3a 20   example below: 
2630: 61 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e  a transformation
2640: 20 74 68 61 74 20 66 6f 72 6d 61 74 73 20 61 20   that formats a 
2650: 22 54 56 20 47 75 69 64 65 22 20 69 6e 74 6f 20  "TV Guide" into 
2660: 48 54 4d 4c 2e 0a 28 64 65 66 69 6e 65 20 28 74  HTML..(define (t
2670: 76 2d 67 75 69 64 65 2d 3e 68 74 6d 6c 20 67 29  v-guide->html g)
2680: 0a 20 20 28 64 65 66 69 6e 65 20 28 63 61 73 74  .  (define (cast
2690: 2d 6c 69 73 74 20 63 6c 29 0a 20 20 20 20 28 73  -list cl).    (s
26a0: 78 6d 6c 2d 6d 61 74 63 68 20 63 6c 0a 20 20 20  xml-match cl.   
26b0: 20 20 20 5b 28 43 61 73 74 4c 69 73 74 20 28 43     [(CastList (C
26c0: 61 73 74 4d 65 6d 62 65 72 20 28 43 68 61 72 61  astMember (Chara
26d0: 63 74 65 72 20 28 4e 61 6d 65 20 2c 63 68 29 29  cter (Name ,ch))
26e0: 20 28 41 63 74 6f 72 20 28 4e 61 6d 65 20 2c 61   (Actor (Name ,a
26f0: 29 29 29 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20  ))) ...).       
2700: 60 28 64 69 76 20 28 75 6c 20 28 6c 69 20 2c 63  `(div (ul (li ,c
2710: 68 20 22 3a 20 22 20 2c 61 29 20 2e 2e 2e 29 29  h ": " ,a) ...))
2720: 5d 29 29 0a 20 20 28 64 65 66 69 6e 65 20 28 70  ])).  (define (p
2730: 72 6f 67 20 70 29 0a 20 20 20 20 28 73 78 6d 6c  rog p).    (sxml
2740: 2d 6d 61 74 63 68 20 70 0a 20 20 20 20 20 20 5b  -match p.      [
2750: 28 50 72 6f 67 72 61 6d 20 28 53 74 61 72 74 20  (Program (Start 
2760: 2c 73 74 61 72 74 2d 74 69 6d 65 29 20 28 44 75  ,start-time) (Du
2770: 72 61 74 69 6f 6e 20 2c 64 75 72 29 20 28 53 65  ration ,dur) (Se
2780: 72 69 65 73 20 2c 73 65 72 69 65 73 2d 74 69 74  ries ,series-tit
2790: 6c 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  le).            
27a0: 20 20 20 20 28 44 65 73 63 72 69 70 74 69 6f 6e      (Description
27b0: 20 2c 64 65 73 63 20 2e 2e 2e 29 29 0a 20 20 20   ,desc ...)).   
27c0: 20 20 20 20 60 28 64 69 76 20 28 70 20 2c 73 74      `(div (p ,st
27d0: 61 72 74 2d 74 69 6d 65 0a 20 20 20 20 20 20 20  art-time.       
27e0: 20 20 20 20 20 20 20 20 20 28 62 72 29 20 2c 73           (br) ,s
27f0: 65 72 69 65 73 2d 74 69 74 6c 65 0a 20 20 20 20  eries-title.    
2800: 20 20 20 20 20 20 20 20 20 20 20 20 28 62 72 29              (br)
2810: 20 2c 64 65 73 63 20 2e 2e 2e 29 29 5d 0a 20 20   ,desc ...))].  
2820: 20 20 20 20 5b 28 50 72 6f 67 72 61 6d 20 28 53      [(Program (S
2830: 74 61 72 74 20 2c 73 74 61 72 74 2d 74 69 6d 65  tart ,start-time
2840: 29 20 28 44 75 72 61 74 69 6f 6e 20 2c 64 75 72  ) (Duration ,dur
2850: 29 20 28 53 65 72 69 65 73 20 2c 73 65 72 69 65  ) (Series ,serie
2860: 73 2d 74 69 74 6c 65 29 0a 20 20 20 20 20 20 20  s-title).       
2870: 20 20 20 20 20 20 20 20 20 28 44 65 73 63 72 69           (Descri
2880: 70 74 69 6f 6e 20 2c 64 65 73 63 20 2e 2e 2e 29  ption ,desc ...)
2890: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28a0: 20 2c 5b 63 61 73 74 2d 6c 69 73 74 20 2d 3e 20   ,[cast-list -> 
28b0: 63 6c 5d 29 0a 20 20 20 20 20 20 20 60 28 64 69  cl]).       `(di
28c0: 76 20 28 70 20 2c 73 74 61 72 74 2d 74 69 6d 65  v (p ,start-time
28d0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
28e0: 20 28 62 72 29 20 2c 73 65 72 69 65 73 2d 74 69   (br) ,series-ti
28f0: 74 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  tle.            
2900: 20 20 20 20 28 62 72 29 20 2c 64 65 73 63 20 2e      (br) ,desc .
2910: 2e 2e 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ..).            
2920: 20 2c 63 6c 29 5d 29 29 0a 20 20 28 73 78 6d 6c   ,cl)])).  (sxml
2930: 2d 6d 61 74 63 68 20 67 0a 20 20 20 20 5b 28 54  -match g.    [(T
2940: 56 47 75 69 64 65 20 28 40 20 28 73 74 61 72 74  VGuide (@ (start
2950: 20 2c 73 74 61 72 74 2d 64 61 74 65 29 0a 20 20   ,start-date).  
2960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
2970: 65 6e 64 20 2c 65 6e 64 2d 64 61 74 65 29 29 0a  end ,end-date)).
2980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 43                (C
2990: 68 61 6e 6e 65 6c 20 28 4e 61 6d 65 20 2c 6e 6d  hannel (Name ,nm
29a0: 29 20 2c 5b 70 72 6f 67 20 2d 3e 20 70 5d 20 2e  ) ,[prog -> p] .
29b0: 2e 2e 29 20 2e 2e 2e 29 0a 20 20 20 20 20 60 28  ..) ...).     `(
29c0: 68 74 6d 6c 20 28 68 65 61 64 20 28 74 69 74 6c  html (head (titl
29d0: 65 20 22 54 56 20 47 75 69 64 65 22 29 29 0a 20  e "TV Guide")). 
29e0: 20 20 20 20 20 20 20 20 20 20 20 28 62 6f 64 79             (body
29f0: 20 28 68 31 20 22 54 56 20 47 75 69 64 65 22 29   (h1 "TV Guide")
2a00: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a10: 20 20 20 28 64 69 76 20 28 68 32 20 2c 6e 6d 29     (div (h2 ,nm)
2a20: 20 2c 70 20 2e 2e 2e 29 20 2e 2e 2e 29 29 5d 29   ,p ...) ...))])
2a30: 29 0a 0a 0a 32 20 5f 73 78 6d 6c 2d 6d 61 74 63  )...2 _sxml-matc
2a40: 68 2d 6c 65 74 5f 20 61 6e 64 20 5f 73 78 6d 6c  h-let_ and _sxml
2a50: 2d 6d 61 74 63 68 2d 6c 65 74 2a 5f 0a 3d 3d 3d  -match-let*_.===
2a60: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
2a70: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
2a80: 3d 3d 3d 3d 3d 0a 0a 73 79 6e 74 61 78 0a 0a 54  =====..syntax..T
2a90: 68 65 20 73 78 6d 6c 2d 6d 61 74 63 68 2d 6c 65  he sxml-match-le
2aa0: 74 20 61 6e 64 20 73 78 6d 6c 2d 6d 61 74 63 68  t and sxml-match
2ab0: 2d 6c 65 74 2a 20 66 6f 72 6d 73 20 67 65 6e 65  -let* forms gene
2ac0: 72 61 6c 69 7a 65 20 74 68 65 20 6c 65 74 20 61  ralize the let a
2ad0: 6e 64 20 6c 65 74 2a 20 66 6f 72 6d 73 20 6f 66  nd let* forms of
2ae0: 20 53 63 68 65 6d 65 20 74 6f 20 61 6c 6c 6f 77   Scheme to allow
2af0: 20 61 6e 20 58 4d 4c 20 70 61 74 74 65 72 6e 20   an XML pattern 
2b00: 69 6e 20 74 68 65 20 62 69 6e 64 69 6e 67 20 70  in the binding p
2b10: 6f 73 69 74 69 6f 6e 2c 20 72 61 74 68 65 72 20  osition, rather 
2b20: 74 68 61 6e 20 61 20 73 69 6d 70 6c 65 20 76 61  than a simple va
2b30: 72 69 61 62 6c 65 2e 0a 0a 46 6f 72 20 65 78 61  riable...For exa
2b40: 6d 70 6c 65 2c 20 74 68 65 20 65 78 70 72 65 73  mple, the expres
2b50: 73 69 6f 6e 20 62 65 6c 6f 77 3a 0a 28 73 78 6d  sion below:.(sxm
2b60: 6c 2d 6d 61 74 63 68 2d 6c 65 74 20 28 5b 28 61  l-match-let ([(a
2b70: 20 2c 69 20 2c 6a 29 20 27 28 61 20 31 20 32 29   ,i ,j) '(a 1 2)
2b80: 5d 29 0a 20 20 28 2b 20 69 20 6a 29 29 0a 0a 0a  ]).  (+ i j))...
2b90: 62 69 6e 64 73 20 74 68 65 20 76 61 72 69 61 62  binds the variab
2ba0: 6c 65 73 20 69 20 61 6e 64 20 6a 20 74 6f 20 31  les i and j to 1
2bb0: 20 61 6e 64 20 32 20 69 6e 20 74 68 65 20 58 4d   and 2 in the XM
2bc0: 4c 20 76 61 6c 75 65 20 67 69 76 65 6e 2e 0a 0a  L value given...
2bd0: 54 68 65 20 73 79 6e 74 61 78 20 66 6f 72 20 74  The syntax for t
2be0: 68 65 73 65 20 66 6f 72 6d 73 20 69 73 20 67 69  hese forms is gi
2bf0: 76 65 6e 20 62 65 6c 6f 77 3a 0a 28 73 78 6d 6c  ven below:.(sxml
2c00: 2d 6d 61 74 63 68 2d 6c 65 74 20 28 5b 70 61 74  -match-let ([pat
2c10: 20 65 78 70 72 5d 20 2e 2e 2e 29 20 65 78 70 72   expr] ...) expr
2c20: 65 73 73 69 6f 6e 30 20 65 78 70 72 65 73 73 69  ession0 expressi
2c30: 6f 6e 20 2e 2e 2e 29 0a 28 73 78 6d 6c 2d 6d 61  on ...).(sxml-ma
2c40: 74 63 68 2d 6c 65 74 2a 20 28 5b 70 61 74 20 65  tch-let* ([pat e
2c50: 78 70 72 5d 20 2e 2e 2e 29 20 65 78 70 72 65 73  xpr] ...) expres
2c60: 73 69 6f 6e 30 20 65 78 70 72 65 73 73 69 6f 6e  sion0 expression
2c70: 20 2e 2e 2e 29 0a 0a 0a 4e 6f 74 65 73 0a 3d 3d   ...)...Notes.==
2c80: 3d 3d 3d 0a 31 2e 20 54 68 69 73 20 65 78 61 6d  ===.1. This exam
2c90: 70 6c 65 20 69 73 20 74 61 6b 65 6e 20 66 72 6f  ple is taken fro
2ca0: 6d 20 4b 72 69 73 68 6e 61 6d 75 72 74 68 69 20  m Krishnamurthi 
2cb0: 65 74 20 61 6c 2e 2c 20 22 54 72 61 6e 73 66 6f  et al., "Transfo
2cc0: 72 6d 61 74 69 6f 6e 2d 62 79 2d 45 78 61 6d 70  rmation-by-Examp
2cd0: 6c 65 20 66 6f 72 20 58 4d 4c 22 2c 20 70 75 62  le for XML", pub
2ce0: 6c 69 73 68 65 64 20 61 74 20 50 41 44 4c 32 30  lished at PADL20
2cf0: 30 30 2e 20 54 68 65 69 72 20 70 61 70 65 72 20  00. Their paper 
2d00: 77 61 73 20 74 68 65 20 66 69 72 73 74 20 74 6f  was the first to
2d10: 20 73 68 6f 77 20 74 68 65 20 75 73 65 66 75 6c   show the useful
2d20: 6e 65 73 73 20 6f 66 20 74 68 65 20 73 79 6e 74  ness of the synt
2d30: 61 78 2d 72 75 6c 65 73 20 73 74 79 6c 65 20 6f  ax-rules style o
2d40: 66 20 70 61 74 74 65 72 6e 20 6d 61 74 63 68 69  f pattern matchi
2d50: 6e 67 20 66 6f 72 20 74 72 61 6e 73 66 6f 72 6d  ng for transform
2d60: 61 74 69 6f 6e 20 6f 66 20 58 4d 4c 2c 20 74 68  ation of XML, th
2d70: 6f 75 67 68 20 74 68 65 20 6c 61 6e 67 75 61 67  ough the languag
2d80: 65 20 64 65 73 63 72 69 62 65 64 2c 20 58 54 33  e described, XT3
2d90: 44 20 69 73 20 61 6e 20 58 4d 4c 20 6c 61 6e 67  D is an XML lang
2da0: 75 61 67 65 2e 0a 0a 32 2e 20 53 65 65 20 6d 61  uage...2. See ma
2db0: 74 63 68 2e 70 64 66 20 61 6e 64 20 61 20 50 4c  tch.pdf and a PL
2dc0: 54 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  T version of the
2dd0: 20 6c 69 62 72 61 72 79 2e 0a 0a 0a               library....