Hex Artifact Content
Not logged in

Artifact 99ad89357558a192cf476500d3892e0b9e556a1b:


0000: 3b 09 09 09 58 4d 4c 20 70 72 6f 63 65 73 73 69  ;...XML processi
0010: 6e 67 20 69 6e 20 53 63 68 65 6d 65 0a 3b 09 09  ng in Scheme.;..
0020: 20 20 20 20 20 53 58 50 61 74 68 20 2d 2d 20 53       SXPath -- S
0030: 58 4d 4c 20 51 75 65 72 79 20 4c 61 6e 67 75 61  XML Query Langua
0040: 67 65 0a 3b 0a 3b 20 53 58 50 61 74 68 20 69 73  ge.;.; SXPath is
0050: 20 61 20 71 75 65 72 79 20 6c 61 6e 67 75 61 67   a query languag
0060: 65 20 66 6f 72 20 53 58 4d 4c 2c 20 61 6e 20 69  e for SXML, an i
0070: 6e 73 74 61 6e 63 65 20 6f 66 20 58 4d 4c 20 49  nstance of XML I
0080: 6e 66 6f 72 6d 61 74 69 6f 6e 0a 3b 20 73 65 74  nformation.; set
0090: 20 28 49 6e 66 6f 73 65 74 29 20 69 6e 20 74 68   (Infoset) in th
00a0: 65 20 66 6f 72 6d 20 6f 66 20 73 2d 65 78 70 72  e form of s-expr
00b0: 65 73 73 69 6f 6e 73 2e 20 53 65 65 20 53 53 41  essions. See SSA
00c0: 58 2e 73 63 6d 20 66 6f 72 20 74 68 65 0a 3b 20  X.scm for the.; 
00d0: 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 53 58  definition of SX
00e0: 4d 4c 20 61 6e 64 20 6d 6f 72 65 20 64 65 74 61  ML and more deta
00f0: 69 6c 73 2e 20 53 58 50 61 74 68 20 69 73 20 61  ils. SXPath is a
0100: 6c 73 6f 20 61 20 74 72 61 6e 73 6c 61 74 69 6f  lso a translatio
0110: 6e 20 69 6e 74 6f 0a 3b 20 53 63 68 65 6d 65 20  n into.; Scheme 
0120: 6f 66 20 61 6e 20 58 4d 4c 20 50 61 74 68 20 4c  of an XML Path L
0130: 61 6e 67 75 61 67 65 2c 20 58 50 61 74 68 3a 0a  anguage, XPath:.
0140: 3b 09 68 74 74 70 3a 2f 2f 77 77 77 2e 77 33 2e  ;.http://www.w3.
0150: 6f 72 67 2f 54 52 2f 78 70 61 74 68 0a 3b 20 58  org/TR/xpath.; X
0160: 50 61 74 68 20 61 6e 64 20 53 58 50 61 74 68 20  Path and SXPath 
0170: 64 65 73 63 72 69 62 65 20 6d 65 61 6e 73 20 6f  describe means o
0180: 66 20 73 65 6c 65 63 74 69 6e 67 20 61 20 73 65  f selecting a se
0190: 74 20 6f 66 20 49 6e 66 6f 73 65 74 27 73 20 69  t of Infoset's i
01a0: 74 65 6d 73 0a 3b 20 6f 72 20 74 68 65 69 72 20  tems.; or their 
01b0: 70 72 6f 70 65 72 74 69 65 73 2e 0a 3b 0a 3b 20  properties..;.; 
01c0: 54 6f 20 66 61 63 69 6c 69 74 61 74 65 20 71 75  To facilitate qu
01d0: 65 72 69 65 73 2c 20 58 50 61 74 68 20 6d 61 70  eries, XPath map
01e0: 73 20 74 68 65 20 58 4d 4c 20 49 6e 66 6f 73 65  s the XML Infose
01f0: 74 20 69 6e 74 6f 20 61 6e 20 65 78 70 6c 69 63  t into an explic
0200: 69 74 0a 3b 20 74 72 65 65 2c 20 61 6e 64 20 69  it.; tree, and i
0210: 6e 74 72 6f 64 75 63 65 73 20 69 6d 70 6f 72 74  ntroduces import
0220: 61 6e 74 20 6e 6f 74 69 6f 6e 73 20 6f 66 20 61  ant notions of a
0230: 20 6c 6f 63 61 74 69 6f 6e 20 70 61 74 68 20 61   location path a
0240: 6e 64 20 61 0a 3b 20 63 75 72 72 65 6e 74 2c 20  nd a.; current, 
0250: 63 6f 6e 74 65 78 74 20 6e 6f 64 65 2e 20 41 20  context node. A 
0260: 6c 6f 63 61 74 69 6f 6e 20 70 61 74 68 20 64 65  location path de
0270: 6e 6f 74 65 73 20 61 20 73 65 6c 65 63 74 69 6f  notes a selectio
0280: 6e 20 6f 66 20 61 20 73 65 74 20 6f 66 0a 3b 20  n of a set of.; 
0290: 6e 6f 64 65 73 20 72 65 6c 61 74 69 76 65 20 74  nodes relative t
02a0: 6f 20 61 20 63 6f 6e 74 65 78 74 20 6e 6f 64 65  o a context node
02b0: 2e 20 41 6e 79 20 58 50 61 74 68 20 74 72 65 65  . Any XPath tree
02c0: 20 68 61 73 20 61 20 64 69 73 74 69 6e 67 75 69   has a distingui
02d0: 73 68 65 64 2c 0a 3b 20 72 6f 6f 74 20 6e 6f 64  shed,.; root nod
02e0: 65 20 2d 2d 20 77 68 69 63 68 20 73 65 72 76 65  e -- which serve
02f0: 73 20 61 73 20 74 68 65 20 63 6f 6e 74 65 78 74  s as the context
0300: 20 6e 6f 64 65 20 66 6f 72 20 61 62 73 6f 6c 75   node for absolu
0310: 74 65 20 6c 6f 63 61 74 69 6f 6e 0a 3b 20 70 61  te location.; pa
0320: 74 68 73 2e 20 4c 6f 63 61 74 69 6f 6e 20 70 61  ths. Location pa
0330: 74 68 20 69 73 20 72 65 63 75 72 73 69 76 65 6c  th is recursivel
0340: 79 20 64 65 66 69 6e 65 64 20 61 73 20 61 20 6c  y defined as a l
0350: 6f 63 61 74 69 6f 6e 20 73 74 65 70 20 6a 6f 69  ocation step joi
0360: 6e 65 64 0a 3b 20 77 69 74 68 20 61 20 6c 6f 63  ned.; with a loc
0370: 61 74 69 6f 6e 20 70 61 74 68 2e 20 41 20 6c 6f  ation path. A lo
0380: 63 61 74 69 6f 6e 20 73 74 65 70 20 69 73 20 61  cation step is a
0390: 20 73 69 6d 70 6c 65 20 71 75 65 72 79 20 6f 66   simple query of
03a0: 20 74 68 65 0a 3b 20 64 61 74 61 62 61 73 65 20   the.; database 
03b0: 72 65 6c 61 74 69 76 65 20 74 6f 20 61 20 63 6f  relative to a co
03c0: 6e 74 65 78 74 20 6e 6f 64 65 2e 20 41 20 73 74  ntext node. A st
03d0: 65 70 20 6d 61 79 20 69 6e 63 6c 75 64 65 20 65  ep may include e
03e0: 78 70 72 65 73 73 69 6f 6e 73 0a 3b 20 74 68 61  xpressions.; tha
03f0: 74 20 66 75 72 74 68 65 72 20 66 69 6c 74 65 72  t further filter
0400: 20 74 68 65 20 73 65 6c 65 63 74 65 64 20 73 65   the selected se
0410: 74 2e 20 45 61 63 68 20 6e 6f 64 65 20 69 6e 20  t. Each node in 
0420: 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73 65  the resulting se
0430: 74 0a 3b 20 69 73 20 75 73 65 64 20 61 73 20 61  t.; is used as a
0440: 20 63 6f 6e 74 65 78 74 20 6e 6f 64 65 20 66 6f   context node fo
0450: 72 20 74 68 65 20 61 64 6a 6f 69 6e 69 6e 67 20  r the adjoining 
0460: 6c 6f 63 61 74 69 6f 6e 20 70 61 74 68 2e 20 54  location path. T
0470: 68 65 20 72 65 73 75 6c 74 0a 3b 20 6f 66 20 74  he result.; of t
0480: 68 65 20 73 74 65 70 20 69 73 20 61 20 75 6e 69  he step is a uni
0490: 6f 6e 20 6f 66 20 74 68 65 20 73 65 74 73 20 72  on of the sets r
04a0: 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 6c  eturned by the l
04b0: 61 74 74 65 72 20 6c 6f 63 61 74 69 6f 6e 0a 3b  atter location.;
04c0: 20 70 61 74 68 73 2e 0a 3b 0a 3b 20 54 68 65 20   paths..;.; The 
04d0: 53 58 4d 4c 20 72 65 70 72 65 73 65 6e 74 61 74  SXML representat
04e0: 69 6f 6e 20 6f 66 20 74 68 65 20 58 4d 4c 20 49  ion of the XML I
04f0: 6e 66 6f 73 65 74 20 28 73 65 65 20 53 53 41 58  nfoset (see SSAX
0500: 2e 73 63 6d 29 20 69 73 20 72 61 74 68 65 72 0a  .scm) is rather.
0510: 3b 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 71  ; suitable for q
0520: 75 65 72 79 69 6e 67 20 61 73 20 69 74 20 69 73  uerying as it is
0530: 2e 20 42 6f 77 69 6e 67 20 74 6f 20 74 68 65 20  . Bowing to the 
0540: 58 50 61 74 68 20 73 70 65 63 69 66 69 63 61 74  XPath specificat
0550: 69 6f 6e 2c 0a 3b 20 77 65 20 77 69 6c 6c 20 72  ion,.; we will r
0560: 65 66 65 72 20 74 6f 20 53 58 4d 4c 20 69 6e 66  efer to SXML inf
0570: 6f 72 6d 61 74 69 6f 6e 20 69 74 65 6d 73 20 61  ormation items a
0580: 73 20 27 4e 6f 64 65 73 27 3a 0a 3b 20 09 3c 4e  s 'Nodes':.; .<N
0590: 6f 64 65 3e 20 3a 3a 3d 20 3c 45 6c 65 6d 65 6e  ode> ::= <Elemen
05a0: 74 3e 20 7c 20 3c 61 74 74 72 69 62 75 74 65 73  t> | <attributes
05b0: 2d 63 6f 6c 6c 3e 20 7c 20 3c 61 74 74 72 69 62  -coll> | <attrib
05c0: 3e 0a 3b 20 09 09 20 20 20 7c 20 22 74 65 78 74  >.; ..   | "text
05d0: 20 73 74 72 69 6e 67 22 20 7c 20 3c 50 49 3e 0a   string" | <PI>.
05e0: 3b 20 54 68 69 73 20 70 72 6f 64 75 63 74 69 6f  ; This productio
05f0: 6e 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 64 65  n can also be de
0600: 73 63 72 69 62 65 64 20 61 73 0a 3b 09 3c 4e 6f  scribed as.;.<No
0610: 64 65 3e 20 3a 3a 3d 20 28 6e 61 6d 65 20 2e 20  de> ::= (name . 
0620: 3c 4e 6f 64 65 73 65 74 3e 29 20 7c 20 22 74 65  <Nodeset>) | "te
0630: 78 74 20 73 74 72 69 6e 67 22 0a 3b 20 41 6e 20  xt string".; An 
0640: 28 6f 72 64 65 72 65 64 29 20 73 65 74 20 6f 66  (ordered) set of
0650: 20 6e 6f 64 65 73 20 69 73 20 6a 75 73 74 20 61   nodes is just a
0660: 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6e   list of the con
0670: 73 74 69 74 75 65 6e 74 20 6e 6f 64 65 73 3a 0a  stituent nodes:.
0680: 3b 20 09 3c 4e 6f 64 65 73 65 74 3e 20 3a 3a 3d  ; .<Nodeset> ::=
0690: 20 28 3c 4e 6f 64 65 3e 20 2e 2e 2e 29 0a 3b 20   (<Node> ...).; 
06a0: 4e 6f 64 65 73 65 74 73 2c 20 61 6e 64 20 4e 6f  Nodesets, and No
06b0: 64 65 73 20 6f 74 68 65 72 20 74 68 61 6e 20 74  des other than t
06c0: 65 78 74 20 73 74 72 69 6e 67 73 20 61 72 65 20  ext strings are 
06d0: 62 6f 74 68 20 6c 69 73 74 73 2e 20 41 0a 3b 20  both lists. A.; 
06e0: 3c 4e 6f 64 65 73 65 74 3e 20 68 6f 77 65 76 65  <Nodeset> howeve
06f0: 72 20 69 73 20 65 69 74 68 65 72 20 61 6e 20 65  r is either an e
0700: 6d 70 74 79 20 6c 69 73 74 2c 20 6f 72 20 61 20  mpty list, or a 
0710: 6c 69 73 74 20 77 68 6f 73 65 20 68 65 61 64 20  list whose head 
0720: 69 73 20 6e 6f 74 0a 3b 20 61 20 73 79 6d 62 6f  is not.; a symbo
0730: 6c 2e 20 20 41 20 73 79 6d 62 6f 6c 20 61 74 20  l.  A symbol at 
0740: 74 68 65 20 68 65 61 64 20 6f 66 20 61 20 6e 6f  the head of a no
0750: 64 65 20 69 73 20 65 69 74 68 65 72 20 61 6e 20  de is either an 
0760: 58 4d 4c 20 6e 61 6d 65 20 28 69 6e 0a 3b 20 77  XML name (in.; w
0770: 68 69 63 68 20 63 61 73 65 20 69 74 27 73 20 61  hich case it's a
0780: 20 74 61 67 20 6f 66 20 61 6e 20 58 4d 4c 20 65   tag of an XML e
0790: 6c 65 6d 65 6e 74 29 2c 20 6f 72 20 61 6e 20 61  lement), or an a
07a0: 64 6d 69 6e 69 73 74 72 61 74 69 76 65 20 6e 61  dministrative na
07b0: 6d 65 0a 3b 20 73 75 63 68 20 61 73 20 27 40 27  me.; such as '@'
07c0: 2e 20 20 54 68 69 73 20 75 6e 69 66 6f 72 6d 20  .  This uniform 
07d0: 6c 69 73 74 20 72 65 70 72 65 73 65 6e 74 61 74  list representat
07e0: 69 6f 6e 20 6d 61 6b 65 73 20 70 72 6f 63 65 73  ion makes proces
07f0: 73 69 6e 67 20 72 61 74 68 65 72 0a 3b 20 73 69  sing rather.; si
0800: 6d 70 6c 65 20 61 6e 64 20 65 6c 65 67 61 6e 74  mple and elegant
0810: 2c 20 77 68 69 6c 65 20 61 76 6f 69 64 69 6e 67  , while avoiding
0820: 20 63 6f 6e 66 75 73 69 6f 6e 2e 20 54 68 65 20   confusion. The 
0830: 6d 75 6c 74 69 2d 62 72 61 6e 63 68 20 74 72 65  multi-branch tre
0840: 65 0a 3b 20 73 74 72 75 63 74 75 72 65 20 66 6f  e.; structure fo
0850: 72 6d 65 64 20 62 79 20 74 68 65 20 6d 75 74 75  rmed by the mutu
0860: 61 6c 6c 79 2d 72 65 63 75 72 73 69 76 65 20 64  ally-recursive d
0870: 61 74 61 74 79 70 65 73 20 3c 4e 6f 64 65 3e 20  atatypes <Node> 
0880: 61 6e 64 0a 3b 20 3c 4e 6f 64 65 73 65 74 3e 20  and.; <Nodeset> 
0890: 6c 65 6e 64 73 20 69 74 73 65 6c 66 20 77 65 6c  lends itself wel
08a0: 6c 20 74 6f 20 70 72 6f 63 65 73 73 69 6e 67 20  l to processing 
08b0: 62 79 20 66 75 6e 63 74 69 6f 6e 61 6c 20 6c 61  by functional la
08c0: 6e 67 75 61 67 65 73 2e 0a 3b 0a 3b 20 41 20 6c  nguages..;.; A l
08d0: 6f 63 61 74 69 6f 6e 20 70 61 74 68 20 69 73 20  ocation path is 
08e0: 69 6e 20 66 61 63 74 20 61 20 63 6f 6d 70 6f 73  in fact a compos
08f0: 69 74 65 20 71 75 65 72 79 20 6f 76 65 72 20 61  ite query over a
0900: 6e 20 58 50 61 74 68 20 74 72 65 65 20 6f 72 0a  n XPath tree or.
0910: 3b 20 69 74 73 20 62 72 61 6e 63 68 2e 20 41 20  ; its branch. A 
0920: 73 69 6e 67 65 20 73 74 65 70 20 69 73 20 61 20  singe step is a 
0930: 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61  combination of a
0940: 20 70 72 6f 6a 65 63 74 69 6f 6e 2c 20 73 65 6c   projection, sel
0950: 65 63 74 69 6f 6e 0a 3b 20 6f 72 20 61 20 74 72  ection.; or a tr
0960: 61 6e 73 69 74 69 76 65 20 63 6c 6f 73 75 72 65  ansitive closure
0970: 2e 20 4d 75 6c 74 69 70 6c 65 20 73 74 65 70 73  . Multiple steps
0980: 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 76 69   are combined vi
0990: 61 20 6a 6f 69 6e 20 61 6e 64 0a 3b 20 75 6e 69  a join and.; uni
09a0: 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 54  on operations. T
09b0: 68 69 73 20 69 6e 73 69 67 68 74 20 61 6c 6c 6f  his insight allo
09c0: 77 73 20 75 73 20 74 6f 20 5f 65 6c 65 67 61 6e  ws us to _elegan
09d0: 74 6c 79 5f 20 69 6d 70 6c 65 6d 65 6e 74 0a 3b  tly_ implement.;
09e0: 20 58 50 61 74 68 20 61 73 20 61 20 73 65 71 75   XPath as a sequ
09f0: 65 6e 63 65 20 6f 66 20 70 72 6f 6a 65 63 74 69  ence of projecti
0a00: 6f 6e 20 61 6e 64 20 66 69 6c 74 65 72 69 6e 67  on and filtering
0a10: 20 70 72 69 6d 69 74 69 76 65 73 20 2d 2d 0a 3b   primitives --.;
0a20: 20 63 6f 6e 76 65 72 74 65 72 73 20 2d 2d 20 6a   converters -- j
0a30: 6f 69 6e 65 64 20 62 79 20 5f 63 6f 6d 62 69 6e  oined by _combin
0a40: 61 74 6f 72 73 5f 2e 20 45 61 63 68 20 63 6f 6e  ators_. Each con
0a50: 76 65 72 74 65 72 20 74 61 6b 65 73 20 61 20 6e  verter takes a n
0a60: 6f 64 65 0a 3b 20 61 6e 64 20 72 65 74 75 72 6e  ode.; and return
0a70: 73 20 61 20 6e 6f 64 65 73 65 74 20 77 68 69 63  s a nodeset whic
0a80: 68 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20  h is the result 
0a90: 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
0aa0: 64 69 6e 67 20 71 75 65 72 79 0a 3b 20 72 65 6c  ding query.; rel
0ab0: 61 74 69 76 65 20 74 6f 20 74 68 61 74 20 6e 6f  ative to that no
0ac0: 64 65 2e 20 41 20 63 6f 6e 76 65 72 74 65 72 20  de. A converter 
0ad0: 63 61 6e 20 61 6c 73 6f 20 62 65 20 63 61 6c 6c  can also be call
0ae0: 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 0a 3b  ed on a set of.;
0af0: 20 6e 6f 64 65 73 2e 20 49 6e 20 74 68 61 74 20   nodes. In that 
0b00: 63 61 73 65 20 69 74 20 72 65 74 75 72 6e 73 20  case it returns 
0b10: 61 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20 63  a union of the c
0b20: 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 71 75 65  orresponding que
0b30: 72 69 65 73 20 6f 76 65 72 0a 3b 20 65 61 63 68  ries over.; each
0b40: 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 73 65 74   node in the set
0b50: 2e 20 54 68 65 20 75 6e 69 6f 6e 20 69 73 20 65  . The union is e
0b60: 61 73 69 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65  asily implemente
0b70: 64 20 61 73 20 61 20 6c 69 73 74 0a 3b 20 61 70  d as a list.; ap
0b80: 70 65 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 61  pend operation a
0b90: 73 20 61 6c 6c 20 6e 6f 64 65 73 20 69 6e 20 61  s all nodes in a
0ba0: 20 53 58 4d 4c 20 74 72 65 65 20 61 72 65 20 63   SXML tree are c
0bb0: 6f 6e 73 69 64 65 72 65 64 0a 3b 20 64 69 73 74  onsidered.; dist
0bc0: 69 6e 63 74 2c 20 62 79 20 58 50 61 74 68 20 63  inct, by XPath c
0bd0: 6f 6e 76 65 6e 74 69 6f 6e 73 2e 20 57 65 20 61  onventions. We a
0be0: 6c 73 6f 20 70 72 65 73 65 72 76 65 20 74 68 65  lso preserve the
0bf0: 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 3b 20   order of the.; 
0c00: 6d 65 6d 62 65 72 73 20 69 6e 20 74 68 65 20 75  members in the u
0c10: 6e 69 6f 6e 2e 20 51 75 65 72 79 20 63 6f 6d 62  nion. Query comb
0c20: 69 6e 61 74 6f 72 73 20 61 72 65 20 68 69 67 68  inators are high
0c30: 2d 6f 72 64 65 72 20 66 75 6e 63 74 69 6f 6e 73  -order functions
0c40: 3a 0a 3b 20 74 68 65 79 20 74 61 6b 65 20 63 6f  :.; they take co
0c50: 6e 76 65 72 74 65 72 28 73 29 20 28 77 68 69 63  nverter(s) (whic
0c60: 68 20 69 73 20 61 20 4e 6f 64 65 7c 4e 6f 64 65  h is a Node|Node
0c70: 73 65 74 20 2d 3e 20 4e 6f 64 65 73 65 74 20 66  set -> Nodeset f
0c80: 75 6e 63 74 69 6f 6e 29 0a 3b 20 61 6e 64 20 63  unction).; and c
0c90: 6f 6d 70 6f 73 65 20 6f 72 20 6f 74 68 65 72 77  ompose or otherw
0ca0: 69 73 65 20 63 6f 6d 62 69 6e 65 20 74 68 65 6d  ise combine them
0cb0: 2e 20 57 65 20 77 69 6c 6c 20 62 65 20 63 6f 6e  . We will be con
0cc0: 63 65 72 6e 65 64 20 77 69 74 68 0a 3b 20 6f 6e  cerned with.; on
0cd0: 6c 79 20 72 65 6c 61 74 69 76 65 20 6c 6f 63 61  ly relative loca
0ce0: 74 69 6f 6e 20 70 61 74 68 73 20 5b 58 50 61 74  tion paths [XPat
0cf0: 68 5d 3a 20 61 6e 20 61 62 73 6f 6c 75 74 65 20  h]: an absolute 
0d00: 6c 6f 63 61 74 69 6f 6e 20 70 61 74 68 20 69 73  location path is
0d10: 20 61 0a 3b 20 72 65 6c 61 74 69 76 65 20 70 61   a.; relative pa
0d20: 74 68 20 61 70 70 6c 69 65 64 20 74 6f 20 74 68  th applied to th
0d30: 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 0a 3b 0a 3b  e root node..;.;
0d40: 20 53 69 6d 69 6c 61 72 6c 79 20 74 6f 20 58 50   Similarly to XP
0d50: 61 74 68 2c 20 53 58 50 61 74 68 20 64 65 66 69  ath, SXPath defi
0d60: 6e 65 73 20 66 75 6c 6c 20 61 6e 64 20 61 62 62  nes full and abb
0d70: 72 65 76 69 61 74 65 64 20 6e 6f 74 61 74 69 6f  reviated notatio
0d80: 6e 73 0a 3b 20 66 6f 72 20 6c 6f 63 61 74 69 6f  ns.; for locatio
0d90: 6e 20 70 61 74 68 73 2e 20 49 6e 20 62 6f 74 68  n paths. In both
0da0: 20 63 61 73 65 73 2c 20 74 68 65 20 61 62 62 72   cases, the abbr
0db0: 65 76 69 61 74 65 64 20 6e 6f 74 61 74 69 6f 6e  eviated notation
0dc0: 20 63 61 6e 20 62 65 0a 3b 20 6d 65 63 68 61 6e   can be.; mechan
0dd0: 69 63 61 6c 6c 79 20 65 78 70 61 6e 64 65 64 20  ically expanded 
0de0: 69 6e 74 6f 20 74 68 65 20 66 75 6c 6c 20 66 6f  into the full fo
0df0: 72 6d 20 62 79 20 73 69 6d 70 6c 65 20 72 65 77  rm by simple rew
0e00: 72 69 74 69 6e 67 0a 3b 20 72 75 6c 65 73 2e 20  riting.; rules. 
0e10: 49 6e 20 63 61 73 65 20 6f 66 20 53 58 50 61 74  In case of SXPat
0e20: 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  h the correspond
0e30: 69 6e 67 20 72 75 6c 65 73 20 61 72 65 20 67 69  ing rules are gi
0e40: 76 65 6e 20 61 73 0a 3b 20 63 6f 6d 6d 65 6e 74  ven as.; comment
0e50: 73 20 74 6f 20 61 20 73 78 70 61 74 68 20 66 75  s to a sxpath fu
0e60: 6e 63 74 69 6f 6e 2c 20 62 65 6c 6f 77 2e 20 54  nction, below. T
0e70: 68 65 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65  he regression te
0e80: 73 74 20 73 75 69 74 65 20 61 74 0a 3b 20 74 68  st suite at.; th
0e90: 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 66 69  e end of this fi
0ea0: 6c 65 20 73 68 6f 77 73 20 61 20 72 65 70 72 65  le shows a repre
0eb0: 73 65 6e 74 61 74 69 76 65 20 73 61 6d 70 6c 65  sentative sample
0ec0: 20 6f 66 20 53 58 50 61 74 68 73 20 69 6e 0a 3b   of SXPaths in.;
0ed0: 20 62 6f 74 68 20 6e 6f 74 61 74 69 6f 6e 73 2c   both notations,
0ee0: 20 6a 75 78 74 61 70 6f 73 65 64 20 77 69 74 68   juxtaposed with
0ef0: 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
0f00: 6e 67 20 58 50 61 74 68 0a 3b 20 65 78 70 72 65  ng XPath.; expre
0f10: 73 73 69 6f 6e 73 2e 20 4d 6f 73 74 20 6f 66 20  ssions. Most of 
0f20: 74 68 65 20 73 61 6d 70 6c 65 73 20 61 72 65 20  the samples are 
0f30: 62 6f 72 72 6f 77 65 64 20 6c 69 74 65 72 61 6c  borrowed literal
0f40: 6c 79 20 66 72 6f 6d 20 74 68 65 0a 3b 20 58 50  ly from the.; XP
0f50: 61 74 68 20 73 70 65 63 69 66 69 63 61 74 69 6f  ath specificatio
0f60: 6e 2c 20 77 68 69 6c 65 20 74 68 65 20 6f 74 68  n, while the oth
0f70: 65 72 73 20 61 72 65 20 61 64 6a 75 73 74 65 64  ers are adjusted
0f80: 20 66 6f 72 20 6f 75 72 20 72 75 6e 6e 69 6e 67   for our running
0f90: 0a 3b 20 65 78 61 6d 70 6c 65 2c 20 74 72 65 65  .; example, tree
0fa0: 31 2e 0a 3b 0a 3b 20 54 6f 20 64 6f 3a 0a 3b 20  1..;.; To do:.; 
0fb0: 52 65 6e 61 6d 65 20 66 69 6c 74 65 72 20 74 6f  Rename filter to
0fc0: 20 6e 6f 64 65 2d 66 69 6c 74 65 72 20 6f 72 20   node-filter or 
0fd0: 6e 73 2d 66 69 6c 74 65 72 0a 3b 20 55 73 65 20  ns-filter.; Use 
0fe0: 3b 3d 3d 3d 20 66 6f 72 20 63 68 61 70 74 65 72  ;=== for chapter
0ff0: 73 2c 20 3b 2d 2d 2d 20 66 6f 72 20 73 65 63 74  s, ;--- for sect
1000: 69 6f 6e 73 2c 20 61 6e 64 20 3b 5e 5e 5e 20 66  ions, and ;^^^ f
1010: 6f 72 20 65 6e 64 20 73 65 63 74 69 6f 6e 73 0a  or end sections.
1020: 3b 0a 3b 20 24 49 64 3a 20 53 58 50 61 74 68 2d  ;.; $Id: SXPath-
1030: 6f 6c 64 2e 73 63 6d 2c 76 20 31 2e 34 20 32 30  old.scm,v 1.4 20
1040: 30 34 2f 30 37 2f 30 37 20 31 36 3a 30 32 3a 33  04/07/07 16:02:3
1050: 31 20 73 70 65 72 62 65 72 20 45 78 70 20 24 0a  1 sperber Exp $.
1060: 0a 0a 09 3b 20 53 65 65 20 68 74 74 70 3a 2f 2f  ...; See http://
1070: 70 6f 62 6f 78 2e 63 6f 6d 2f 7e 6f 6c 65 67 2f  pobox.com/~oleg/
1080: 66 74 70 2f 53 63 68 65 6d 65 2f 6d 79 65 6e 76  ftp/Scheme/myenv
1090: 2e 73 63 6d 0a 09 3b 20 53 65 65 20 68 74 74 70  .scm..; See http
10a0: 3a 2f 2f 70 6f 62 6f 78 2e 63 6f 6d 2f 7e 6f 6c  ://pobox.com/~ol
10b0: 65 67 2f 66 74 70 2f 53 63 68 65 6d 65 2f 6d 79  eg/ftp/Scheme/my
10c0: 65 6e 76 2d 73 63 6d 2e 73 63 6d 0a 09 3b 20 53  env-scm.scm..; S
10d0: 65 65 20 68 74 74 70 3a 2f 2f 70 6f 62 6f 78 2e  ee http://pobox.
10e0: 63 6f 6d 2f 7e 6f 6c 65 67 2f 66 74 70 2f 53 63  com/~oleg/ftp/Sc
10f0: 68 65 6d 65 2f 6d 79 65 6e 76 2d 62 69 67 6c 6f  heme/myenv-biglo
1100: 6f 2e 73 63 6d 0a 3b 28 6d 6f 64 75 6c 65 20 53  o.scm.;(module S
1110: 58 50 61 74 68 0a 3b 20 20 28 69 6e 63 6c 75 64  XPath.;  (includ
1120: 65 20 22 6d 79 65 6e 76 2d 62 69 67 6c 6f 6f 2e  e "myenv-bigloo.
1130: 73 63 6d 22 29 29 09 09 3b 20 46 6f 72 20 75 73  scm"))..; For us
1140: 65 20 77 69 74 68 20 42 69 67 6c 6f 6f 20 32 2e  e with Bigloo 2.
1150: 32 62 0a 3b 28 6c 6f 61 64 20 22 6d 79 65 6e 76  2b.;(load "myenv
1160: 2d 73 63 6d 2e 73 63 6d 22 29 09 09 3b 20 46 6f  -scm.scm")..; Fo
1170: 72 20 75 73 65 20 77 69 74 68 20 53 43 4d 20 76  r use with SCM v
1180: 35 64 32 0a 3b 28 69 6e 63 6c 75 64 65 20 22 6d  5d2.;(include "m
1190: 79 65 6e 76 2e 73 63 6d 22 29 09 09 3b 20 46 6f  yenv.scm")..; Fo
11a0: 72 20 75 73 65 20 77 69 74 68 20 47 61 6d 62 69  r use with Gambi
11b0: 74 2d 43 20 33 2e 30 0a 0a 0a 0a 28 64 65 66 69  t-C 3.0....(defi
11c0: 6e 65 20 28 6e 6f 64 65 73 65 74 3f 20 78 29 0a  ne (nodeset? x).
11d0: 20 20 28 6f 72 20 28 61 6e 64 20 28 70 61 69 72    (or (and (pair
11e0: 3f 20 78 29 20 28 6e 6f 74 20 28 73 79 6d 62 6f  ? x) (not (symbo
11f0: 6c 3f 20 28 63 61 72 20 78 29 29 29 29 20 28 6e  l? (car x)))) (n
1200: 75 6c 6c 3f 20 78 29 29 29 0a 0a 3b 2d 2d 2d 2d  ull? x)))..;----
1210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1220: 2d 2d 2d 2d 2d 0a 3b 20 42 61 73 69 63 20 63 6f  -----.; Basic co
1230: 6e 76 65 72 74 65 72 73 20 61 6e 64 20 61 70 70  nverters and app
1240: 6c 69 63 61 74 6f 72 73 0a 3b 20 41 20 63 6f 6e  licators.; A con
1250: 76 65 72 74 65 72 20 69 73 20 61 20 66 75 6e 63  verter is a func
1260: 74 69 6f 6e 0a 3b 09 74 79 70 65 20 43 6f 6e 76  tion.;.type Conv
1270: 65 72 74 65 72 20 3d 20 4e 6f 64 65 7c 4e 6f 64  erter = Node|Nod
1280: 65 73 65 74 20 2d 3e 20 4e 6f 64 65 73 65 74 0a  eset -> Nodeset.
1290: 3b 20 41 20 63 6f 6e 76 65 72 74 65 72 20 63 61  ; A converter ca
12a0: 6e 20 61 6c 73 6f 20 70 6c 61 79 20 61 20 72 6f  n also play a ro
12b0: 6c 65 20 6f 66 20 61 20 70 72 65 64 69 63 61 74  le of a predicat
12c0: 65 3a 20 69 6e 20 74 68 61 74 20 63 61 73 65 2c  e: in that case,
12d0: 20 69 66 20 61 0a 3b 20 63 6f 6e 76 65 72 74 65   if a.; converte
12e0: 72 2c 20 61 70 70 6c 69 65 64 20 74 6f 20 61 20  r, applied to a 
12f0: 6e 6f 64 65 20 6f 72 20 61 20 6e 6f 64 65 73 65  node or a nodese
1300: 74 2c 20 79 69 65 6c 64 73 20 61 20 6e 6f 6e 2d  t, yields a non-
1310: 65 6d 70 74 79 0a 3b 20 6e 6f 64 65 73 65 74 2c  empty.; nodeset,
1320: 20 74 68 65 20 63 6f 6e 76 65 72 74 65 72 2d 70   the converter-p
1330: 72 65 64 69 63 61 74 65 20 69 73 20 64 65 65 6d  redicate is deem
1340: 65 64 20 73 61 74 69 73 66 69 65 64 2e 20 54 68  ed satisfied. Th
1350: 72 6f 75 67 68 6f 75 74 0a 3b 20 74 68 69 73 20  roughout.; this 
1360: 66 69 6c 65 20 61 20 6e 69 6c 20 6e 6f 64 65 73  file a nil nodes
1370: 65 74 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  et is equivalent
1380: 20 74 6f 20 23 66 20 69 6e 20 64 65 6e 6f 74 69   to #f in denoti
1390: 6e 67 20 61 20 66 61 69 6c 75 72 65 2e 0a 0a 3b  ng a failure...;
13a0: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
13b0: 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
13c0: 74 73 20 61 20 27 4e 6f 64 65 20 74 65 73 74 27  ts a 'Node test'
13d0: 20 61 73 20 64 65 66 69 6e 65 64 20 69 6e 0a 3b   as defined in.;
13e0: 20 53 65 63 2e 20 32 2e 33 20 6f 66 20 58 50 61   Sec. 2.3 of XPa
13f0: 74 68 20 64 6f 63 75 6d 65 6e 74 2e 20 41 20 6e  th document. A n
1400: 6f 64 65 20 74 65 73 74 20 69 73 20 6f 6e 65 20  ode test is one 
1410: 6f 66 20 74 68 65 20 63 6f 6d 70 6f 6e 65 6e 74  of the component
1420: 73 20 6f 66 20 61 0a 3b 20 6c 6f 63 61 74 69 6f  s of a.; locatio
1430: 6e 20 73 74 65 70 2e 20 49 74 20 69 73 20 61 6c  n step. It is al
1440: 73 6f 20 61 20 63 6f 6e 76 65 72 74 65 72 2d 70  so a converter-p
1450: 72 65 64 69 63 61 74 65 20 69 6e 20 53 58 50 61  redicate in SXPa
1460: 74 68 2e 0a 3b 0a 3b 20 54 68 65 20 66 75 6e 63  th..;.; The func
1470: 74 69 6f 6e 20 6e 6f 64 65 2d 74 79 70 65 6f 66  tion node-typeof
1480: 3f 20 74 61 6b 65 73 20 61 20 74 79 70 65 20 63  ? takes a type c
1490: 72 69 74 65 72 69 6f 6e 20 61 6e 64 20 72 65 74  riterion and ret
14a0: 75 72 6e 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c  urns a function,
14b0: 0a 3b 20 77 68 69 63 68 2c 20 77 68 65 6e 20 61  .; which, when a
14c0: 70 70 6c 69 65 64 20 74 6f 20 61 20 6e 6f 64 65  pplied to a node
14d0: 2c 20 77 69 6c 6c 20 74 65 6c 6c 20 69 66 20 74  , will tell if t
14e0: 68 65 20 6e 6f 64 65 20 73 61 74 69 73 66 69 65  he node satisfie
14f0: 73 0a 3b 20 74 68 65 20 74 65 73 74 2e 0a 3b 09  s.; the test..;.
1500: 6e 6f 64 65 2d 74 79 70 65 6f 66 3f 20 3a 3a 20  node-typeof? :: 
1510: 43 72 69 74 20 2d 3e 20 4e 6f 64 65 20 2d 3e 20  Crit -> Node -> 
1520: 42 6f 6f 6c 65 61 6e 0a 3b 0a 3b 20 54 68 65 20  Boolean.;.; The 
1530: 63 72 69 74 65 72 69 6f 6e 20 27 63 72 69 74 27  criterion 'crit'
1540: 20 69 73 20 61 20 73 79 6d 62 6f 6c 2c 20 6f 6e   is a symbol, on
1550: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1560: 6e 67 3a 0a 3b 09 69 64 09 09 2d 20 74 65 73 74  ng:.;.id..- test
1570: 73 20 69 66 20 74 68 65 20 4e 6f 64 65 20 68 61  s if the Node ha
1580: 73 20 74 68 65 20 72 69 67 68 74 20 6e 61 6d 65  s the right name
1590: 20 28 69 64 29 0a 3b 09 40 09 09 2d 20 74 65 73   (id).;.@..- tes
15a0: 74 73 20 69 66 20 74 68 65 20 4e 6f 64 65 20 69  ts if the Node i
15b0: 73 20 61 6e 20 3c 61 74 74 72 69 62 75 74 65 73  s an <attributes
15c0: 2d 63 6f 6c 6c 3e 0a 3b 09 2a 09 09 2d 20 74 65  -coll>.;.*..- te
15d0: 73 74 73 20 69 66 20 74 68 65 20 4e 6f 64 65 20  sts if the Node 
15e0: 69 73 20 61 6e 20 3c 45 6c 65 6d 65 6e 74 3e 0a  is an <Element>.
15f0: 3b 09 2a 74 65 78 74 2a 09 09 2d 20 74 65 73 74  ;.*text*..- test
1600: 73 20 69 66 20 74 68 65 20 4e 6f 64 65 20 69 73  s if the Node is
1610: 20 61 20 74 65 78 74 20 6e 6f 64 65 0a 3b 09 2a   a text node.;.*
1620: 50 49 2a 09 09 2d 20 74 65 73 74 73 20 69 66 20  PI*..- tests if 
1630: 74 68 65 20 4e 6f 64 65 20 69 73 20 61 20 50 49  the Node is a PI
1640: 20 6e 6f 64 65 0a 3b 09 2a 61 6e 79 2a 09 09 2d   node.;.*any*..-
1650: 20 23 74 20 66 6f 72 20 61 6e 79 20 74 79 70 65   #t for any type
1660: 20 6f 66 20 4e 6f 64 65 0a 0a 28 64 65 66 69 6e   of Node..(defin
1670: 65 20 28 6e 6f 64 65 2d 74 79 70 65 6f 66 3f 20  e (node-typeof? 
1680: 63 72 69 74 29 0a 20 20 28 6c 61 6d 62 64 61 20  crit).  (lambda 
1690: 28 6e 6f 64 65 29 0a 20 20 20 20 28 63 61 73 65  (node).    (case
16a0: 20 63 72 69 74 0a 20 20 20 20 20 20 28 28 2a 29   crit.      ((*)
16b0: 20 28 61 6e 64 20 28 70 61 69 72 3f 20 6e 6f 64   (and (pair? nod
16c0: 65 29 20 28 6e 6f 74 20 28 6d 65 6d 71 20 28 63  e) (not (memq (c
16d0: 61 72 20 6e 6f 64 65 29 20 27 28 40 20 2a 50 49  ar node) '(@ *PI
16e0: 2a 29 29 29 29 29 0a 20 20 20 20 20 20 28 28 2a  *))))).      ((*
16f0: 61 6e 79 2a 29 20 23 74 29 0a 20 20 20 20 20 20  any*) #t).      
1700: 28 28 2a 74 65 78 74 2a 29 20 28 73 74 72 69 6e  ((*text*) (strin
1710: 67 3f 20 6e 6f 64 65 29 29 0a 20 20 20 20 20 20  g? node)).      
1720: 28 65 6c 73 65 0a 20 20 20 20 20 20 20 28 61 6e  (else.       (an
1730: 64 20 28 70 61 69 72 3f 20 6e 6f 64 65 29 20 28  d (pair? node) (
1740: 65 71 3f 20 63 72 69 74 20 28 63 61 72 20 6e 6f  eq? crit (car no
1750: 64 65 29 29 29 29 0a 29 29 29 0a 0a 0a 3b 20 43  de)))).)))...; C
1760: 75 72 72 69 65 64 20 65 71 75 69 76 61 6c 65 6e  urried equivalen
1770: 63 65 20 63 6f 6e 76 65 72 74 65 72 2d 70 72 65  ce converter-pre
1780: 64 69 63 61 74 65 73 0a 28 64 65 66 69 6e 65 20  dicates.(define 
1790: 28 6e 6f 64 65 2d 65 71 3f 20 6f 74 68 65 72 29  (node-eq? other)
17a0: 0a 20 20 28 6c 61 6d 62 64 61 20 28 6e 6f 64 65  .  (lambda (node
17b0: 29 0a 20 20 20 20 28 65 71 3f 20 6f 74 68 65 72  ).    (eq? other
17c0: 20 6e 6f 64 65 29 29 29 0a 0a 28 64 65 66 69 6e   node)))..(defin
17d0: 65 20 28 6e 6f 64 65 2d 65 71 75 61 6c 3f 20 6f  e (node-equal? o
17e0: 74 68 65 72 29 0a 20 20 28 6c 61 6d 62 64 61 20  ther).  (lambda 
17f0: 28 6e 6f 64 65 29 0a 20 20 20 20 28 65 71 75 61  (node).    (equa
1800: 6c 3f 20 6f 74 68 65 72 20 6e 6f 64 65 29 29 29  l? other node)))
1810: 0a 0a 3b 20 6e 6f 64 65 2d 70 6f 73 3a 3a 20 4e  ..; node-pos:: N
1820: 20 2d 3e 20 4e 6f 64 65 73 65 74 20 2d 3e 20 4e   -> Nodeset -> N
1830: 6f 64 65 73 65 74 2c 20 6f 72 0a 3b 20 6e 6f 64  odeset, or.; nod
1840: 65 2d 70 6f 73 3a 3a 20 4e 20 2d 3e 20 43 6f 6e  e-pos:: N -> Con
1850: 76 65 72 74 65 72 0a 3b 20 53 65 6c 65 63 74 20  verter.; Select 
1860: 74 68 65 20 4e 27 74 68 20 65 6c 65 6d 65 6e 74  the N'th element
1870: 20 6f 66 20 61 20 4e 6f 64 65 73 65 74 20 61 6e   of a Nodeset an
1880: 64 20 72 65 74 75 72 6e 20 61 73 20 61 20 73 69  d return as a si
1890: 6e 67 75 6c 61 72 20 4e 6f 64 65 73 65 74 3b 0a  ngular Nodeset;.
18a0: 3b 20 52 65 74 75 72 6e 20 61 6e 20 65 6d 70 74  ; Return an empt
18b0: 79 20 6e 6f 64 65 73 65 74 20 69 66 20 74 68 65  y nodeset if the
18c0: 20 4e 74 68 20 65 6c 65 6d 65 6e 74 20 64 6f 65   Nth element doe
18d0: 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 3b 20 28  s not exist..; (
18e0: 28 6e 6f 64 65 2d 70 6f 73 20 31 29 20 4e 6f 64  (node-pos 1) Nod
18f0: 65 73 65 74 29 20 73 65 6c 65 63 74 73 20 74 68  eset) selects th
1900: 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20 68 65  e node at the he
1910: 61 64 20 6f 66 20 74 68 65 20 4e 6f 64 65 73 65  ad of the Nodese
1920: 74 2c 0a 3b 20 69 66 20 65 78 69 73 74 73 3b 20  t,.; if exists; 
1930: 28 28 6e 6f 64 65 2d 70 6f 73 20 32 29 20 4e 6f  ((node-pos 2) No
1940: 64 65 73 65 74 29 20 73 65 6c 65 63 74 73 20 74  deset) selects t
1950: 68 65 20 4e 6f 64 65 20 61 66 74 65 72 20 74 68  he Node after th
1960: 61 74 2c 20 69 66 0a 3b 20 65 78 69 73 74 73 2e  at, if.; exists.
1970: 0a 3b 20 4e 20 63 61 6e 20 61 6c 73 6f 20 62 65  .; N can also be
1980: 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
1990: 65 72 3a 20 69 6e 20 74 68 61 74 20 63 61 73 65  er: in that case
19a0: 20 74 68 65 20 6e 6f 64 65 20 69 73 20 70 69 63   the node is pic
19b0: 6b 65 64 20 66 72 6f 6d 0a 3b 20 74 68 65 20 74  ked from.; the t
19c0: 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 2e  ail of the list.
19d0: 0a 3b 20 28 28 6e 6f 64 65 2d 70 6f 73 20 2d 31  .; ((node-pos -1
19e0: 29 20 4e 6f 64 65 73 65 74 29 20 73 65 6c 65 63  ) Nodeset) selec
19f0: 74 73 20 74 68 65 20 6c 61 73 74 20 6e 6f 64 65  ts the last node
1a00: 20 6f 66 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20   of a non-empty 
1a10: 6e 6f 64 65 73 65 74 3b 0a 3b 20 28 28 6e 6f 64  nodeset;.; ((nod
1a20: 65 2d 70 6f 73 20 2d 32 29 20 4e 6f 64 65 73 65  e-pos -2) Nodese
1a30: 74 29 20 73 65 6c 65 63 74 73 20 74 68 65 20 6c  t) selects the l
1a40: 61 73 74 20 62 75 74 20 6f 6e 65 20 6e 6f 64 65  ast but one node
1a50: 2c 20 69 66 20 65 78 69 73 74 73 2e 0a 0a 28 64  , if exists...(d
1a60: 65 66 69 6e 65 20 28 6e 6f 64 65 2d 70 6f 73 20  efine (node-pos 
1a70: 6e 29 0a 20 20 28 6c 61 6d 62 64 61 20 28 6e 6f  n).  (lambda (no
1a80: 64 65 73 65 74 29 0a 20 20 20 20 28 63 6f 6e 64  deset).    (cond
1a90: 0a 20 20 20 20 20 28 28 6e 6f 74 20 28 6e 6f 64  .     ((not (nod
1aa0: 65 73 65 74 3f 20 6e 6f 64 65 73 65 74 29 29 20  eset? nodeset)) 
1ab0: 27 28 29 29 0a 20 20 20 20 20 28 28 6e 75 6c 6c  '()).     ((null
1ac0: 3f 20 6e 6f 64 65 73 65 74 29 20 6e 6f 64 65 73  ? nodeset) nodes
1ad0: 65 74 29 0a 20 20 20 20 20 28 28 65 71 76 3f 20  et).     ((eqv? 
1ae0: 6e 20 31 29 20 28 6c 69 73 74 20 28 63 61 72 20  n 1) (list (car 
1af0: 6e 6f 64 65 73 65 74 29 29 29 0a 20 20 20 20 20  nodeset))).     
1b00: 28 28 6e 65 67 61 74 69 76 65 3f 20 6e 29 20 28  ((negative? n) (
1b10: 28 6e 6f 64 65 2d 70 6f 73 20 28 2b 20 6e 20 31  (node-pos (+ n 1
1b20: 20 28 6c 65 6e 67 74 68 20 6e 6f 64 65 73 65 74   (length nodeset
1b30: 29 29 29 20 6e 6f 64 65 73 65 74 29 29 0a 20 20  ))) nodeset)).  
1b40: 20 20 20 28 65 6c 73 65 0a 20 20 20 20 20 20 28     (else.      (
1b50: 61 73 73 65 72 74 20 28 70 6f 73 69 74 69 76 65  assert (positive
1b60: 3f 20 6e 29 29 0a 20 20 20 20 20 20 28 28 6e 6f  ? n)).      ((no
1b70: 64 65 2d 70 6f 73 20 28 64 65 63 20 6e 29 29 20  de-pos (dec n)) 
1b80: 28 63 64 72 20 6e 6f 64 65 73 65 74 29 29 29 29  (cdr nodeset))))
1b90: 29 29 0a 0a 3b 20 66 69 6c 74 65 72 3a 3a 20 43  ))..; filter:: C
1ba0: 6f 6e 76 65 72 74 65 72 20 2d 3e 20 43 6f 6e 76  onverter -> Conv
1bb0: 65 72 74 65 72 0a 3b 20 41 20 66 69 6c 74 65 72  erter.; A filter
1bc0: 20 61 70 70 6c 69 63 61 74 6f 72 2c 20 77 68 69   applicator, whi
1bd0: 63 68 20 69 6e 74 72 6f 64 75 63 65 73 20 61 20  ch introduces a 
1be0: 66 69 6c 74 65 72 69 6e 67 20 63 6f 6e 74 65 78  filtering contex
1bf0: 74 2e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a  t. The argument.
1c00: 3b 20 63 6f 6e 76 65 72 74 65 72 20 69 73 20 63  ; converter is c
1c10: 6f 6e 73 69 64 65 72 65 64 20 61 20 70 72 65 64  onsidered a pred
1c20: 69 63 61 74 65 2c 20 77 69 74 68 20 65 69 74 68  icate, with eith
1c30: 65 72 20 23 66 20 6f 72 20 6e 69 6c 20 72 65 73  er #f or nil res
1c40: 75 6c 74 20 6d 65 61 6e 69 6e 67 0a 3b 20 66 61  ult meaning.; fa
1c50: 69 6c 75 72 65 2e 0a 28 64 65 66 69 6e 65 20 28  ilure..(define (
1c60: 66 69 6c 74 65 72 20 70 72 65 64 3f 29 0a 20 20  filter pred?).  
1c70: 28 6c 61 6d 62 64 61 20 28 6c 73 74 29 09 3b 20  (lambda (lst).; 
1c80: 61 20 6e 6f 64 65 73 65 74 20 6f 72 20 61 20 6e  a nodeset or a n
1c90: 6f 64 65 20 28 77 69 6c 6c 20 62 65 20 63 6f 6e  ode (will be con
1ca0: 76 65 72 74 65 64 20 74 6f 20 61 20 73 69 6e 67  verted to a sing
1cb0: 6c 65 74 6f 6e 20 6e 73 65 74 29 0a 20 20 20 20  leton nset).    
1cc0: 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6c 73 74 20  (let loop ((lst 
1cd0: 28 69 66 20 28 6e 6f 64 65 73 65 74 3f 20 6c 73  (if (nodeset? ls
1ce0: 74 29 20 6c 73 74 20 28 6c 69 73 74 20 6c 73 74  t) lst (list lst
1cf0: 29 29 29 20 28 72 65 73 20 27 28 29 29 29 0a 20  ))) (res '())). 
1d00: 20 20 20 20 20 28 69 66 20 28 6e 75 6c 6c 3f 20       (if (null? 
1d10: 6c 73 74 29 0a 09 20 20 28 72 65 76 65 72 73 65  lst)..  (reverse
1d20: 20 72 65 73 29 0a 09 20 20 28 6c 65 74 20 28 28   res)..  (let ((
1d30: 70 72 65 64 2d 72 65 73 75 6c 74 20 28 70 72 65  pred-result (pre
1d40: 64 3f 20 28 63 61 72 20 6c 73 74 29 29 29 29 0a  d? (car lst)))).
1d50: 09 20 20 20 20 28 6c 6f 6f 70 20 28 63 64 72 20  .    (loop (cdr 
1d60: 6c 73 74 29 0a 09 09 20 20 28 69 66 20 28 61 6e  lst)...  (if (an
1d70: 64 20 70 72 65 64 2d 72 65 73 75 6c 74 20 28 6e  d pred-result (n
1d80: 6f 74 20 28 6e 75 6c 6c 3f 20 70 72 65 64 2d 72  ot (null? pred-r
1d90: 65 73 75 6c 74 29 29 29 0a 09 09 20 20 20 20 20  esult)))...     
1da0: 20 28 63 6f 6e 73 20 28 63 61 72 20 6c 73 74 29   (cons (car lst)
1db0: 20 72 65 73 29 0a 09 09 20 20 20 20 20 20 72 65   res)...      re
1dc0: 73 29 29 29 29 29 29 29 0a 0a 3b 20 74 61 6b 65  s)))))))..; take
1dd0: 2d 75 6e 74 69 6c 3a 3a 20 43 6f 6e 76 65 72 74  -until:: Convert
1de0: 65 72 20 2d 3e 20 43 6f 6e 76 65 72 74 65 72 2c  er -> Converter,
1df0: 20 6f 72 0a 3b 20 74 61 6b 65 2d 75 6e 74 69 6c   or.; take-until
1e00: 3a 3a 20 50 72 65 64 20 2d 3e 20 4e 6f 64 65 7c  :: Pred -> Node|
1e10: 4e 6f 64 65 73 65 74 20 2d 3e 20 4e 6f 64 65 73  Nodeset -> Nodes
1e20: 65 74 0a 3b 20 47 69 76 65 6e 20 61 20 63 6f 6e  et.; Given a con
1e30: 76 65 72 74 65 72 2d 70 72 65 64 69 63 61 74 65  verter-predicate
1e40: 20 61 6e 64 20 61 20 6e 6f 64 65 73 65 74 2c 20   and a nodeset, 
1e50: 61 70 70 6c 79 20 74 68 65 20 70 72 65 64 69 63  apply the predic
1e60: 61 74 65 20 74 6f 0a 3b 20 65 61 63 68 20 65 6c  ate to.; each el
1e70: 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6e 6f 64  ement of the nod
1e80: 65 73 65 74 2c 20 75 6e 74 69 6c 20 74 68 65 20  eset, until the 
1e90: 70 72 65 64 69 63 61 74 65 20 79 69 65 6c 64 73  predicate yields
1ea0: 20 61 6e 79 74 68 69 6e 67 20 62 75 74 20 23 66   anything but #f
1eb0: 20 6f 72 0a 3b 20 6e 69 6c 2e 20 52 65 74 75 72   or.; nil. Retur
1ec0: 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f  n the elements o
1ed0: 66 20 74 68 65 20 69 6e 70 75 74 20 6e 6f 64 65  f the input node
1ee0: 73 65 74 20 74 68 61 74 20 68 61 76 65 20 62 65  set that have be
1ef0: 65 6e 20 70 72 6f 63 65 73 73 65 64 0a 3b 20 74  en processed.; t
1f00: 69 6c 6c 20 74 68 61 74 20 6d 6f 6d 65 6e 74 20  ill that moment 
1f10: 28 74 68 61 74 20 69 73 2c 20 77 68 69 63 68 20  (that is, which 
1f20: 66 61 69 6c 20 74 68 65 20 70 72 65 64 69 63 61  fail the predica
1f30: 74 65 29 2e 0a 3b 20 74 61 6b 65 2d 75 6e 74 69  te)..; take-unti
1f40: 6c 20 69 73 20 61 20 76 61 72 69 61 74 69 6f 6e  l is a variation
1f50: 20 6f 66 20 74 68 65 20 66 69 6c 74 65 72 20 61   of the filter a
1f60: 62 6f 76 65 3a 20 74 61 6b 65 2d 75 6e 74 69 6c  bove: take-until
1f70: 20 70 61 73 73 65 73 0a 3b 20 65 6c 65 6d 65 6e   passes.; elemen
1f80: 74 73 20 6f 66 20 61 6e 20 6f 72 64 65 72 65 64  ts of an ordered
1f90: 20 69 6e 70 75 74 20 73 65 74 20 74 69 6c 6c 20   input set till 
1fa0: 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69  (but not includi
1fb0: 6e 67 29 20 74 68 65 20 66 69 72 73 74 0a 3b 20  ng) the first.; 
1fc0: 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 73 61 74  element that sat
1fd0: 69 73 66 69 65 73 20 74 68 65 20 70 72 65 64 69  isfies the predi
1fe0: 63 61 74 65 2e 0a 3b 20 54 68 65 20 6e 6f 64 65  cate..; The node
1ff0: 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20  set returned by 
2000: 28 28 74 61 6b 65 2d 75 6e 74 69 6c 20 28 6e 6f  ((take-until (no
2010: 74 20 70 72 65 64 29 29 20 6e 73 65 74 29 20 69  t pred)) nset) i
2020: 73 20 61 20 73 75 62 73 65 74 20 2d 2d 20 0a 3b  s a subset -- .;
2030: 20 74 6f 20 62 65 20 6d 6f 72 65 20 70 72 65 63   to be more prec
2040: 69 73 65 2c 20 61 20 70 72 65 66 69 78 20 2d 2d  ise, a prefix --
2050: 20 6f 66 20 74 68 65 20 6e 6f 64 65 73 65 74 20   of the nodeset 
2060: 72 65 74 75 72 6e 65 64 20 62 79 0a 3b 20 28 28  returned by.; ((
2070: 66 69 6c 74 65 72 20 70 72 65 64 29 20 6e 73 65  filter pred) nse
2080: 74 29 0a 0a 28 64 65 66 69 6e 65 20 28 74 61 6b  t)..(define (tak
2090: 65 2d 75 6e 74 69 6c 20 70 72 65 64 3f 29 0a 20  e-until pred?). 
20a0: 20 28 6c 61 6d 62 64 61 20 28 6c 73 74 29 09 3b   (lambda (lst).;
20b0: 20 61 20 6e 6f 64 65 73 65 74 20 6f 72 20 61 20   a nodeset or a 
20c0: 6e 6f 64 65 20 28 77 69 6c 6c 20 62 65 20 63 6f  node (will be co
20d0: 6e 76 65 72 74 65 64 20 74 6f 20 61 20 73 69 6e  nverted to a sin
20e0: 67 6c 65 74 6f 6e 20 6e 73 65 74 29 0a 20 20 20  gleton nset).   
20f0: 20 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6c 73 74   (let loop ((lst
2100: 20 28 69 66 20 28 6e 6f 64 65 73 65 74 3f 20 6c   (if (nodeset? l
2110: 73 74 29 20 6c 73 74 20 28 6c 69 73 74 20 6c 73  st) lst (list ls
2120: 74 29 29 29 29 0a 20 20 20 20 20 20 28 69 66 20  t)))).      (if 
2130: 28 6e 75 6c 6c 3f 20 6c 73 74 29 20 6c 73 74 0a  (null? lst) lst.
2140: 09 20 20 28 6c 65 74 20 28 28 70 72 65 64 2d 72  .  (let ((pred-r
2150: 65 73 75 6c 74 20 28 70 72 65 64 3f 20 28 63 61  esult (pred? (ca
2160: 72 20 6c 73 74 29 29 29 29 0a 09 20 20 20 20 28  r lst))))..    (
2170: 69 66 20 28 61 6e 64 20 70 72 65 64 2d 72 65 73  if (and pred-res
2180: 75 6c 74 20 28 6e 6f 74 20 28 6e 75 6c 6c 3f 20  ult (not (null? 
2190: 70 72 65 64 2d 72 65 73 75 6c 74 29 29 29 0a 09  pred-result)))..
21a0: 09 27 28 29 0a 09 09 28 63 6f 6e 73 20 28 63 61  .'()...(cons (ca
21b0: 72 20 6c 73 74 29 20 28 6c 6f 6f 70 20 28 63 64  r lst) (loop (cd
21c0: 72 20 6c 73 74 29 29 29 29 29 0a 09 20 20 29 29  r lst)))))..  ))
21d0: 29 29 0a 0a 0a 3b 20 74 61 6b 65 2d 61 66 74 65  ))...; take-afte
21e0: 72 3a 3a 20 43 6f 6e 76 65 72 74 65 72 20 2d 3e  r:: Converter ->
21f0: 20 43 6f 6e 76 65 72 74 65 72 2c 20 6f 72 0a 3b   Converter, or.;
2200: 20 74 61 6b 65 2d 61 66 74 65 72 3a 3a 20 50 72   take-after:: Pr
2210: 65 64 20 2d 3e 20 4e 6f 64 65 7c 4e 6f 64 65 73  ed -> Node|Nodes
2220: 65 74 20 2d 3e 20 4e 6f 64 65 73 65 74 0a 3b 20  et -> Nodeset.; 
2230: 47 69 76 65 6e 20 61 20 63 6f 6e 76 65 72 74 65  Given a converte
2240: 72 2d 70 72 65 64 69 63 61 74 65 20 61 6e 64 20  r-predicate and 
2250: 61 20 6e 6f 64 65 73 65 74 2c 20 61 70 70 6c 79  a nodeset, apply
2260: 20 74 68 65 20 70 72 65 64 69 63 61 74 65 20 74   the predicate t
2270: 6f 0a 3b 20 65 61 63 68 20 65 6c 65 6d 65 6e 74  o.; each element
2280: 20 6f 66 20 74 68 65 20 6e 6f 64 65 73 65 74 2c   of the nodeset,
2290: 20 75 6e 74 69 6c 20 74 68 65 20 70 72 65 64 69   until the predi
22a0: 63 61 74 65 20 79 69 65 6c 64 73 20 61 6e 79 74  cate yields anyt
22b0: 68 69 6e 67 20 62 75 74 20 23 66 20 6f 72 0a 3b  hing but #f or.;
22c0: 20 6e 69 6c 2e 20 52 65 74 75 72 6e 20 74 68 65   nil. Return the
22d0: 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
22e0: 20 69 6e 70 75 74 20 6e 6f 64 65 73 65 74 20 74   input nodeset t
22f0: 68 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65  hat have not bee
2300: 6e 20 70 72 6f 63 65 73 73 65 64 3a 0a 3b 20 74  n processed:.; t
2310: 68 61 74 20 69 73 2c 20 72 65 74 75 72 6e 20 74  hat is, return t
2320: 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  he elements of t
2330: 68 65 20 69 6e 70 75 74 20 6e 6f 64 65 73 65 74  he input nodeset
2340: 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65   that follow the
2350: 20 66 69 72 73 74 0a 3b 20 65 6c 65 6d 65 6e 74   first.; element
2360: 20 74 68 61 74 20 73 61 74 69 73 66 69 65 64 20   that satisfied 
2370: 74 68 65 20 70 72 65 64 69 63 61 74 65 2e 0a 3b  the predicate..;
2380: 20 74 61 6b 65 2d 61 66 74 65 72 20 61 6c 6f 6e   take-after alon
2390: 67 20 77 69 74 68 20 74 61 6b 65 2d 75 6e 74 69  g with take-unti
23a0: 6c 20 70 61 72 74 69 74 69 6f 6e 20 61 6e 20 69  l partition an i
23b0: 6e 70 75 74 20 6e 6f 64 65 73 65 74 20 69 6e 74  nput nodeset int
23c0: 6f 20 74 68 72 65 65 0a 3b 20 70 61 72 74 73 3a  o three.; parts:
23d0: 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
23e0: 6e 74 20 74 68 61 74 20 73 61 74 69 73 66 69 65  nt that satisfie
23f0: 73 20 61 20 70 72 65 64 69 63 61 74 65 2c 20 61  s a predicate, a
2400: 6c 6c 20 70 72 65 63 65 64 69 6e 67 0a 3b 20 65  ll preceding.; e
2410: 6c 65 6d 65 6e 74 73 20 61 6e 64 20 61 6c 6c 20  lements and all 
2420: 66 6f 6c 6c 6f 77 69 6e 67 20 65 6c 65 6d 65 6e  following elemen
2430: 74 73 2e 0a 0a 28 64 65 66 69 6e 65 20 28 74 61  ts...(define (ta
2440: 6b 65 2d 61 66 74 65 72 20 70 72 65 64 3f 29 0a  ke-after pred?).
2450: 20 20 28 6c 61 6d 62 64 61 20 28 6c 73 74 29 09    (lambda (lst).
2460: 3b 20 61 20 6e 6f 64 65 73 65 74 20 6f 72 20 61  ; a nodeset or a
2470: 20 6e 6f 64 65 20 28 77 69 6c 6c 20 62 65 20 63   node (will be c
2480: 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 73 69  onverted to a si
2490: 6e 67 6c 65 74 6f 6e 20 6e 73 65 74 29 0a 20 20  ngleton nset).  
24a0: 20 20 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6c 73    (let loop ((ls
24b0: 74 20 28 69 66 20 28 6e 6f 64 65 73 65 74 3f 20  t (if (nodeset? 
24c0: 6c 73 74 29 20 6c 73 74 20 28 6c 69 73 74 20 6c  lst) lst (list l
24d0: 73 74 29 29 29 29 0a 20 20 20 20 20 20 28 69 66  st)))).      (if
24e0: 20 28 6e 75 6c 6c 3f 20 6c 73 74 29 20 6c 73 74   (null? lst) lst
24f0: 0a 09 20 20 28 6c 65 74 20 28 28 70 72 65 64 2d  ..  (let ((pred-
2500: 72 65 73 75 6c 74 20 28 70 72 65 64 3f 20 28 63  result (pred? (c
2510: 61 72 20 6c 73 74 29 29 29 29 0a 09 20 20 20 20  ar lst))))..    
2520: 28 69 66 20 28 61 6e 64 20 70 72 65 64 2d 72 65  (if (and pred-re
2530: 73 75 6c 74 20 28 6e 6f 74 20 28 6e 75 6c 6c 3f  sult (not (null?
2540: 20 70 72 65 64 2d 72 65 73 75 6c 74 29 29 29 0a   pred-result))).
2550: 09 09 28 63 64 72 20 6c 73 74 29 0a 09 09 28 6c  ..(cdr lst)...(l
2560: 6f 6f 70 20 28 63 64 72 20 6c 73 74 29 29 29 29  oop (cdr lst))))
2570: 0a 09 20 20 29 29 29 29 0a 0a 3b 20 41 70 70 6c  ..  ))))..; Appl
2580: 79 20 70 72 6f 63 20 74 6f 20 65 61 63 68 20 65  y proc to each e
2590: 6c 65 6d 65 6e 74 20 6f 66 20 6c 73 74 20 61 6e  lement of lst an
25a0: 64 20 72 65 74 75 72 6e 20 74 68 65 20 6c 69 73  d return the lis
25b0: 74 20 6f 66 20 72 65 73 75 6c 74 73 2e 0a 3b 20  t of results..; 
25c0: 69 66 20 70 72 6f 63 20 72 65 74 75 72 6e 73 20  if proc returns 
25d0: 61 20 6e 6f 64 65 73 65 74 2c 20 73 70 6c 69 63  a nodeset, splic
25e0: 65 20 69 74 20 69 6e 74 6f 20 74 68 65 20 72 65  e it into the re
25f0: 73 75 6c 74 0a 3b 0a 3b 20 46 72 6f 6d 20 61 6e  sult.;.; From an
2600: 6f 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76  other point of v
2610: 69 65 77 2c 20 6d 61 70 2d 75 6e 69 6f 6e 20 69  iew, map-union i
2620: 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 43 6f 6e  s a function Con
2630: 76 65 72 74 65 72 2d 3e 43 6f 6e 76 65 72 74 65  verter->Converte
2640: 72 2c 0a 3b 20 77 68 69 63 68 20 70 6c 61 63 65  r,.; which place
2650: 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2d 63 6f  s an argument-co
2660: 6e 76 65 72 74 65 72 20 69 6e 20 61 20 6a 6f 69  nverter in a joi
2670: 6e 69 6e 67 20 63 6f 6e 74 65 78 74 2e 0a 0a 28  ning context...(
2680: 64 65 66 69 6e 65 20 28 6d 61 70 2d 75 6e 69 6f  define (map-unio
2690: 6e 20 70 72 6f 63 20 6c 73 74 29 0a 20 20 28 69  n proc lst).  (i
26a0: 66 20 28 6e 75 6c 6c 3f 20 6c 73 74 29 20 6c 73  f (null? lst) ls
26b0: 74 0a 20 20 20 20 20 20 28 6c 65 74 20 28 28 70  t.      (let ((p
26c0: 72 6f 63 2d 72 65 73 20 28 70 72 6f 63 20 28 63  roc-res (proc (c
26d0: 61 72 20 6c 73 74 29 29 29 29 0a 09 28 28 69 66  ar lst))))..((if
26e0: 20 28 6e 6f 64 65 73 65 74 3f 20 70 72 6f 63 2d   (nodeset? proc-
26f0: 72 65 73 29 20 61 70 70 65 6e 64 20 63 6f 6e 73  res) append cons
2700: 29 0a 09 20 70 72 6f 63 2d 72 65 73 20 28 6d 61  ).. proc-res (ma
2710: 70 2d 75 6e 69 6f 6e 20 70 72 6f 63 20 28 63 64  p-union proc (cd
2720: 72 20 6c 73 74 29 29 29 29 29 29 0a 0a 3b 20 6e  r lst))))))..; n
2730: 6f 64 65 2d 72 65 76 65 72 73 65 20 3a 3a 20 43  ode-reverse :: C
2740: 6f 6e 76 65 72 74 65 72 2c 20 6f 72 0a 3b 20 6e  onverter, or.; n
2750: 6f 64 65 2d 72 65 76 65 72 73 65 3a 3a 20 4e 6f  ode-reverse:: No
2760: 64 65 7c 4e 6f 64 65 73 65 74 20 2d 3e 20 4e 6f  de|Nodeset -> No
2770: 64 65 73 65 74 0a 3b 20 52 65 76 65 72 73 65 73  deset.; Reverses
2780: 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 6e 6f   the order of no
2790: 64 65 73 20 69 6e 20 74 68 65 20 6e 6f 64 65 73  des in the nodes
27a0: 65 74 0a 3b 20 54 68 69 73 20 62 61 73 69 63 20  et.; This basic 
27b0: 63 6f 6e 76 65 72 74 65 72 20 69 73 20 6e 65 65  converter is nee
27c0: 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ded to implement
27d0: 20 61 20 72 65 76 65 72 73 65 20 64 6f 63 75 6d   a reverse docum
27e0: 65 6e 74 20 6f 72 64 65 72 0a 3b 20 28 73 65 65  ent order.; (see
27f0: 20 74 68 65 20 58 50 61 74 68 20 52 65 63 6f 6d   the XPath Recom
2800: 6d 65 6e 64 61 74 69 6f 6e 29 2e 0a 28 64 65 66  mendation)..(def
2810: 69 6e 65 20 6e 6f 64 65 2d 72 65 76 65 72 73 65  ine node-reverse
2820: 20 0a 20 20 28 6c 61 6d 62 64 61 20 28 6e 6f 64   .  (lambda (nod
2830: 65 2d 6f 72 2d 6e 6f 64 65 73 65 74 29 0a 20 20  e-or-nodeset).  
2840: 20 20 28 69 66 20 28 6e 6f 74 20 28 6e 6f 64 65    (if (not (node
2850: 73 65 74 3f 20 6e 6f 64 65 2d 6f 72 2d 6e 6f 64  set? node-or-nod
2860: 65 73 65 74 29 29 20 28 6c 69 73 74 20 6e 6f 64  eset)) (list nod
2870: 65 2d 6f 72 2d 6e 6f 64 65 73 65 74 29 0a 09 28  e-or-nodeset)..(
2880: 72 65 76 65 72 73 65 20 6e 6f 64 65 2d 6f 72 2d  reverse node-or-
2890: 6e 6f 64 65 73 65 74 29 29 29 29 0a 0a 3b 20 6e  nodeset))))..; n
28a0: 6f 64 65 2d 74 72 61 63 65 3a 3a 20 53 74 72 69  ode-trace:: Stri
28b0: 6e 67 20 2d 3e 20 43 6f 6e 76 65 72 74 65 72 0a  ng -> Converter.
28c0: 3b 20 28 6e 6f 64 65 2d 74 72 61 63 65 20 74 69  ; (node-trace ti
28d0: 74 6c 65 29 20 69 73 20 61 6e 20 69 64 65 6e 74  tle) is an ident
28e0: 69 74 79 20 63 6f 6e 76 65 72 74 65 72 2e 20 49  ity converter. I
28f0: 6e 20 61 64 64 69 74 69 6f 6e 20 69 74 20 70 72  n addition it pr
2900: 69 6e 74 73 20 6f 75 74 0a 3b 20 61 20 6e 6f 64  ints out.; a nod
2910: 65 20 6f 72 20 6e 6f 64 65 73 65 74 20 69 74 20  e or nodeset it 
2920: 69 73 20 61 70 70 6c 69 65 64 20 74 6f 2c 20 70  is applied to, p
2930: 72 65 66 69 78 65 64 20 77 69 74 68 20 74 68 65  refixed with the
2940: 20 27 74 69 74 6c 65 27 2e 0a 3b 20 54 68 69 73   'title'..; This
2950: 20 63 6f 6e 76 65 72 74 65 72 20 69 73 20 76 65   converter is ve
2960: 72 79 20 75 73 65 66 75 6c 20 66 6f 72 20 64 65  ry useful for de
2970: 62 75 67 67 69 6e 67 2e 0a 0a 28 64 65 66 69 6e  bugging...(defin
2980: 65 20 28 6e 6f 64 65 2d 74 72 61 63 65 20 74 69  e (node-trace ti
2990: 74 6c 65 29 0a 20 20 28 6c 61 6d 62 64 61 20 28  tle).  (lambda (
29a0: 6e 6f 64 65 2d 6f 72 2d 6e 6f 64 65 73 65 74 29  node-or-nodeset)
29b0: 0a 20 20 20 20 28 63 6f 75 74 20 6e 6c 20 22 2d  .    (cout nl "-
29c0: 2d 3e 22 29 0a 20 20 20 20 28 64 69 73 70 6c 61  ->").    (displa
29d0: 79 20 74 69 74 6c 65 29 0a 20 20 20 20 28 64 69  y title).    (di
29e0: 73 70 6c 61 79 20 22 20 3a 22 29 0a 20 20 20 20  splay " :").    
29f0: 28 70 72 65 74 74 79 2d 70 72 69 6e 74 20 6e 6f  (pretty-print no
2a00: 64 65 2d 6f 72 2d 6e 6f 64 65 73 65 74 29 0a 20  de-or-nodeset). 
2a10: 20 20 20 6e 6f 64 65 2d 6f 72 2d 6e 6f 64 65 73     node-or-nodes
2a20: 65 74 29 29 0a 0a 0a 3b 2d 2d 2d 2d 2d 2d 2d 2d  et))...;--------
2a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2a40: 2d 0a 3b 20 43 6f 6e 76 65 72 74 65 72 20 63 6f  -.; Converter co
2a50: 6d 62 69 6e 61 74 6f 72 73 0a 3b 0a 3b 20 43 6f  mbinators.;.; Co
2a60: 6d 62 69 6e 61 74 6f 72 73 20 61 72 65 20 68 69  mbinators are hi
2a70: 67 68 65 72 2d 6f 72 64 65 72 20 66 75 6e 63 74  gher-order funct
2a80: 69 6f 6e 73 20 74 68 61 74 20 74 72 61 6e 73 6d  ions that transm
2a90: 6f 67 72 69 66 79 20 61 20 63 6f 6e 76 65 72 74  ogrify a convert
2aa0: 65 72 0a 3b 20 6f 72 20 67 6c 75 65 20 61 20 73  er.; or glue a s
2ab0: 65 71 75 65 6e 63 65 20 6f 66 20 63 6f 6e 76 65  equence of conve
2ac0: 72 74 65 72 73 20 69 6e 74 6f 20 61 20 73 69 6e  rters into a sin
2ad0: 67 6c 65 2c 20 6e 6f 6e 2d 74 72 69 76 69 61 6c  gle, non-trivial
2ae0: 0a 3b 20 63 6f 6e 76 65 72 74 65 72 2e 20 54 68  .; converter. Th
2af0: 65 20 67 6f 61 6c 20 69 73 20 74 6f 20 61 72 72  e goal is to arr
2b00: 69 76 65 20 61 74 20 63 6f 6e 76 65 72 74 65 72  ive at converter
2b10: 73 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e  s that correspon
2b20: 64 20 74 6f 0a 3b 20 58 50 61 74 68 20 6c 6f 63  d to.; XPath loc
2b30: 61 74 69 6f 6e 20 70 61 74 68 73 2e 0a 3b 0a 3b  ation paths..;.;
2b40: 20 46 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e   From a differen
2b50: 74 20 70 6f 69 6e 74 20 6f 66 20 76 69 65 77 2c  t point of view,
2b60: 20 61 20 63 6f 6d 62 69 6e 61 74 6f 72 20 69 73   a combinator is
2b70: 20 61 20 66 69 78 65 64 2c 20 6e 61 6d 65 64 0a   a fixed, named.
2b80: 3b 20 5f 70 61 74 74 65 72 6e 5f 20 6f 66 20 61  ; _pattern_ of a
2b90: 70 70 6c 79 69 6e 67 20 63 6f 6e 76 65 72 74 65  pplying converte
2ba0: 72 73 2e 20 47 69 76 65 6e 20 62 65 6c 6f 77 20  rs. Given below 
2bb0: 69 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 73 65  is a complete se
2bc0: 74 20 6f 66 0a 3b 20 73 75 63 68 20 70 61 74 74  t of.; such patt
2bd0: 65 72 6e 73 20 74 68 61 74 20 74 6f 67 65 74 68  erns that togeth
2be0: 65 72 20 69 6d 70 6c 65 6d 65 6e 74 20 58 50 61  er implement XPa
2bf0: 74 68 20 6c 6f 63 61 74 69 6f 6e 20 70 61 74 68  th location path
2c00: 0a 3b 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e  .; specification
2c10: 2e 20 41 73 20 69 74 20 74 75 72 6e 73 20 6f 75  . As it turns ou
2c20: 74 2c 20 61 6c 6c 20 74 68 65 73 65 20 63 6f 6d  t, all these com
2c30: 62 69 6e 61 74 6f 72 73 20 63 61 6e 20 62 65 20  binators can be 
2c40: 62 75 69 6c 74 0a 3b 20 66 72 6f 6d 20 61 20 73  built.; from a s
2c50: 6d 61 6c 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  mall number of b
2c60: 61 73 69 63 20 62 6c 6f 63 6b 73 3a 20 72 65 67  asic blocks: reg
2c70: 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 61 6c 20  ular functional 
2c80: 63 6f 6d 70 6f 73 69 74 69 6f 6e 2c 0a 3b 20 6d  composition,.; m
2c90: 61 70 2d 75 6e 69 6f 6e 20 61 6e 64 20 66 69 6c  ap-union and fil
2ca0: 74 65 72 20 61 70 70 6c 69 63 61 74 6f 72 73 2c  ter applicators,
2cb0: 20 61 6e 64 20 74 68 65 20 6e 6f 64 65 73 65 74   and the nodeset
2cc0: 20 75 6e 69 6f 6e 2e 0a 0a 0a 0a 3b 20 73 65 6c   union.....; sel
2cd0: 65 63 74 2d 6b 69 64 73 3a 3a 20 50 72 65 64 20  ect-kids:: Pred 
2ce0: 2d 3e 20 4e 6f 64 65 20 2d 3e 20 4e 6f 64 65 73  -> Node -> Nodes
2cf0: 65 74 0a 3b 20 47 69 76 65 6e 20 61 20 4e 6f 64  et.; Given a Nod
2d00: 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 28 6f 72  e, return an (or
2d10: 64 65 72 65 64 29 20 73 75 62 73 65 74 20 69 74  dered) subset it
2d20: 73 20 63 68 69 6c 64 72 65 6e 20 74 68 61 74 20  s children that 
2d30: 73 61 74 69 73 66 79 0a 3b 20 74 68 65 20 50 72  satisfy.; the Pr
2d40: 65 64 20 28 61 20 63 6f 6e 76 65 72 74 65 72 2c  ed (a converter,
2d50: 20 61 63 74 75 61 6c 6c 79 29 0a 3b 20 73 65 6c   actually).; sel
2d60: 65 63 74 2d 6b 69 64 73 3a 3a 20 50 72 65 64 20  ect-kids:: Pred 
2d70: 2d 3e 20 4e 6f 64 65 73 65 74 20 2d 3e 20 4e 6f  -> Nodeset -> No
2d80: 64 65 73 65 74 0a 3b 20 54 68 65 20 73 61 6d 65  deset.; The same
2d90: 20 61 73 20 61 62 6f 76 65 2c 20 62 75 74 20 73   as above, but s
2da0: 65 6c 65 63 74 20 61 6d 6f 6e 67 20 63 68 69 6c  elect among chil
2db0: 64 72 65 6e 20 6f 66 20 61 6c 6c 20 74 68 65 20  dren of all the 
2dc0: 6e 6f 64 65 73 20 69 6e 0a 3b 20 74 68 65 20 4e  nodes in.; the N
2dd0: 6f 64 65 73 65 74 0a 3b 0a 3b 20 4d 6f 72 65 20  odeset.;.; More 
2de0: 73 75 63 63 69 6e 63 74 6c 79 2c 20 74 68 65 20  succinctly, the 
2df0: 73 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68 69  signature of thi
2e00: 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 3b 20  s function is.; 
2e10: 73 65 6c 65 63 74 2d 6b 69 64 73 3a 3a 20 43 6f  select-kids:: Co
2e20: 6e 76 65 72 74 65 72 20 2d 3e 20 43 6f 6e 76 65  nverter -> Conve
2e30: 72 74 65 72 0a 0a 28 64 65 66 69 6e 65 20 28 73  rter..(define (s
2e40: 65 6c 65 63 74 2d 6b 69 64 73 20 74 65 73 74 2d  elect-kids test-
2e50: 70 72 65 64 3f 29 0a 20 20 28 6c 61 6d 62 64 61  pred?).  (lambda
2e60: 20 28 6e 6f 64 65 29 09 09 3b 20 6e 6f 64 65 20   (node)..; node 
2e70: 6f 72 20 6e 6f 64 65 2d 73 65 74 0a 20 20 20 20  or node-set.    
2e80: 28 63 6f 6e 64 20 0a 20 20 20 20 20 28 28 6e 75  (cond .     ((nu
2e90: 6c 6c 3f 20 6e 6f 64 65 29 20 6e 6f 64 65 29 0a  ll? node) node).
2ea0: 20 20 20 20 20 28 28 6e 6f 74 20 28 70 61 69 72       ((not (pair
2eb0: 3f 20 6e 6f 64 65 29 29 20 27 28 29 29 20 20 20  ? node)) '())   
2ec0: 3b 20 4e 6f 20 63 68 69 6c 64 72 65 6e 0a 20 20  ; No children.  
2ed0: 20 20 20 28 28 73 79 6d 62 6f 6c 3f 20 28 63 61     ((symbol? (ca
2ee0: 72 20 6e 6f 64 65 29 29 0a 20 20 20 20 20 20 28  r node)).      (
2ef0: 28 66 69 6c 74 65 72 20 74 65 73 74 2d 70 72 65  (filter test-pre
2f00: 64 3f 29 20 28 63 64 72 20 6e 6f 64 65 29 29 29  d?) (cdr node)))
2f10: 09 3b 20 69 74 27 73 20 61 20 73 69 6e 67 6c 65  .; it's a single
2f20: 20 6e 6f 64 65 0a 20 20 20 20 20 28 65 6c 73 65   node.     (else
2f30: 20 28 6d 61 70 2d 75 6e 69 6f 6e 20 28 73 65 6c   (map-union (sel
2f40: 65 63 74 2d 6b 69 64 73 20 74 65 73 74 2d 70 72  ect-kids test-pr
2f50: 65 64 3f 29 20 6e 6f 64 65 29 29 29 29 29 0a 0a  ed?) node)))))..
2f60: 0a 3b 20 6e 6f 64 65 2d 73 65 6c 66 3a 3a 20 50  .; node-self:: P
2f70: 72 65 64 20 2d 3e 20 4e 6f 64 65 20 2d 3e 20 4e  red -> Node -> N
2f80: 6f 64 65 73 65 74 2c 20 6f 72 0a 3b 20 6e 6f 64  odeset, or.; nod
2f90: 65 2d 73 65 6c 66 3a 3a 20 43 6f 6e 76 65 72 74  e-self:: Convert
2fa0: 65 72 20 2d 3e 20 43 6f 6e 76 65 72 74 65 72 0a  er -> Converter.
2fb0: 3b 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 65 6c  ; Similar to sel
2fc0: 65 63 74 2d 6b 69 64 73 20 62 75 74 20 61 70 70  ect-kids but app
2fd0: 6c 79 20 74 6f 20 74 68 65 20 4e 6f 64 65 20 69  ly to the Node i
2fe0: 74 73 65 6c 66 20 72 61 74 68 65 72 0a 3b 20 74  tself rather.; t
2ff0: 68 61 6e 20 74 6f 20 69 74 73 20 63 68 69 6c 64  han to its child
3000: 72 65 6e 2e 20 54 68 65 20 72 65 73 75 6c 74 69  ren. The resulti
3010: 6e 67 20 4e 6f 64 65 73 65 74 20 77 69 6c 6c 20  ng Nodeset will 
3020: 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72 20 6f  contain either o
3030: 6e 65 0a 3b 20 63 6f 6d 70 6f 6e 65 6e 74 2c 20  ne.; component, 
3040: 6f 72 20 77 69 6c 6c 20 62 65 20 65 6d 70 74 79  or will be empty
3050: 20 28 69 66 20 74 68 65 20 4e 6f 64 65 20 66 61   (if the Node fa
3060: 69 6c 65 64 20 74 68 65 20 50 72 65 64 29 2e 0a  iled the Pred)..
3070: 28 64 65 66 69 6e 65 20 6e 6f 64 65 2d 73 65 6c  (define node-sel
3080: 66 20 66 69 6c 74 65 72 29 0a 0a 0a 3b 20 6e 6f  f filter)...; no
3090: 64 65 2d 6a 6f 69 6e 3a 3a 20 5b 4c 6f 63 50 61  de-join:: [LocPa
30a0: 74 68 5d 20 2d 3e 20 4e 6f 64 65 7c 4e 6f 64 65  th] -> Node|Node
30b0: 73 65 74 20 2d 3e 20 4e 6f 64 65 73 65 74 2c 20  set -> Nodeset, 
30c0: 6f 72 0a 3b 20 6e 6f 64 65 2d 6a 6f 69 6e 3a 3a  or.; node-join::
30d0: 20 5b 43 6f 6e 76 65 72 74 65 72 5d 20 2d 3e 20   [Converter] -> 
30e0: 43 6f 6e 76 65 72 74 65 72 0a 3b 20 6a 6f 69 6e  Converter.; join
30f0: 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f 66   the sequence of
3100: 20 6c 6f 63 61 74 69 6f 6e 20 73 74 65 70 73 20   location steps 
3110: 6f 72 20 70 61 74 68 73 20 61 73 20 64 65 73 63  or paths as desc
3120: 72 69 62 65 64 0a 3b 20 69 6e 20 74 68 65 20 74  ribed.; in the t
3130: 69 74 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62  itle comments ab
3140: 6f 76 65 2e 0a 28 64 65 66 69 6e 65 20 28 6e 6f  ove..(define (no
3150: 64 65 2d 6a 6f 69 6e 20 2e 20 73 65 6c 65 63 74  de-join . select
3160: 6f 72 73 29 0a 20 20 28 6c 61 6d 62 64 61 20 28  ors).  (lambda (
3170: 6e 6f 64 65 73 65 74 29 09 09 3b 20 4e 6f 64 65  nodeset)..; Node
3180: 73 65 74 20 6f 72 20 6e 6f 64 65 0a 20 20 20 20  set or node.    
3190: 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6e 6f 64 65  (let loop ((node
31a0: 73 65 74 20 6e 6f 64 65 73 65 74 29 20 28 73 65  set nodeset) (se
31b0: 6c 65 63 74 6f 72 73 20 73 65 6c 65 63 74 6f 72  lectors selector
31c0: 73 29 29 0a 20 20 20 20 20 20 28 69 66 20 28 6e  s)).      (if (n
31d0: 75 6c 6c 3f 20 73 65 6c 65 63 74 6f 72 73 29 20  ull? selectors) 
31e0: 6e 6f 64 65 73 65 74 0a 09 20 20 28 6c 6f 6f 70  nodeset..  (loop
31f0: 20 0a 09 20 20 20 28 69 66 20 28 6e 6f 64 65 73   ..   (if (nodes
3200: 65 74 3f 20 6e 6f 64 65 73 65 74 29 0a 09 20 20  et? nodeset)..  
3210: 20 20 20 20 20 28 6d 61 70 2d 75 6e 69 6f 6e 20       (map-union 
3220: 28 63 61 72 20 73 65 6c 65 63 74 6f 72 73 29 20  (car selectors) 
3230: 6e 6f 64 65 73 65 74 29 0a 09 20 20 20 20 20 20  nodeset)..      
3240: 20 28 28 63 61 72 20 73 65 6c 65 63 74 6f 72 73   ((car selectors
3250: 29 20 6e 6f 64 65 73 65 74 29 29 0a 09 20 20 20  ) nodeset))..   
3260: 28 63 64 72 20 73 65 6c 65 63 74 6f 72 73 29 29  (cdr selectors))
3270: 29 29 29 29 0a 0a 0a 3b 20 6e 6f 64 65 2d 72 65  ))))...; node-re
3280: 64 75 63 65 3a 3a 20 5b 4c 6f 63 50 61 74 68 5d  duce:: [LocPath]
3290: 20 2d 3e 20 4e 6f 64 65 7c 4e 6f 64 65 73 65 74   -> Node|Nodeset
32a0: 20 2d 3e 20 4e 6f 64 65 73 65 74 2c 20 6f 72 0a   -> Nodeset, or.
32b0: 3b 20 6e 6f 64 65 2d 72 65 64 75 63 65 3a 3a 20  ; node-reduce:: 
32c0: 5b 43 6f 6e 76 65 72 74 65 72 5d 20 2d 3e 20 43  [Converter] -> C
32d0: 6f 6e 76 65 72 74 65 72 0a 3b 20 41 20 72 65 67  onverter.; A reg
32e0: 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e 61 6c 20  ular functional 
32f0: 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 63  composition of c
3300: 6f 6e 76 65 72 74 65 72 73 2e 0a 3b 20 46 72 6f  onverters..; Fro
3310: 6d 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f  m a different po
3320: 69 6e 74 20 6f 66 20 76 69 65 77 2c 0a 3b 20 20  int of view,.;  
3330: 20 20 28 28 61 70 70 6c 79 20 6e 6f 64 65 2d 72    ((apply node-r
3340: 65 64 75 63 65 20 63 6f 6e 76 65 72 74 65 72 73  educe converters
3350: 29 20 6e 6f 64 65 73 65 74 29 0a 3b 20 69 73 20  ) nodeset).; is 
3360: 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a 3b 20  equivalent to.; 
3370: 20 20 20 28 66 6f 6c 64 6c 20 61 70 70 6c 79 20     (foldl apply 
3380: 6e 6f 64 65 73 65 74 20 63 6f 6e 76 65 72 74 65  nodeset converte
3390: 72 73 29 0a 3b 20 69 2e 65 2e 2c 20 66 6f 6c 64  rs).; i.e., fold
33a0: 69 6e 67 2c 20 6f 72 20 72 65 64 75 63 69 6e 67  ing, or reducing
33b0: 2c 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6e 76  , a list of conv
33c0: 65 72 74 65 72 73 20 77 69 74 68 20 74 68 65 20  erters with the 
33d0: 6e 6f 64 65 73 65 74 0a 3b 20 61 73 20 61 20 73  nodeset.; as a s
33e0: 65 65 64 2e 0a 28 64 65 66 69 6e 65 20 28 6e 6f  eed..(define (no
33f0: 64 65 2d 72 65 64 75 63 65 20 2e 20 63 6f 6e 76  de-reduce . conv
3400: 65 72 74 65 72 73 29 0a 20 20 28 6c 61 6d 62 64  erters).  (lambd
3410: 61 20 28 6e 6f 64 65 73 65 74 29 09 09 3b 20 4e  a (nodeset)..; N
3420: 6f 64 65 73 65 74 20 6f 72 20 6e 6f 64 65 0a 20  odeset or node. 
3430: 20 20 20 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6e     (let loop ((n
3440: 6f 64 65 73 65 74 20 6e 6f 64 65 73 65 74 29 20  odeset nodeset) 
3450: 28 63 6f 6e 76 65 72 74 65 72 73 20 63 6f 6e 76  (converters conv
3460: 65 72 74 65 72 73 29 29 0a 20 20 20 20 20 20 28  erters)).      (
3470: 69 66 20 28 6e 75 6c 6c 3f 20 63 6f 6e 76 65 72  if (null? conver
3480: 74 65 72 73 29 20 6e 6f 64 65 73 65 74 0a 09 20  ters) nodeset.. 
3490: 20 28 6c 6f 6f 70 20 28 28 63 61 72 20 63 6f 6e   (loop ((car con
34a0: 76 65 72 74 65 72 73 29 20 6e 6f 64 65 73 65 74  verters) nodeset
34b0: 29 20 28 63 64 72 20 63 6f 6e 76 65 72 74 65 72  ) (cdr converter
34c0: 73 29 29 29 29 29 29 0a 0a 0a 3b 20 6e 6f 64 65  s))))))...; node
34d0: 2d 6f 72 3a 3a 20 5b 43 6f 6e 76 65 72 74 65 72  -or:: [Converter
34e0: 5d 20 2d 3e 20 43 6f 6e 76 65 72 74 65 72 0a 3b  ] -> Converter.;
34f0: 20 54 68 69 73 20 63 6f 6d 62 69 6e 61 74 6f 72   This combinator
3500: 20 61 70 70 6c 69 65 73 20 61 6c 6c 20 63 6f 6e   applies all con
3510: 76 65 72 74 65 72 73 20 74 6f 20 61 20 67 69 76  verters to a giv
3520: 65 6e 20 6e 6f 64 65 20 61 6e 64 0a 3b 20 70 72  en node and.; pr
3530: 6f 64 75 63 65 73 20 74 68 65 20 75 6e 69 6f 6e  oduces the union
3540: 20 6f 66 20 74 68 65 69 72 20 72 65 73 75 6c 74   of their result
3550: 73 2e 0a 3b 20 54 68 69 73 20 63 6f 6d 62 69 6e  s..; This combin
3560: 61 74 6f 72 20 63 6f 72 72 65 73 70 6f 6e 64 73  ator corresponds
3570: 20 74 6f 20 61 20 75 6e 69 6f 6e 2c 20 27 7c 27   to a union, '|'
3580: 20 6f 70 65 72 61 74 69 6f 6e 20 66 6f 72 20 58   operation for X
3590: 50 61 74 68 0a 3b 20 6c 6f 63 61 74 69 6f 6e 20  Path.; location 
35a0: 70 61 74 68 73 2e 0a 3b 20 28 64 65 66 69 6e 65  paths..; (define
35b0: 20 28 6e 6f 64 65 2d 6f 72 20 2e 20 63 6f 6e 76   (node-or . conv
35c0: 65 72 74 65 72 73 29 0a 3b 20 20 20 28 6c 61 6d  erters).;   (lam
35d0: 62 64 61 20 28 6e 6f 64 65 2d 6f 72 2d 6e 6f 64  bda (node-or-nod
35e0: 65 73 65 74 29 0a 3b 20 20 20 20 20 28 69 66 20  eset).;     (if 
35f0: 28 6e 75 6c 6c 3f 20 63 6f 6e 76 65 72 74 65 72  (null? converter
3600: 73 29 20 6e 6f 64 65 2d 6f 72 2d 6e 6f 64 65 73  s) node-or-nodes
3610: 65 74 0a 3b 20 09 28 61 70 70 65 6e 64 20 0a 3b  et.; .(append .;
3620: 20 09 20 28 28 63 61 72 20 63 6f 6e 76 65 72 74   . ((car convert
3630: 65 72 73 29 20 6e 6f 64 65 2d 6f 72 2d 6e 6f 64  ers) node-or-nod
3640: 65 73 65 74 29 0a 3b 20 09 20 28 28 61 70 70 6c  eset).; . ((appl
3650: 79 20 6e 6f 64 65 2d 6f 72 20 28 63 64 72 20 63  y node-or (cdr c
3660: 6f 6e 76 65 72 74 65 72 73 29 29 20 6e 6f 64 65  onverters)) node
3670: 2d 6f 72 2d 6e 6f 64 65 73 65 74 29 29 29 29 29  -or-nodeset)))))
3680: 0a 3b 20 4d 6f 72 65 20 6f 70 74 69 6d 61 6c 20  .; More optimal 
3690: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
36a0: 6f 6c 6c 6f 77 73 0a 28 64 65 66 69 6e 65 20 28  ollows.(define (
36b0: 6e 6f 64 65 2d 6f 72 20 2e 20 63 6f 6e 76 65 72  node-or . conver
36c0: 74 65 72 73 29 0a 20 20 28 6c 61 6d 62 64 61 20  ters).  (lambda 
36d0: 28 6e 6f 64 65 2d 6f 72 2d 6e 6f 64 65 73 65 74  (node-or-nodeset
36e0: 29 0a 20 20 20 20 28 6c 65 74 20 6c 6f 6f 70 20  ).    (let loop 
36f0: 28 28 72 65 73 75 6c 74 20 27 28 29 29 20 28 63  ((result '()) (c
3700: 6f 6e 76 65 72 74 65 72 73 20 63 6f 6e 76 65 72  onverters conver
3710: 74 65 72 73 29 29 0a 20 20 20 20 20 20 28 69 66  ters)).      (if
3720: 20 28 6e 75 6c 6c 3f 20 63 6f 6e 76 65 72 74 65   (null? converte
3730: 72 73 29 20 72 65 73 75 6c 74 0a 09 20 20 28 6c  rs) result..  (l
3740: 6f 6f 70 20 28 61 70 70 65 6e 64 20 72 65 73 75  oop (append resu
3750: 6c 74 20 28 6f 72 20 28 28 63 61 72 20 63 6f 6e  lt (or ((car con
3760: 76 65 72 74 65 72 73 29 20 6e 6f 64 65 2d 6f 72  verters) node-or
3770: 2d 6e 6f 64 65 73 65 74 29 20 27 28 29 29 29 0a  -nodeset) '())).
3780: 09 09 28 63 64 72 20 63 6f 6e 76 65 72 74 65 72  ..(cdr converter
3790: 73 29 29 29 29 29 29 0a 0a 0a 3b 20 6e 6f 64 65  s))))))...; node
37a0: 2d 63 6c 6f 73 75 72 65 3a 3a 20 43 6f 6e 76 65  -closure:: Conve
37b0: 72 74 65 72 20 2d 3e 20 43 6f 6e 76 65 72 74 65  rter -> Converte
37c0: 72 0a 3b 20 53 65 6c 65 63 74 20 61 6c 6c 20 5f  r.; Select all _
37d0: 64 65 73 63 65 6e 64 61 6e 74 73 5f 20 6f 66 20  descendants_ of 
37e0: 61 20 6e 6f 64 65 20 74 68 61 74 20 73 61 74 69  a node that sati
37f0: 73 66 79 20 61 20 63 6f 6e 76 65 72 74 65 72 2d  sfy a converter-
3800: 70 72 65 64 69 63 61 74 65 2e 0a 3b 20 54 68 69  predicate..; Thi
3810: 73 20 63 6f 6d 62 69 6e 61 74 6f 72 20 69 73 20  s combinator is 
3820: 73 69 6d 69 6c 61 72 20 74 6f 20 73 65 6c 65 63  similar to selec
3830: 74 2d 6b 69 64 73 20 62 75 74 20 61 70 70 6c 69  t-kids but appli
3840: 65 73 20 74 6f 0a 3b 20 67 72 61 6e 64 2e 2e 2e  es to.; grand...
3850: 20 63 68 69 6c 64 72 65 6e 20 61 73 20 77 65 6c   children as wel
3860: 6c 2e 0a 3b 20 54 68 69 73 20 63 6f 6d 62 69 6e  l..; This combin
3870: 61 74 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 73 20  ator implements 
3880: 74 68 65 20 22 64 65 73 63 65 6e 64 61 6e 74 3a  the "descendant:
3890: 3a 22 20 58 50 61 74 68 20 61 78 69 73 0a 3b 20  :" XPath axis.; 
38a0: 43 6f 6e 63 65 70 74 75 61 6c 6c 79 2c 20 74 68  Conceptually, th
38b0: 69 73 20 63 6f 6d 62 69 6e 61 74 6f 72 20 63 61  is combinator ca
38c0: 6e 20 62 65 20 65 78 70 72 65 73 73 65 64 20 61  n be expressed a
38d0: 73 0a 3b 20 28 64 65 66 69 6e 65 20 28 6e 6f 64  s.; (define (nod
38e0: 65 2d 63 6c 6f 73 75 72 65 20 66 29 0a 3b 20 20  e-closure f).;  
38f0: 20 20 20 20 28 6e 6f 64 65 2d 6f 72 0a 3b 20 20      (node-or.;  
3900: 20 20 20 20 20 20 28 73 65 6c 65 63 74 2d 6b 69        (select-ki
3910: 64 73 20 66 29 0a 3b 09 20 28 6e 6f 64 65 2d 72  ds f).;. (node-r
3920: 65 64 75 63 65 20 28 73 65 6c 65 63 74 2d 6b 69  educe (select-ki
3930: 64 73 20 28 6e 6f 64 65 2d 74 79 70 65 6f 66 3f  ds (node-typeof?
3940: 20 27 2a 29 29 20 28 6e 6f 64 65 2d 63 6c 6f 73   '*)) (node-clos
3950: 75 72 65 20 66 29 29 29 29 0a 3b 20 54 68 69 73  ure f)))).; This
3960: 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 61 73 20   definition, as 
3970: 77 72 69 74 74 65 6e 2c 20 6c 6f 6f 6b 73 20 73  written, looks s
3980: 6f 6d 65 77 68 61 74 20 6c 69 6b 65 20 61 20 66  omewhat like a f
3990: 69 78 70 6f 69 6e 74 2c 20 61 6e 64 20 69 74 0a  ixpoint, and it.
39a0: 3b 20 77 69 6c 6c 20 72 75 6e 20 66 6f 72 65 76  ; will run forev
39b0: 65 72 2e 20 49 74 20 69 73 20 6f 62 76 69 6f 75  er. It is obviou
39c0: 73 20 68 6f 77 65 76 65 72 20 74 68 61 74 20 73  s however that s
39d0: 6f 6f 6e 65 72 20 6f 72 20 6c 61 74 65 72 0a 3b  ooner or later.;
39e0: 20 28 73 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e   (select-kids (n
39f0: 6f 64 65 2d 74 79 70 65 6f 66 3f 20 27 2a 29 29  ode-typeof? '*))
3a00: 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 6e 20   will return an 
3a10: 65 6d 70 74 79 20 6e 6f 64 65 73 65 74 2e 20 41  empty nodeset. A
3a20: 74 0a 3b 20 74 68 69 73 20 70 6f 69 6e 74 20 66  t.; this point f
3a30: 75 72 74 68 65 72 20 69 74 65 72 61 74 69 6f 6e  urther iteration
3a40: 73 20 77 69 6c 6c 20 6e 6f 20 6c 6f 6e 67 65 72  s will no longer
3a50: 20 61 66 66 65 63 74 20 74 68 65 20 72 65 73 75   affect the resu
3a60: 6c 74 20 61 6e 64 0a 3b 20 63 61 6e 20 62 65 20  lt and.; can be 
3a70: 73 74 6f 70 70 65 64 2e 0a 0a 28 64 65 66 69 6e  stopped...(defin
3a80: 65 20 28 6e 6f 64 65 2d 63 6c 6f 73 75 72 65 20  e (node-closure 
3a90: 74 65 73 74 2d 70 72 65 64 3f 29 09 20 20 20 20  test-pred?).    
3aa0: 0a 20 20 28 6c 61 6d 62 64 61 20 28 6e 6f 64 65  .  (lambda (node
3ab0: 29 09 09 3b 20 4e 6f 64 65 73 65 74 20 6f 72 20  )..; Nodeset or 
3ac0: 6e 6f 64 65 0a 20 20 20 20 28 6c 65 74 20 6c 6f  node.    (let lo
3ad0: 6f 70 20 28 28 70 61 72 65 6e 74 20 6e 6f 64 65  op ((parent node
3ae0: 29 20 28 72 65 73 75 6c 74 20 27 28 29 29 29 0a  ) (result '())).
3af0: 20 20 20 20 20 20 28 69 66 20 28 6e 75 6c 6c 3f        (if (null?
3b00: 20 70 61 72 65 6e 74 29 20 72 65 73 75 6c 74 0a   parent) result.
3b10: 09 20 20 28 6c 6f 6f 70 20 28 28 73 65 6c 65 63  .  (loop ((selec
3b20: 74 2d 6b 69 64 73 20 28 6e 6f 64 65 2d 74 79 70  t-kids (node-typ
3b30: 65 6f 66 3f 20 27 2a 29 29 20 70 61 72 65 6e 74  eof? '*)) parent
3b40: 29 0a 09 09 28 61 70 70 65 6e 64 20 72 65 73 75  )...(append resu
3b50: 6c 74 0a 09 09 09 28 28 73 65 6c 65 63 74 2d 6b  lt....((select-k
3b60: 69 64 73 20 74 65 73 74 2d 70 72 65 64 3f 29 20  ids test-pred?) 
3b70: 70 61 72 65 6e 74 29 29 29 0a 09 20 20 29 29 29  parent)))..  )))
3b80: 29 0a 0a 3b 20 6e 6f 64 65 2d 70 61 72 65 6e 74  )..; node-parent
3b90: 3a 3a 20 52 6f 6f 74 4e 6f 64 65 20 2d 3e 20 43  :: RootNode -> C
3ba0: 6f 6e 76 65 72 74 65 72 0a 3b 20 28 6e 6f 64 65  onverter.; (node
3bb0: 2d 70 61 72 65 6e 74 20 72 6f 6f 74 6e 6f 64 65  -parent rootnode
3bc0: 29 20 79 69 65 6c 64 73 20 61 20 63 6f 6e 76 65  ) yields a conve
3bd0: 72 74 65 72 20 74 68 61 74 20 72 65 74 75 72 6e  rter that return
3be0: 73 20 61 20 70 61 72 65 6e 74 20 6f 66 20 61 0a  s a parent of a.
3bf0: 3b 20 6e 6f 64 65 20 69 74 20 69 73 20 61 70 70  ; node it is app
3c00: 6c 69 65 64 20 74 6f 2e 20 49 66 20 61 70 70 6c  lied to. If appl
3c10: 69 65 64 20 74 6f 20 61 20 6e 6f 64 65 73 65 74  ied to a nodeset
3c20: 2c 20 69 74 20 72 65 74 75 72 6e 73 20 74 68 65  , it returns the
3c30: 20 6c 69 73 74 0a 3b 20 6f 66 20 70 61 72 65 6e   list.; of paren
3c40: 74 73 20 6f 66 20 6e 6f 64 65 73 20 69 6e 20 74  ts of nodes in t
3c50: 68 65 20 6e 6f 64 65 73 65 74 2e 20 54 68 65 20  he nodeset. The 
3c60: 72 6f 6f 74 6e 6f 64 65 20 64 6f 65 73 20 6e 6f  rootnode does no
3c70: 74 20 68 61 76 65 0a 3b 20 74 6f 20 62 65 20 74  t have.; to be t
3c80: 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
3c90: 74 68 65 20 77 68 6f 6c 65 20 53 58 4d 4c 20 74  the whole SXML t
3ca0: 72 65 65 20 2d 2d 20 69 74 20 6d 61 79 20 62 65  ree -- it may be
3cb0: 20 61 20 72 6f 6f 74 20 6e 6f 64 65 0a 3b 20 6f   a root node.; o
3cc0: 66 20 61 20 62 72 61 6e 63 68 20 6f 66 20 69 6e  f a branch of in
3cd0: 74 65 72 65 73 74 2e 0a 3b 20 47 69 76 65 6e 20  terest..; Given 
3ce0: 74 68 65 20 6e 6f 74 61 74 69 6f 6e 20 6f 66 20  the notation of 
3cf0: 50 68 69 6c 69 70 20 57 61 64 6c 65 72 27 73 20  Philip Wadler's 
3d00: 70 61 70 65 72 20 6f 6e 20 73 65 6d 61 6e 74 69  paper on semanti
3d10: 63 73 20 6f 66 20 58 53 4c 54 2c 0a 3b 20 20 70  cs of XSLT,.;  p
3d20: 61 72 65 6e 74 28 78 29 20 3d 20 7b 20 79 20 7c  arent(x) = { y |
3d30: 20 79 3d 73 75 62 6e 6f 64 65 2a 28 72 6f 6f 74   y=subnode*(root
3d40: 29 2c 20 78 3d 73 75 62 6e 6f 64 65 28 79 29 20  ), x=subnode(y) 
3d50: 7d 0a 3b 20 54 68 65 72 65 66 6f 72 65 2c 20 6e  }.; Therefore, n
3d60: 6f 64 65 2d 70 61 72 65 6e 74 20 69 73 20 6e 6f  ode-parent is no
3d70: 74 20 74 68 65 20 66 75 6e 64 61 6d 65 6e 74 61  t the fundamenta
3d80: 6c 20 63 6f 6e 76 65 72 74 65 72 3a 20 69 74 20  l converter: it 
3d90: 63 61 6e 20 62 65 0a 3b 20 65 78 70 72 65 73 73  can be.; express
3da0: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 65  ed through the e
3db0: 78 69 73 74 69 6e 67 20 6f 6e 65 73 2e 20 20 59  xisting ones.  Y
3dc0: 65 74 20 6e 6f 64 65 2d 70 61 72 65 6e 74 20 69  et node-parent i
3dd0: 73 20 61 20 72 61 74 68 65 72 0a 3b 20 63 6f 6e  s a rather.; con
3de0: 76 65 6e 69 65 6e 74 20 63 6f 6e 76 65 72 74 65  venient converte
3df0: 72 2e 20 49 74 20 63 6f 72 72 65 73 70 6f 6e 64  r. It correspond
3e00: 73 20 74 6f 20 61 20 70 61 72 65 6e 74 3a 3a 20  s to a parent:: 
3e10: 61 78 69 73 20 6f 66 20 53 58 50 61 74 68 2e 0a  axis of SXPath..
3e20: 3b 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  ; Note that the 
3e30: 70 61 72 65 6e 74 3a 3a 20 61 78 69 73 20 63 61  parent:: axis ca
3e40: 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 61  n be used with a
3e50: 6e 20 61 74 74 72 69 62 75 74 65 20 6e 6f 64 65  n attribute node
3e60: 20 61 73 20 77 65 6c 6c 21 0a 0a 28 64 65 66 69   as well!..(defi
3e70: 6e 65 20 28 6e 6f 64 65 2d 70 61 72 65 6e 74 20  ne (node-parent 
3e80: 72 6f 6f 74 6e 6f 64 65 29 0a 20 20 28 6c 61 6d  rootnode).  (lam
3e90: 62 64 61 20 28 6e 6f 64 65 29 09 09 3b 20 4e 6f  bda (node)..; No
3ea0: 64 65 73 65 74 20 6f 72 20 6e 6f 64 65 0a 20 20  deset or node.  
3eb0: 20 20 28 69 66 20 28 6e 6f 64 65 73 65 74 3f 20    (if (nodeset? 
3ec0: 6e 6f 64 65 29 20 28 6d 61 70 2d 75 6e 69 6f 6e  node) (map-union
3ed0: 20 28 6e 6f 64 65 2d 70 61 72 65 6e 74 20 72 6f   (node-parent ro
3ee0: 6f 74 6e 6f 64 65 29 20 6e 6f 64 65 29 0a 09 28  otnode) node)..(
3ef0: 6c 65 74 20 28 28 70 72 65 64 0a 09 20 20 20 20  let ((pred..    
3f00: 20 20 20 28 6e 6f 64 65 2d 6f 72 0a 09 09 28 6e     (node-or...(n
3f10: 6f 64 65 2d 72 65 64 75 63 65 0a 09 09 20 28 6e  ode-reduce... (n
3f20: 6f 64 65 2d 73 65 6c 66 20 28 6e 6f 64 65 2d 74  ode-self (node-t
3f30: 79 70 65 6f 66 3f 20 27 2a 29 29 0a 09 09 20 28  ypeof? '*))... (
3f40: 73 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e 6f 64  select-kids (nod
3f50: 65 2d 65 71 3f 20 6e 6f 64 65 29 29 29 0a 09 09  e-eq? node)))...
3f60: 28 6e 6f 64 65 2d 6a 6f 69 6e 0a 09 09 20 28 73  (node-join... (s
3f70: 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e 6f 64 65  elect-kids (node
3f80: 2d 74 79 70 65 6f 66 3f 20 27 40 29 29 0a 09 09  -typeof? '@))...
3f90: 20 28 73 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e   (select-kids (n
3fa0: 6f 64 65 2d 65 71 3f 20 6e 6f 64 65 29 29 29 29  ode-eq? node))))
3fb0: 29 29 0a 09 20 20 28 28 6e 6f 64 65 2d 6f 72 0a  ))..  ((node-or.
3fc0: 09 20 20 20 20 28 6e 6f 64 65 2d 73 65 6c 66 20  .    (node-self 
3fd0: 70 72 65 64 29 0a 09 20 20 20 20 28 6e 6f 64 65  pred)..    (node
3fe0: 2d 63 6c 6f 73 75 72 65 20 70 72 65 64 29 29 0a  -closure pred)).
3ff0: 09 20 20 20 72 6f 6f 74 6e 6f 64 65 29 29 29 29  .   rootnode))))
4000: 29 0a 0a 3b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  )..;------------
4010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 3b 20  -------------.; 
4020: 45 76 61 6c 75 61 74 65 20 61 6e 20 61 62 62 72  Evaluate an abbr
4030: 65 76 69 61 74 65 64 20 53 58 50 61 74 68 0a 3b  eviated SXPath.;
4040: 09 73 78 70 61 74 68 3a 3a 20 41 62 62 72 50 61  .sxpath:: AbbrPa
4050: 74 68 20 2d 3e 20 43 6f 6e 76 65 72 74 65 72 2c  th -> Converter,
4060: 20 6f 72 0a 3b 09 73 78 70 61 74 68 3a 3a 20 41   or.;.sxpath:: A
4070: 62 62 72 50 61 74 68 20 2d 3e 20 4e 6f 64 65 7c  bbrPath -> Node|
4080: 4e 6f 64 65 73 65 74 20 2d 3e 20 4e 6f 64 65 73  Nodeset -> Nodes
4090: 65 74 0a 3b 20 41 62 62 72 50 61 74 68 20 69 73  et.; AbbrPath is
40a0: 20 61 20 6c 69 73 74 2e 20 49 74 20 69 73 20 74   a list. It is t
40b0: 72 61 6e 73 6c 61 74 65 64 20 74 6f 20 74 68 65  ranslated to the
40c0: 20 66 75 6c 6c 20 53 58 50 61 74 68 20 61 63 63   full SXPath acc
40d0: 6f 72 64 69 6e 67 0a 3b 20 74 6f 20 74 68 65 20  ording.; to the 
40e0: 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 77 72 69 74  following rewrit
40f0: 69 6e 67 20 72 75 6c 65 73 0a 3b 20 28 73 78 70  ing rules.; (sxp
4100: 61 74 68 20 27 28 29 29 20 2d 3e 20 28 6e 6f 64  ath '()) -> (nod
4110: 65 2d 6a 6f 69 6e 29 0a 3b 20 28 73 78 70 61 74  e-join).; (sxpat
4120: 68 20 27 28 70 61 74 68 2d 63 6f 6d 70 6f 6e 65  h '(path-compone
4130: 6e 74 20 2e 2e 2e 29 29 20 2d 3e 0a 3b 09 09 28  nt ...)) ->.;..(
4140: 6e 6f 64 65 2d 6a 6f 69 6e 20 28 73 78 70 61 74  node-join (sxpat
4150: 68 31 20 70 61 74 68 2d 63 6f 6d 70 6f 6e 65 6e  h1 path-componen
4160: 74 29 20 28 73 78 70 61 74 68 20 27 28 2e 2e 2e  t) (sxpath '(...
4170: 29 29 29 0a 3b 20 28 73 78 70 61 74 68 31 20 27  ))).; (sxpath1 '
4180: 2f 2f 29 20 2d 3e 20 28 6e 6f 64 65 2d 6f 72 20  //) -> (node-or 
4190: 0a 3b 09 09 20 20 20 20 20 28 6e 6f 64 65 2d 73  .;..     (node-s
41a0: 65 6c 66 20 28 6e 6f 64 65 2d 74 79 70 65 6f 66  elf (node-typeof
41b0: 3f 20 27 2a 61 6e 79 2a 29 29 0a 3b 09 09 20 20  ? '*any*)).;..  
41c0: 20 20 20 20 28 6e 6f 64 65 2d 63 6c 6f 73 75 72      (node-closur
41d0: 65 20 28 6e 6f 64 65 2d 74 79 70 65 6f 66 3f 20  e (node-typeof? 
41e0: 27 2a 61 6e 79 2a 29 29 29 0a 3b 20 28 73 78 70  '*any*))).; (sxp
41f0: 61 74 68 31 20 27 28 65 71 75 61 6c 3f 20 78 29  ath1 '(equal? x)
4200: 29 20 2d 3e 20 28 73 65 6c 65 63 74 2d 6b 69 64  ) -> (select-kid
4210: 73 20 28 6e 6f 64 65 2d 65 71 75 61 6c 3f 20 78  s (node-equal? x
4220: 29 29 0a 3b 20 28 73 78 70 61 74 68 31 20 27 28  )).; (sxpath1 '(
4230: 65 71 3f 20 78 29 29 20 20 20 20 2d 3e 20 28 73  eq? x))    -> (s
4240: 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e 6f 64 65  elect-kids (node
4250: 2d 65 71 3f 20 78 29 29 0a 3b 20 28 73 78 70 61  -eq? x)).; (sxpa
4260: 74 68 31 20 3f 73 79 6d 62 6f 6c 29 20 20 20 20  th1 ?symbol)    
4270: 20 2d 3e 20 28 73 65 6c 65 63 74 2d 6b 69 64 73   -> (select-kids
4280: 20 28 6e 6f 64 65 2d 74 79 70 65 6f 66 3f 20 3f   (node-typeof? ?
4290: 73 79 6d 62 6f 6c 29 0a 3b 20 28 73 78 70 61 74  symbol).; (sxpat
42a0: 68 31 20 70 72 6f 63 65 64 75 72 65 29 20 20 20  h1 procedure)   
42b0: 2d 3e 20 70 72 6f 63 65 64 75 72 65 0a 3b 20 28  -> procedure.; (
42c0: 73 78 70 61 74 68 31 20 27 28 3f 73 79 6d 62 6f  sxpath1 '(?symbo
42d0: 6c 20 2e 2e 2e 29 29 20 2d 3e 20 28 73 78 70 61  l ...)) -> (sxpa
42e0: 74 68 31 20 27 28 28 3f 73 79 6d 62 6f 6c 29 20  th1 '((?symbol) 
42f0: 2e 2e 2e 29 29 0a 3b 20 28 73 78 70 61 74 68 31  ...)).; (sxpath1
4300: 20 27 28 70 61 74 68 20 72 65 64 75 63 65 72 20   '(path reducer 
4310: 2e 2e 2e 29 29 20 2d 3e 0a 3b 09 09 28 6e 6f 64  ...)) ->.;..(nod
4320: 65 2d 72 65 64 75 63 65 20 28 73 78 70 61 74 68  e-reduce (sxpath
4330: 20 70 61 74 68 29 20 28 73 78 70 61 74 68 72 20   path) (sxpathr 
4340: 72 65 64 75 63 65 72 29 20 2e 2e 2e 29 0a 3b 20  reducer) ...).; 
4350: 28 73 78 70 61 74 68 72 20 6e 75 6d 62 65 72 29  (sxpathr number)
4360: 20 20 20 20 20 20 2d 3e 20 28 6e 6f 64 65 2d 70        -> (node-p
4370: 6f 73 20 6e 75 6d 62 65 72 29 0a 3b 20 28 73 78  os number).; (sx
4380: 70 61 74 68 72 20 70 61 74 68 2d 66 69 6c 74 65  pathr path-filte
4390: 72 29 20 2d 3e 20 28 66 69 6c 74 65 72 20 28 73  r) -> (filter (s
43a0: 78 70 61 74 68 20 70 61 74 68 2d 66 69 6c 74 65  xpath path-filte
43b0: 72 29 29 0a 0a 28 64 65 66 69 6e 65 20 28 73 78  r))..(define (sx
43c0: 70 61 74 68 20 70 61 74 68 29 0a 20 20 28 6c 61  path path).  (la
43d0: 6d 62 64 61 20 28 6e 6f 64 65 73 65 74 29 0a 20  mbda (nodeset). 
43e0: 20 20 20 28 6c 65 74 20 6c 6f 6f 70 20 28 28 6e     (let loop ((n
43f0: 6f 64 65 73 65 74 20 6e 6f 64 65 73 65 74 29 20  odeset nodeset) 
4400: 28 70 61 74 68 20 70 61 74 68 29 29 0a 20 20 20  (path path)).   
4410: 20 28 63 6f 6e 64 0a 20 20 20 20 20 28 28 6e 75   (cond.     ((nu
4420: 6c 6c 3f 20 70 61 74 68 29 20 6e 6f 64 65 73 65  ll? path) nodese
4430: 74 29 0a 20 20 20 20 20 28 28 6e 6f 64 65 73 65  t).     ((nodese
4440: 74 3f 20 6e 6f 64 65 73 65 74 29 0a 20 20 20 20  t? nodeset).    
4450: 20 20 28 6d 61 70 2d 75 6e 69 6f 6e 20 28 73 78    (map-union (sx
4460: 70 61 74 68 20 70 61 74 68 29 20 6e 6f 64 65 73  path path) nodes
4470: 65 74 29 29 0a 20 20 20 20 20 28 28 70 72 6f 63  et)).     ((proc
4480: 65 64 75 72 65 3f 20 28 63 61 72 20 70 61 74 68  edure? (car path
4490: 29 29 0a 20 20 20 20 20 20 28 6c 6f 6f 70 20 28  )).      (loop (
44a0: 28 63 61 72 20 70 61 74 68 29 20 6e 6f 64 65 73  (car path) nodes
44b0: 65 74 29 20 28 63 64 72 20 70 61 74 68 29 29 29  et) (cdr path)))
44c0: 0a 20 20 20 20 20 28 28 65 71 3f 20 27 2f 2f 20  .     ((eq? '// 
44d0: 28 63 61 72 20 70 61 74 68 29 29 0a 20 20 20 20  (car path)).    
44e0: 20 20 28 6c 6f 6f 70 0a 20 20 20 20 20 20 20 28    (loop.       (
44f0: 28 69 66 20 28 6e 6f 64 65 73 65 74 3f 20 6e 6f  (if (nodeset? no
4500: 64 65 73 65 74 29 20 61 70 70 65 6e 64 20 63 6f  deset) append co
4510: 6e 73 29 20 6e 6f 64 65 73 65 74 0a 09 28 28 6e  ns) nodeset..((n
4520: 6f 64 65 2d 63 6c 6f 73 75 72 65 20 28 6e 6f 64  ode-closure (nod
4530: 65 2d 74 79 70 65 6f 66 3f 20 27 2a 61 6e 79 2a  e-typeof? '*any*
4540: 29 29 20 6e 6f 64 65 73 65 74 29 29 0a 20 20 20  )) nodeset)).   
4550: 20 20 20 20 28 63 64 72 20 70 61 74 68 29 29 29      (cdr path)))
4560: 0a 20 20 20 20 20 28 28 73 79 6d 62 6f 6c 3f 20  .     ((symbol? 
4570: 28 63 61 72 20 70 61 74 68 29 29 0a 20 20 20 20  (car path)).    
4580: 20 20 28 6c 6f 6f 70 20 28 28 73 65 6c 65 63 74    (loop ((select
4590: 2d 6b 69 64 73 20 28 6e 6f 64 65 2d 74 79 70 65  -kids (node-type
45a0: 6f 66 3f 20 28 63 61 72 20 70 61 74 68 29 29 29  of? (car path)))
45b0: 20 6e 6f 64 65 73 65 74 29 0a 09 20 20 20 20 28   nodeset)..    (
45c0: 63 64 72 20 70 61 74 68 29 29 29 0a 20 20 20 20  cdr path))).    
45d0: 20 28 28 61 6e 64 20 28 70 61 69 72 3f 20 28 63   ((and (pair? (c
45e0: 61 72 20 70 61 74 68 29 29 20 28 65 71 3f 20 27  ar path)) (eq? '
45f0: 65 71 75 61 6c 3f 20 28 63 61 61 72 20 70 61 74  equal? (caar pat
4600: 68 29 29 29 0a 20 20 20 20 20 20 28 6c 6f 6f 70  h))).      (loop
4610: 20 28 28 73 65 6c 65 63 74 2d 6b 69 64 73 20 28   ((select-kids (
4620: 61 70 70 6c 79 20 6e 6f 64 65 2d 65 71 75 61 6c  apply node-equal
4630: 3f 20 28 63 64 61 72 20 70 61 74 68 29 29 29 20  ? (cdar path))) 
4640: 6e 6f 64 65 73 65 74 29 0a 09 20 20 20 20 28 63  nodeset)..    (c
4650: 64 72 20 70 61 74 68 29 29 29 0a 20 20 20 20 20  dr path))).     
4660: 28 28 61 6e 64 20 28 70 61 69 72 3f 20 28 63 61  ((and (pair? (ca
4670: 72 20 70 61 74 68 29 29 20 28 65 71 3f 20 27 65  r path)) (eq? 'e
4680: 71 3f 20 28 63 61 61 72 20 70 61 74 68 29 29 29  q? (caar path)))
4690: 0a 20 20 20 20 20 20 28 6c 6f 6f 70 20 28 28 73  .      (loop ((s
46a0: 65 6c 65 63 74 2d 6b 69 64 73 20 28 61 70 70 6c  elect-kids (appl
46b0: 79 20 6e 6f 64 65 2d 65 71 3f 20 28 63 64 61 72  y node-eq? (cdar
46c0: 20 70 61 74 68 29 29 29 20 6e 6f 64 65 73 65 74   path))) nodeset
46d0: 29 0a 09 20 20 20 20 28 63 64 72 20 70 61 74 68  )..    (cdr path
46e0: 29 29 29 0a 20 20 20 20 20 28 28 70 61 69 72 3f  ))).     ((pair?
46f0: 20 28 63 61 72 20 70 61 74 68 29 29 0a 20 20 20   (car path)).   
4700: 20 20 20 28 6c 65 74 20 72 65 64 75 63 65 72 20     (let reducer 
4710: 28 28 6e 6f 64 65 73 65 74 20 0a 09 09 20 20 20  ((nodeset ...   
4720: 20 20 28 69 66 20 28 73 79 6d 62 6f 6c 3f 20 28    (if (symbol? (
4730: 63 61 61 72 20 70 61 74 68 29 29 0a 09 09 09 20  caar path)).... 
4740: 28 28 73 65 6c 65 63 74 2d 6b 69 64 73 20 28 6e  ((select-kids (n
4750: 6f 64 65 2d 74 79 70 65 6f 66 3f 20 28 63 61 61  ode-typeof? (caa
4760: 72 20 70 61 74 68 29 29 29 20 6e 6f 64 65 73 65  r path))) nodese
4770: 74 29 0a 09 09 09 20 28 6c 6f 6f 70 20 6e 6f 64  t).... (loop nod
4780: 65 73 65 74 20 28 63 61 61 72 20 70 61 74 68 29  eset (caar path)
4790: 29 29 29 0a 09 09 20 20 20 20 28 72 65 64 75 63  )))...    (reduc
47a0: 69 6e 67 2d 70 61 74 68 20 28 63 64 61 72 20 70  ing-path (cdar p
47b0: 61 74 68 29 29 29 0a 09 28 63 6f 6e 64 0a 09 20  ath)))..(cond.. 
47c0: 28 28 6e 75 6c 6c 3f 20 72 65 64 75 63 69 6e 67  ((null? reducing
47d0: 2d 70 61 74 68 29 20 28 6c 6f 6f 70 20 6e 6f 64  -path) (loop nod
47e0: 65 73 65 74 20 28 63 64 72 20 70 61 74 68 29 29  eset (cdr path))
47f0: 29 0a 09 20 28 28 6e 75 6d 62 65 72 3f 20 28 63  ).. ((number? (c
4800: 61 72 20 72 65 64 75 63 69 6e 67 2d 70 61 74 68  ar reducing-path
4810: 29 29 0a 09 20 20 28 72 65 64 75 63 65 72 20 28  ))..  (reducer (
4820: 28 6e 6f 64 65 2d 70 6f 73 20 28 63 61 72 20 72  (node-pos (car r
4830: 65 64 75 63 69 6e 67 2d 70 61 74 68 29 29 20 6e  educing-path)) n
4840: 6f 64 65 73 65 74 29 0a 09 09 20 20 20 28 63 64  odeset)...   (cd
4850: 72 20 72 65 64 75 63 69 6e 67 2d 70 61 74 68 29  r reducing-path)
4860: 29 29 0a 09 20 28 65 6c 73 65 0a 09 20 20 28 72  )).. (else..  (r
4870: 65 64 75 63 65 72 20 28 28 66 69 6c 74 65 72 20  educer ((filter 
4880: 28 73 78 70 61 74 68 20 28 63 61 72 20 72 65 64  (sxpath (car red
4890: 75 63 69 6e 67 2d 70 61 74 68 29 29 29 20 6e 6f  ucing-path))) no
48a0: 64 65 73 65 74 29 0a 09 09 20 20 20 28 63 64 72  deset)...   (cdr
48b0: 20 72 65 64 75 63 69 6e 67 2d 70 61 74 68 29 29   reducing-path))
48c0: 29 29 29 29 0a 20 20 20 20 20 28 65 6c 73 65 0a  )))).     (else.
48d0: 20 20 20 20 20 20 28 65 72 72 6f 72 20 22 49 6e        (error "In
48e0: 76 61 6c 69 64 20 70 61 74 68 20 73 74 65 70 3a  valid path step:
48f0: 20 22 20 28 63 61 72 20 70 61 74 68 29 29 29 0a   " (car path))).
4900: 29 29 29 29 0a                                   )))).