ℂDuce: OCamlDuce: OCamlDuce: code samples and applications

Previous page: OCamlDuce: manual Next page: Funding

Code samples

Parsing XML files

OCamlDuce does not come with any built-in XML parser. However, the Ocamlduce.Load module in the standard library makes it easy to plug existing XML parsers. Here is some code which demonstrate how to do that with three of the most popular OCaml XML parser libraries:

Converting DTD to OCamlDuce types

This tool produces a set of OCamlDuce type declarations from a DTD. It requires PXP.

Note: This application does not use any of the new features, but it can be useful in the development of OCamlDuce applications.

Parsing XML Schema, producing valid XHTML output

This application parses XML Schema Definitions (.xsd files), and produces summaries (toplevel declaration names) in XHTML. OCamlDuce type system ensures that the parser is coherent with the input XML type (any valid XML Schema is accepted) and that the printer is coherent with the output XML type (it is necessarily a valid XHTML document).

Of course, for such a simple transformation, parsing the XML document into an internal representation is not necessary. A direct XML-to-XML transformation would be easy to write. We wanted to illustrate a complex parsing of XML.

It it interesting to introduce errors in the parser schema_loader.ml or the printer dump_schema.ml and see how the type system catches them.

Note: The application uses XML Light to parse XML document.
Note: Some features of XML Schema are not parsed, such as redefine elements or substitution groups.
Note: To compile the application with the provided Makefile, you must make the environment variable OCAMLFIND_CONF point to the $GODI/etc/findlib-ocamlduce.conf file.

String regular expressions

OCamlDuce supports regular expression types and patterns, not only for sequences of XML elements, but also for strings. The following example shows how to use regular expressions to split a string of the form name1=val1,...,namen=valn with n>0 into a list of pairs [ (name1,val1); ...; (namen,valn) ]. The *? operator in regular expressions means ``ungreedy match'' (match the shortest possible subsequence). The last pattern describes precisely strings which are not matched by the other cases. It would be possible to replace it with the wildcard _.

let rec split (s : {{ String }}) =
  match s with
    | {{ [ n::_*? '=' v::_*? ',' rest::_* ] }} -> (n,v)::(split rest)
    | {{ [ n::_*? '=' v::_*? ] }} -> [ (n,v) ]
    | {{ Any - [ _* '=' _* ] }} -> failwith "split"

Applications in OCamlDuce

  • Review2Atom by Anil Madhavapeddy: translates paper review files in XML format into an Atom feed suitable for aggregation.
  • SOSS by Stefan Lampe: an implementation of a SOAP server for OCaml, designed to allow a service, developed in OCaml, to be made available as a SOAP service with minimal effort.
  • AADL4Ocaml by Erwan Jahier and Louis Mandel: a parser for a subset of the AADL language (Architecture Analysis & Design Language).

ℂDuce: OCamlDuce: OCamlDuce: code samples and applications

Previous page: OCamlDuce: manual Next page: Funding