xml.ml 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. open Astring
  2. let ns_rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  3. let ns_xsd = "http://www.w3.org/2001/XMLSchema"
  4. (* and ns_sec = As2_vocab.Constants.ActivityStreams.ns_sec ^ "#" *)
  5. let ns_a = "http://www.w3.org/2005/Atom" (* https://www.rfc-editor.org/rfc/rfc4287#section-2 *)
  6. let ns_thr = "http://purl.org/syndication/thread/1.0" (* https://www.rfc-editor.org/rfc/rfc4685#section-2 *)
  7. let ns_seppo = "http://seppo.social/2023/ns#"
  8. let ns_backoffice = "http://seppo.social/2023/backoffice#"
  9. let ns_rfc7033 = "urn:ietf:rfc:7033"
  10. let ns_rfc7565 = "urn:ietf:rfc:7565"
  11. let ns_as = As2_vocab.Constants.ActivityStreams.ns_as
  12. let ns_xhtml = "http://www.w3.org/1999/xhtml"
  13. (* for testing purpose only *)
  14. let to_buf ?(xsl = None) ?(readme = None) ?(indent = None) (x : _ Xmlm.frag) (dst : Buffer.t) =
  15. let piw n v =
  16. (* TODO check syntax *)
  17. Printf.bprintf dst "<?%s %s?>\n" n v in
  18. piw "xml" "version=\"1.0\"";
  19. (match xsl with
  20. | Some v -> piw "xml-stylesheet" (Printf.sprintf "type='text/xsl' href='%s'" v)
  21. | None -> ());
  22. (match readme with
  23. | Some v -> Printf.bprintf dst "<!--%s-->\n" v
  24. | None -> ());
  25. let o = Xmlm.make_output ~decl:false ~indent (`Buffer dst) in
  26. Xmlm.output_doc_tree (fun x -> x) o (None,x)
  27. let pi oc n l =
  28. assert (String.exists (fun c -> c == '"') n == false);
  29. let w = output_string oc in
  30. w "<?"; w n;
  31. l |> List.fold_left
  32. (fun _ (k,v) ->
  33. assert (String.exists (fun c -> c == '"') v == false);
  34. w " "; w k; w "=\""; w v; w "\""
  35. ) ();
  36. w "?>\n"
  37. let to_chan ?(xsl = None) ?(readme = None) ?(indent = None) (x : _ Xmlm.frag) dst =
  38. pi dst "xml" ["version","1.0"];
  39. (match xsl with
  40. | Some v -> pi dst "xml-stylesheet" ["type","text/xsl"; "href",v]
  41. | None -> ());
  42. (match readme with
  43. | Some v -> Printf.fprintf dst "<!--%s-->\n" v
  44. | None -> ());
  45. let o = Xmlm.make_output ~decl:false ~indent (`Channel dst) in
  46. Xmlm.output_doc_tree (fun x -> x) o (None,x)