/
xml-parse.r
1151 lines (1087 loc) · 43.8 KB
/
xml-parse.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
REBOL [
Title: "A more XML 1.0 compliant set of XML parsing tools."
File: %xml-parse.r
Date: 4-Dec-2001
Version: 0.7.4
Author: "Gavin F. McKenzie"
Email: gavin.mckenzie@sympatico.ca
Purpose: {
REBOL's built-in parse-xml function lacks a number of
XML 1.0 compliant features, including:
- support for CDATA sections
- support for XML Namespaces
- exposure of the internal DTD subset
The intent of this script is to create an XML parser
that can operate either via an event/callback mechanism,
or produce a block structure similar to REBOL's built-in
parse-xml function.
This XML parser is designed to call-back into a
'parse-handler' object that has been designed to be similar
to the well-known XML parsing interface known as "SAX"
(Simple API for XML) by David Megginson.
For more information on SAX, see David's website at:
http://www.megginson.com/SAX/index.html
Several parse-handlers are included here:
- the base 'class' xml-parse-handler that contains only empty
callback stubs
- the debugging aid echo-handler that prints out the callback
event stream
- the block-handler that produces a superset of the block structure
created by REBOL's built-in parse-xml function
Alternatively, you may choose to build your own parse
event handler rather than use the functionality provided
here.
A new function is defined "parse-xml+" that represents
the enhanced counterpart to the built-in REBOL parse-xml.
Additional features provided by this parser:
1. Document prolog information
The built-in REBOL parse-xml function returns a set of
nested blocks where the first two items in the outermost
block are the words document and none, such as:
[document none [...]]
The parse-xml+ function provided herein can produce a
set of nested blocks where the second item of the outermost
block is used to represent prolog and document type
information.
An example of this block is:
[ version "1.0" encoding "utf-8" standalone "yes"
doctype none pubid none sysid none subset none
]
2. CDATA Section Processing
XML provides for enclosing data content within CDATA
sections for the convenience of avoiding the need to
escape certain XML sensitive characters in the data
such as the ampersand (&) and less-than-sign (<).
An example of a CDATA section:
<foo>abc <![CDATA[Jack & Jill]]> xyz</foo>
A compliant XML parser would report that the content
of element 'foo' is "abc Jack & Jill xyz".
CDATA sections are also useful when putting text samples
of XML within the content of an XML document.
<example><![CDATA[<foo>bar</foo>]]></example>
Here the value of element 'example' is the text
"<foo>bar</foo>"
3. Comments
This parser provides the opportunity to process
comments embedded within the XML.
4. Processing Instructions
This parser provides the opportunity to process
processing instructions embedded within the XML.
<foo><?my-app this is a processing instruction?></foo>
5. Automatic Character Entity Expansion
In XML document it is common to encounter "character
entities" within the content of the document. These
entities are the means for escaping sensitive XML
characters so that the character will be processed as
data rather than markup. The most common characters
that are subjected to this treatment are the
ampersand (&) and less-than-sign (<).
This parser recognizes these common entities and
automatically converts them to their character
equivalents.
For example:
<foo>Jack & Jill</foo>
This parser will automatically replace the &
character entity reference to the ampersand (&)
character; hence, the value of element 'foo' is
"Jack & Jill".
Character entities can also be encoded with their
Unicode numeric equivalent rather than the symbolic
name in either decimal or hex form, such as:
<foo>Ampersands: &&&</foo>
The value of element 'foo' is "Ampersands: &&&".
6. Namespace Processing
Namespace processing is vital to handling real-world
XML.
@@TBD: say more here
}
History: [
0.7.4 { Fixed a defect to allow optional space around
the '=' on an attribute.
Thanks to Brett Handley for reporting the defect.}
0.7.3 { Fixed bug where attr-ns-prefix wasn't getting cleared
when processing an un-prefixed attribute.}
0.7.2 { Changed the start-document in the block-handler
to perform a copy/deep, fixing a bug that occurred
on successive invocations of parse-xml+.}
0.7.1 { First public release. }
]
]
; TO DO
; - ** WARNING ** Namespace processing is not ready for primetime!
; - anything that uses xmlQuote is wrong; there is the potential for
; uncaught mismatched quotes
; - do some start/end-tag matching and error-checking
; - process entities defined in the internal DTD subset
; - add comments, comments, comments!!
xml-parse: make object! [
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; XML PARSE CONTENT HANDLERS
;
; This XML parser is designed to call-back into a 'parse-handler' object
; that has been designed to be similar to the well-known XML parsing
; interface known as "SAX" (Simple API for XML) by David Megginson.
;
; For more information on SAX, see David's website at:
; http://www.megginson.com/SAX/index.html
;
; Several parse-handlers are included here:
; - the base 'class' xml-parse-handler that contains only empty
; callback stubs
; - the debugging aid echo-handler that prints out the callback
; event stream
; - the block-handler that produces a superset of the block structure
; created by REBOL's built-in parse-xml function
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This is an 'empty' xml-parse-handler that is provided as a base-object
; for extending into custom xml-parse-handlers.
;
xml-parse-handler: make object! [
start-document: func [
][
]
xml-decl: func [
version-info [string! none!]
encoding [string! none!]
standalone [string! none!]
][
]
document-type: func [
document-type [string!]
public-id [string! none!]
system-id [string! none!]
internal-subset [string! none!]
][
]
start-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
attr-list [block!]
][
]
end-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
][
]
characters: func [
characters [string! none!]
][
]
pi: func [
pi-target [string! none!]
pi [string! none!]
][
]
comment: func [
comment [string! none!]
][
]
end-document: func [] [
]
start-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
]
end-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
]
get-parse-result: func [{
This function can be used to return a specific result from
the parse operation, such as returning the parse XML as a
series of blocks similar to REBOL's built-in parse-xml.
By default, returning none from this function will cause the
return code from the REBOL parse function to be passed back
to the caller of the parse.}
][
none
]
]
;
; This xml-parse-handler simply echoes the parsing to the console.
; This was primarily useful as a debugging aid during the development
; of the XML parse production rules.
;
echo-handler: make xml-parse-handler [
start-document: func [
][
print remold ['start-doc]
]
xml-decl: func [
version-info [string! none!]
encoding [string! none!]
standalone [string! none!]
][
print remold ['xml-decl 'version-info version-info
'encoding encoding 'standalone standalone
]
]
document-type: func [
document-type [string!]
public-id [string! none!]
system-id [string! none!]
internal-subset [string! none!]
][
print remold ['doc-type document-type
'public-id public-id
'system-id system-id
'internal-subset internal-subset
]
]
start-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
attr-list [block!]
][
print remold ['start-elem ns-uri local-name q-name
'attr-list attr-list
]
]
end-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
][
print remold ['end-elem ns-uri local-name q-name]
]
characters: func [
characters [string! none!]
][
print remold ['characters characters]
]
pi: func [
pi-target [string! none!]
pi [string! none!]
][
print remold ['pi pi-target pi]
]
comment: func [
comment [string! none!]
][
print remold ['comment comment]
]
end-document: func [
][
print remold ['end-doc]
]
start-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
print remold ['start-prefix ns-prefix-uri-pairs]
]
end-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
print remold ['end-prefix ns-prefix-uri-pairs]
]
]
;
; This xml-parse-handler produces a set of nested blocks representing
; the parsed XML content. The blocks structure is a compatible superset
; of the block structure produced by REBOL's built-in parse-xml function.
; Extensions to the structure are appended to any applicable block;
; hence, existing code intended for use with REBOL's existing post-parse
; block structure should continue to work.
;
block-handler: make xml-parse-handler [
xml-doc: copy []
xml-block: copy []
xml-content: copy ""
start-document: func [
][
;
; Seed the document
;
xml-block: reduce copy/deep ['document [version none
encoding none
standalone none
doctype none
pubid none
sysid none
subset none
]
none
]
]
xml-decl: func [
version-info [string! none!]
encoding [string! none!]
standalone [string! none!]
][
change next (find xml-block/2 'version) version-info
change next (find xml-block/2 'encoding) encoding
change next (find xml-block/2 'standalone) standalone
]
document-type: func [
document-type [string!]
public-id [string! none!]
system-id [string! none!]
internal-subset [string! none!]
][
change next (find xml-block/2 'doctype) document-type
change next (find xml-block/2 'pubid) public-id
change next (find xml-block/2 'sysid) system-id
change next (find xml-block/2 'subset) internal-subset
]
start-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
attr-list [block!]
][
;
; Is there any pending content to add before
; we start a new element?
;
if not empty? xml-content [
add-child copy xml-content
clear head xml-content
]
insert/only tail xml-doc xml-block
xml-block: add-child copy reduce [local-name none none]
;
; Add the attribute list
;
if not empty? attr-list [
xml-block/2: copy attr-list
]
]
characters: func [
characters [string! none!]
][
;
; Accumulate more character data
;
if not none? characters [
append xml-content characters
]
]
end-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
][
;
; Is there any pending content to add before
; we terminate this element?
;
if not empty? xml-content [
add-child copy xml-content
clear head xml-content
]
;
; Basic well-formedness check
;
; while [q-name <> first xml-block] [
; if empty? xml-doc [
; print ["End tag error:" q-name]
; halt
; ]
; pop-xml-block
; ]
pop-xml-block
]
add-child: func [child] [
if none? third xml-block [xml-block/3: make block! 1]
insert/only tail third xml-block child
child
]
pop-xml-block: func [] [
xml-block: last xml-doc
remove back tail xml-doc
]
get-parse-result: func [] [
xml-block
]
]
;
; This xml-parse-handler enhances the block-handler with namespace
; processing. It should only be used with a parser that has been
; set to namespace-aware true.
;
ns-block-handler: make xml-parse-handler [
xml-doc: copy []
xml-block: copy []
xml-content: copy ""
nsinfo-stack: copy []
start-document: func [
][
;
; Seed the document
;
xml-block: reduce copy/deep ['document [version none
encoding none
standalone none
doctype none
pubid none
sysid none
subset none
]
none
]
]
xml-decl: func [
version-info [string! none!]
encoding [string! none!]
standalone [string! none!]
][
change next (find xml-block/2 'version) version-info
change next (find xml-block/2 'encoding) encoding
change next (find xml-block/2 'standalone) standalone
]
document-type: func [
document-type [string!]
public-id [string! none!]
system-id [string! none!]
internal-subset [string! none!]
][
change next (find xml-block/2 'doctype) document-type
change next (find xml-block/2 'pubid) public-id
change next (find xml-block/2 'sysid) system-id
change next (find xml-block/2 'subset) internal-subset
]
start-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
attr-list [block!]
][
;
; Is there any pending content to add before
; we start a new element?
;
if not empty? xml-content [
add-child copy xml-content
clear head xml-content
]
insert/only tail xml-doc xml-block
xml-block: add-child copy reduce [local-name
none
none
ns-uri
]
;
; Add the attribute list
;
if not empty? attr-list [
xml-block/2: copy attr-list
]
]
characters: func [
characters [string! none!]
][
;
; Accumulate more character data
;
if not none? characters [
append xml-content characters
]
]
end-element: func [
ns-uri [string! none!]
local-name [string! none!]
q-name [string!]
][
;
; Is there any pending content to add before
; we terminate this element?
;
if not empty? xml-content [
add-child copy xml-content
clear head xml-content
]
;
; Basic well-formedness check
;
; while [q-name <> first xml-block] [
; if empty? xml-doc [
; print ["End tag error:" q-name]
; halt
; ]
; pop-xml-block
; ]
pop-xml-block
]
start-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
insert/only nsinfo-stack ns-prefix-uri-pairs
]
end-prefix-mapping: func [
ns-prefix-uri-pairs [block!]
][
remove nsinfo-stack
]
add-child: func [child] [
if none? third xml-block [xml-block/3: make block! 1]
insert/only tail third xml-block child
child
]
pop-xml-block: func [] [
xml-block: last xml-doc
remove back tail xml-doc
]
get-parse-result: func [] [
xml-block
]
]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; ACTUAL XML PARSER OBJECT
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
parser: make object! [
element-q-name: none
element-local-name: none
document-type: none
system-id: none
public-id: none
internal-subset: none
encoding: none
characters: none
entity-ref: none
char-ref-value: none
comment: none
pi-target: none
pi: none
version-info: none
encoding: none
standalone: none
attr-name: none
attr-value: none
attr-list: copy []
attr-ns-prefix: none
element-ns-prefix: none
ns-uri: none
nsinfo-stack: copy []
nsinfo: copy []
;
; Set a default xml-parse-handler
;
handler: block-handler
;
; Namespace processing, set true to process namespaces
;
namespace-aware: no
;
; XML 1.0 Production Rules
;
xmlLetter: charset [#"A" - #"Z" #"a" - #"z"]
xmlDigit: charset [#"0" - #"9"]
xmlHexDigit: charset [#"0" - #"9" #"a" - #"f" #"A" - #"F"]
xmlAlpha: charset [#"A" - #"Z" #"a" - #"z"]
xmlAlphaNum: charset [#"0" - #"9" #"A" - #"Z" #"a" - #"z"]
xmlQuote: charset [#"^"" #"^'"]
xmlSpace: charset [#"^(20)" #"^(09)" #"^(0D)" #"^(0A)"]
xmlNotMarkupChar: complement charset [#"<" #"&"] ;fix this
xmlS: [some xmlSpace]
xmlEq: [[any xmlSpace] "=" [any xmlSpace]]
xmlChar: [any [xmlAlphaNum | xmlSpace]]
xmlCharData: [ copy characters
some xmlNotMarkupChar
(handler/characters characters)
]
xmlNameProd: [[xmlLetter | #"_" | #":"] any xmlNameChar]
xml10Name: xmlNameProd
xmlName: xml10Name
xmlNameChar: [ xmlLetter | xmlDigit |
#"." | #"-" | #"_" | #":"
]
xmlNames: [xmlName any [xmlS xmlName]]
xmlNMToken: [some xmlNameChar]
xmlNMTokens: [xmlNMToken any [xmlS xmlNMToken]]
xmlMisc: [xmlComment | xmlPI | xmlS]
xmlPERef: [#"%" xmlNameProd #"%"]
xmlEntIntro: charset [#"^^" #"^%" #"^&" #"^""]
xmlEntityVal: [#"^"" any [xmlEntIntro | xmlPERef] #"^""]
xmlContent: [any [ xmlElement | xmlComment | xmlPI |
xmlCDSect | xmlCharData | xmlReference
]
] ;fix this
xmlAttValueStrict: [ [#"^"" copy attr-value to #"^"" #"^""] |
[#"'" copy attr-value to #"'" #"'"]
] ; fix this
xmlAttValue: xmlAttValueStrict
xmlAttType: [ xmlStringType |
xmlTokenizedType |
xmlEnumeratedType
]
xml10AttrStrict: [ copy attr-name xmlName
xmlEq
xmlAttValue
(append append
attr-list attr-name attr-value
)
]
xmlAttribute: xml10AttrStrict
xmlSTag: [ #"<"
copy element-q-name xmlName
[ (clear head attr-list clear head nsinfo)
any [xmlS xmlAttribute]
]
[any xmlSpace]
#">"
(either namespace-aware [
handler/start-prefix-mapping nsinfo
insert/only nsinfo-stack copy nsinfo
handler/start-element
ns-uri
element-local-name
element-q-name
attr-list
][
handler/start-element
none
element-q-name
element-q-name
attr-list
]
)
]
xmlETag: [ "</"
copy element-q-name xmlName
[any xmlSpace]
#">"
(either namespace-aware [
handler/end-element
ns-uri
element-local-name
element-q-name
handler/end-prefix-mapping
first nsinfo-stack
remove nsinfo-stack
][
handler/end-element
none
element-q-name
element-q-name
]
)
]
xmlEmptyElemTag: [ #"<"
copy element-q-name xmlName
[ (clear head attr-list clear head nsinfo)
any [xmlS xmlAttribute]
]
[any xmlSpace]
"/>"
(either namespace-aware [
handler/start-prefix-mapping nsinfo
insert/only nsinfo-stack copy nsinfo
handler/start-element
ns-uri
element-local-name
element-q-name
attr-list
][
handler/start-element
none
element-q-name
element-q-name
attr-list
]
characters: none
handler/characters characters
either namespace-aware [
handler/end-element
ns-uri
element-local-name
element-q-name
handler/end-prefix-mapping
first nsinfo-stack
remove nsinfo-stack
][
handler/end-element
none
element-q-name
element-q-name
]
)
]
xmlEmptyElem: [ #"<"
copy element-q-name xmlName
[ (clear head attr-list clear head nsinfo)
any [xmlS xmlAttribute]
]
[any xmlSpace]
"></"
xmlName
[any xmlSpace]
#">"
(either namespace-aware [
handler/start-prefix-mapping nsinfo
insert/only nsinfo-stack copy nsinfo
handler/start-element
ns-uri
element-local-name
element-q-name
attr-list
][
handler/start-element
none
element-q-name
element-q-name
attr-list
]
characters: none
handler/characters characters
either namespace-aware [
handler/end-element
ns-uri
element-local-name
element-q-name
handler/end-prefix-mapping
first nsinfo-stack
remove nsinfo-stack
][
handler/end-element
none
element-q-name
element-q-name
]
)
]
xmlElementStrict: [ xmlEmptyElemTag |
xmlEmptyElem |
[xmlSTag opt xmlContent xmlETag]
]
xmlElement: xmlElementStrict
xmlPI: [ "<?"
copy pi-target xmlPITarget
copy pi to "?>" "?>"
(handler/pi pi-target pi)
]
xmlPITarget: [xmlNameProd]
xmlComment: [ "<!--" copy comment to "-->" "-->"
(handler/comment comment)
]
xmlDecl: [ (version-info: encoding: standalone: none)
"<?xml"
any xmlSpace
xmlVersionInfo
opt [xmlS xmlEncodingDecl]
opt [xmlS xmlSDDecl]
any xmlSpace
"?>"
(handler/xml-decl
version-info
encoding
standalone
)
]
xmlVersionInfo: [ "version"
xmlEq
[ #"^"" copy version-info xmlVersionNum "^"" |
#"^'" copy version-info xmlVersionNum "^'"
]
]
xmlVersionNum: [some [xmlVersionNumChars | "-"]]
xmlVersionNumChars: charset [ #"0" - #"9"
#"A" - #"Z"
#"a" - #"z"
"_.:"
]
xmlEncodingDecl: [ (encoding: none)
"encoding"
xmlEq
[ #"^"" copy encoding xmlEncName "^"" |
#"^'" copy encoding xmlEncName "^'"
]
]
xmlEncName: [xmlAlpha any [xmlEncNameChars]]
xmlEncNameChars: charset [ #"0" - #"9"
#"A" - #"Z"
#"a" - #"z"
"_.-"
]
xmlDocument: [ (handler/start-document)
xmlProlog
xmlElement
any xmlMisc
(handler/end-document)
]
xmlProlog: [ opt xmlDecl
any xmlMisc
opt [xmlDocTypeDecl any xmlMisc]
]
xmlDocTypeDecl: [ "<!DOCTYPE"
xmlS
copy document-type xmlName
opt [xmlS xmlExternalID]
any xmlSpace
"["
copy internal-subset
to "]"
"]"
any xmlSpace ">"
(handler/document-type
document-type
public-id
system-id
internal-subset
)
]
xmlSDDecl: [ (standalone: none)
"standalone"
xmlEq
[ xmlQuote
copy standalone ["yes" | "no"]
xmlQuote
]
]
xmlStringType: "CDATA"
xmlTokenizedType: [ "ID" | "IDREF" | "IDREFS" |
"ENTITY" | "ENTITIES" |
"NMTOKEN" | "NMTOKENS"
]
xmlEnumeratedType: [] ; fix this
xmlReference: [xmlCharRef | xmlEntityRef]
xmlEntityRef: [ ["&" copy entity-ref xmlNameProd ";"]
( char-ref-value:
convert-character-entity entity-ref
either none? char-ref-value [
;
; couldn't convert the
; chararacter-entity, so pass
; it through as character data,
; unconverted
;
handler/characters
rejoin ["&" entity-ref ";"]
][
;
; converted the chararacter-entity
; to a character
;
handler/characters char-ref-value
]
)
]
xmlCharRef: [ [ [ "&"
[copy entity-ref
["#" some xmlDigit]
]
";"
] |
[ "&"
[copy entity-ref
["#x" some xmlHexDigit]
]
";"
]
]
( char-ref-value:
convert-character-entity entity-ref
either none? char-ref-value [
;
; couldn't convert the
; chararacter-entity, so pass
; it through as character data,
; unconverted
;
handler/characters
rejoin ["&" entity-ref ";"]
][
;
; converted the chararacter-entity
; to a character
;
handler/characters char-ref-value
]
)
]
xmlExternalID: [ ["SYSTEM" xmlSpace xmlSystemLiteral] |
["PUBLIC" xmlSpace xmlPubIDLiteral
xmlSpace xmlSystemLiteral
]
]
xmlSystemLiteral: [ [#"^"" copy system-id to #"^"" #"^""] |
[#"'" copy system-id to #"'" #"'"]
]
xmlPubIDLiteral: [ [#"^"" copy public-id to #"^"" #"^""] |
[#"'" copy public-id to #"'" #"'"]
]
xmlNDataDecl: [xmlS "NDATA" xmlS xmlNameProd]
xmlCDSect: [ "<![CDATA["
copy characters to "]]>"
"]]>"
(handler/characters characters)
]
; XML Namespace-Specific Production Rules
;
xmlNSAttribute: [ [ copy attr-name xmlPrefixedAttName
xmlEq xmlAttValue
(ns-uri: copy attr-value
append nsinfo
reduce [attr-ns-prefix
attr-value
]
)
] |
[ copy attr-name xmlDefaultAttName
xmlEq
xmlAttValue
(ns-uri: copy attr-value
append nsinfo
reduce [attr-ns-prefix
attr-value
]
)
] |
[ xmlAQName
xmlEq
xmlAttValue
(append attr-list
reduce [attr-name
attr-value
attr-ns-prefix
]
)