Search code examples
ocamlppx

Are there any usage examples for the OCaml ppx_xml_conv module


I'm looking for a simple example for the ppx_xml_conv module from janestreet. I'm not terribly familiar with the (relatively) new ppx thing and can't really figure it out from the source code. Ultimately, I'm trying to write a client for an old SOAP service, and want to turn the xsd (from the wsdl) into a type and serializer/deserializer.


Solution

  • Since there is a bug in ppx_xml_conv, I'll give an example for ppx_sexp_conv which works identically.

    $ cat a.ml
    open Sexplib.Std
    
    type attr = {
      attr_id : string;
      attr_path : string;
      attr_value : string;
    } [@@deriving sexp]
    
    $ cat a.mli
    type attr = {
      attr_id : string;
      attr_path : string;
      attr_value : string;
    } [@@deriving sexp]
    
    $ ocamlfind ocamlc -package sexplib,ppx_sexp_conv -dsource -c a.mli
    type attr = {
      attr_id: string;
      attr_path: string;
      attr_value: string;}[@@deriving sexp]
    val attr_of_sexp : Sexplib.Sexp.t -> attr
    val sexp_of_attr : attr -> Sexplib.Sexp.t
    
    $ ocamlfind ocamlc -package sexplib,ppx_sexp_conv -dsource -c a.ml
    (* ... long output ... *)
    

    I used the -dsource flag so you can see the generated output. Note that it wasn't necessary to create a ppx executable and call it separately. Compiling with the ppx_sexp_conv package causes that package's ppx extension to automatically get applied.

    As another example here's an executable:

    $ cat b.ml
    open Sexplib.Std
    
    type attr = {
      attr_id : string;
      attr_path : string;
      attr_value : string;
    } [@@deriving sexp]
    
    let x = {attr_id="abc"; attr_path="foo/bar"; attr_value="something"}
    
    let () = Printf.printf "sexp: %s\n" (Sexplib.Sexp.to_string (sexp_of_attr x))
    
    $ ocamlfind ocamlc -package sexplib,ppx_sexp_conv -linkpkg b.ml
    
    $ ./a.out 
    sexp: ((attr_id abc)(attr_path foo/bar)(attr_value something))