1 (* Title: Pure/General/xml.ML
3 Author: David Aspinall, Stefan Berghofer and Markus Wenzel
11 val text: string -> string
12 val text_charref: string -> string
13 val cdata: string -> string
14 val element: string -> (string * string) list -> string list -> string
16 Elem of string * (string * string) list * tree list
18 val string_of_tree: tree -> string
19 val parse_content: string list -> tree list * string list
20 val parse_elem: string list -> tree * string list
21 val parse_document: string list -> (string option * tree) * string list
22 val scan_comment_whspc : string list -> unit * string list
23 val tree_of_string: string -> tree
29 (** string based representation (small scale) **)
31 val header = "<?xml version=\"1.0\"?>\n";
34 (* text and character data *)
36 fun decode "<" = "<"
38 | decode "&" = "&"
39 | decode "'" = "'"
40 | decode """ = "\""
43 fun encode "<" = "<"
45 | encode "&" = "&"
46 | encode "'" = "'"
47 | encode "\"" = """
50 fun encode_charref c = "&#"^Int.toString (Char.ord c)^ ";"
52 val text = Library.translate_string encode
54 val text_charref = implode o (map encode_charref) o String.explode
56 val cdata = enclose "<![CDATA[" "]]>\n"
61 fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
63 fun element name atts cs =
64 let val elem = space_implode " " (name :: map attribute atts) in
65 if null cs then enclose "<" "/>" elem
66 else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
72 (** explicit XML trees **)
75 Elem of string * (string * string) list * tree list
78 fun string_of_tree tree =
80 fun string_of (Elem (name, atts, ts)) buf =
83 |> fold Buffer.add (separate " " (name :: map attribute atts))
86 buf' |> Buffer.add "/>"
88 buf' |> Buffer.add ">"
90 |> Buffer.add "</" |> Buffer.add name |> Buffer.add ">"
92 | string_of (Text s) buf = Buffer.add (text s) buf;
93 in Buffer.content (string_of tree Buffer.empty) end;
100 "XML parsing error: " ^ s ^ "\nfound: " ^ quote (Symbol.beginning 100 xs);
102 val scan_whspc = Scan.any Symbol.is_blank;
104 val scan_special = $$ "&" ^^ Symbol.scan_id ^^ $$ ";" >> decode;
106 val parse_chars = Scan.repeat1 (Scan.unless (scan_whspc -- $$ "<")
107 (scan_special || Scan.one Symbol.not_eof)) >> implode;
109 val parse_cdata = Scan.this_string "<![CDATA[" |--
110 (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.not_eof)) >>
111 implode) --| Scan.this_string "]]>";
114 Symbol.scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
115 (($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s)
116 (scan_special || Scan.one Symbol.not_eof)) >> implode) --| $$ s) >> snd);
118 val parse_comment = Scan.this_string "<!--" --
119 Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.not_eof)) --
120 Scan.this_string "-->";
122 val scan_comment_whspc =
123 (scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K())));
125 val parse_pi = Scan.this_string "<?" |--
126 Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.not_eof)) --|
127 Scan.this_string "?>";
129 fun parse_content xs =
130 ((Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) [] --
131 (Scan.repeat (scan_whspc |--
132 ( parse_elem >> single
133 || parse_cdata >> (single o Text)
135 || parse_comment >> K []) --
136 Scan.optional (scan_whspc |-- parse_chars >> (single o Text)) []
137 >> op @) >> flat) >> op @) --| scan_whspc) xs
140 ($$ "<" |-- Symbol.scan_id --
141 Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
142 !! (err "Expected > or />")
143 (Scan.this_string "/>" >> K []
144 || $$ ">" |-- parse_content --|
145 !! (err ("Expected </" ^ s ^ ">"))
146 (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
147 (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
150 Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
151 (Scan.repeat (Scan.unless ($$ ">")
152 (Scan.one Symbol.not_eof)) >> implode) --| $$ ">" --| scan_whspc) --
155 fun tree_of_string s =
156 (case Scan.finite Symbol.stopper (Scan.error (!! (err "Malformed element")
157 (scan_whspc |-- parse_elem --| scan_whspc))) (Symbol.explode s) of
159 | (_, ys) => error ("XML parsing error: Unprocessed input\n" ^ Symbol.beginning 100 ys));