src/Pure/General/xml.ML
changeset 25838 00b2a1b2c4e9
parent 24584 01e83ffa6c54
child 26525 14a56f013469
equal deleted inserted replaced
25837:2a7efcfe9b54 25838:00b2a1b2c4e9
     8 signature XML =
     8 signature XML =
     9 sig
     9 sig
    10   (*string functions*)
    10   (*string functions*)
    11   val header: string
    11   val header: string
    12   val text: string -> string
    12   val text: string -> string
    13   val text_charref: string -> string
       
    14   val cdata: string -> string
       
    15   type attributes = (string * string) list
    13   type attributes = (string * string) list
    16   val attribute: string * string -> string
    14   val attribute: string * string -> string
    17   val element: string -> attributes -> string list -> string
    15   val element: string -> attributes -> string list -> string
    18   (*tree functions*)
    16   (*tree functions*)
    19   datatype tree =
    17   datatype tree =
    20       Elem of string * attributes * tree list
    18       Elem of string * attributes * tree list
    21     | Text of string
    19     | Text of string
    22     | Output of output
    20     | Output of output
    23   type content = tree list
    21   type content = tree list
    24   type element = string * attributes * content
    22   type element = string * attributes * content
       
    23   val buffer_of_tree: tree -> Buffer.T
    25   val string_of_tree: tree -> string
    24   val string_of_tree: tree -> string
    26   val buffer_of_tree: tree -> Buffer.T
    25   val plain_content: tree -> string
    27   val parse_string : string -> string option
    26   val parse_string : string -> string option
    28   val parse_content: string list -> tree list * string list
    27   val parse_content: string list -> tree list * string list
    29   val parse_elem: string list -> tree * string list
    28   val parse_elem: string list -> tree * string list
    30   val parse_document: string list -> (string option * tree) * string list
    29   val parse_document: string list -> (string option * tree) * string list
    31   val tree_of_string: string -> tree
    30   val tree_of_string: string -> tree
    32   val scan_comment_whspc : string list -> unit * string list
    31   val scan_comment_whspc: string list -> unit * string list
    33 end;
    32 end;
    34 
    33 
    35 structure XML: XML =
    34 structure XML: XML =
    36 struct
    35 struct
    37 
    36 
    54   | encode "&" = "&"
    53   | encode "&" = "&"
    55   | encode "'" = "'"
    54   | encode "'" = "'"
    56   | encode "\"" = """
    55   | encode "\"" = """
    57   | encode c = c;
    56   | encode c = c;
    58 
    57 
    59 fun encode_charref c = "&#" ^ Int.toString (ord c) ^ ";"
    58 val text = translate_string encode;
    60 
       
    61 val text = Library.translate_string encode;
       
    62 
       
    63 val text_charref = translate_string encode_charref;
       
    64 
       
    65 val cdata = enclose "<![CDATA[" "]]>\n";
       
    66 
    59 
    67 
    60 
    68 (* elements *)
    61 (* elements *)
    69 
    62 
    70 fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
    63 fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
    85     Elem of string * attributes * tree list
    78     Elem of string * attributes * tree list
    86   | Text of string
    79   | Text of string
    87   | Output of output;
    80   | Output of output;
    88 
    81 
    89 type content = tree list;
    82 type content = tree list;
    90 
       
    91 type element = string * attributes * content;
    83 type element = string * attributes * content;
    92 
    84 
    93 fun buffer_of_tree tree =
    85 fun buffer_of_tree tree =
    94   let
    86   let
    95     fun string_of (Elem (name, atts, ts)) buf =
    87     fun string_of (Elem (name, atts, ts)) buf =
   107       | string_of (Text s) buf = Buffer.add (text s) buf
    99       | string_of (Text s) buf = Buffer.add (text s) buf
   108       | string_of (Output s) buf = Buffer.add s buf;
   100       | string_of (Output s) buf = Buffer.add s buf;
   109   in string_of tree Buffer.empty end;
   101   in string_of tree Buffer.empty end;
   110 
   102 
   111 val string_of_tree = Buffer.content o buffer_of_tree;
   103 val string_of_tree = Buffer.content o buffer_of_tree;
       
   104 
       
   105 fun plain_content tree =
       
   106   let
       
   107     fun content (Elem (_, _, ts)) = fold content ts
       
   108       | content (Text s) = Buffer.add s
       
   109       | content (Output _) = I;    (* FIXME !? *)
       
   110   in Buffer.empty |> content tree |> Buffer.content end;
   112 
   111 
   113 
   112 
   114 
   113 
   115 (** XML parsing **)
   114 (** XML parsing **)
   116 
   115