@@ -65,3 +65,59 @@ module Import
6565 | _ -> raise Malformed_stream
6666
6767end
68+
69+ (* * Output *)
70+
71+ type output = [ signal | `Raw of string list ]
72+
73+ module Export
74+ (Xml : Xml_sigs.Iterable )
75+ = struct
76+
77+ let mk ~ns name = (ns, name)
78+
79+ let convert_attributes ~ns attributes =
80+ attributes |> List. map @@ fun attribute ->
81+ let value =
82+ match Xml. acontent attribute with
83+ | AFloat n -> Xml_print. string_of_number n
84+ | AInt n -> string_of_int n
85+ | AStr s -> s
86+ | AStrL (Space, ss ) -> String. concat " " ss
87+ | AStrL (Comma, ss ) -> String. concat " , " ss
88+ in
89+ (mk ~ns (Xml. aname attribute), value)
90+
91+ let (++) x l = Seq. Cons (x, l)
92+ let rec mk_elt ~ns x q () : output Seq.node =
93+ match Xml. content x with
94+ | Empty -> q ()
95+ | Comment s -> `Comment s ++ q
96+ | EncodedPCDATA s -> `Raw [s] ++ q
97+ | PCDATA s -> `Text [s] ++ q
98+ | Entity s -> `Raw [" &" ^ s^ " ;" ] ++ q
99+ | Leaf (name , attributes ) ->
100+ `Start_element (mk ~ns name, convert_attributes ~ns attributes) ++
101+ fun () -> `End_element ++ q
102+ | Node (name , attributes , children ) ->
103+ `Start_element (mk ~ns name, convert_attributes ~ns attributes) ++
104+ mk_list ~ns children q
105+ and mk_list ~ns l q () : output Seq.node =
106+ match l with
107+ | [] -> Seq. Nil
108+ | h :: t -> mk_elt ~ns h (mk_list ~ns t q) ()
109+
110+ let to_seq ?(namespace =" " ) xml : output Seq.t =
111+ mk_elt ~ns: namespace xml Seq. empty
112+ let to_seql ?(namespace =" " ) l : output Seq.t =
113+ mk_list ~ns: namespace l Seq. empty
114+ end
115+
116+ module Typed_export
117+ (Xml : Xml_sigs.Iterable )
118+ (Typed_xml : Xml_sigs.Typed_xml with module Xml := Xml )
119+ = struct
120+ module E = Export (Xml )
121+ let export l =
122+ E. to_seql ~namespace: Typed_xml.Info. namespace @@ Typed_xml. toeltl l
123+ end
0 commit comments