Since , the ocaml-printf rule can be used to access the content of computed OCaml values, as strings.

Let's take a first example. Suppose we evaluated some OCaml code with the following rule:

let x = "Hello world!";; ]]>

Now we can get the contents of x as a string to include in our document using the new rule:

x is: x ]]>

The ocaml-printf rule builds some OCaml code of the form:

Printf.sprintf "<format>" <code>

The <code> is the contents of the ocaml-printf node, and <format> is %s by default. Another format can be used by setting the format attribute like in the following example:

(1+2) ]]>

Note that the parentheses around the expression in the ocaml-printf node is mandatory, otherwise the Printf.sprintf "%d" 1+2 would result in an error.

Of course more compilated format and ocaml expressions can be used:

let y = 1 and z = 21.;;
y z ((float y) +. z) ]]>

The result of ocaml-printf is considered as CDATA in the XML document:

is rewritten

Let's define a list:

let my_list = [ 101 ; 202 ; 303 ; 404 ];;

The as-xml rule can be used to parse the result of ocaml-printf as XML, using the defer_ attribute to compute the contents of the node first:

This gives the following table:

As shown above, the parsed XML is then rewritten during the rest of the evaluation process. The current value of foo is foo.

The session attribute is honored:

...]]>

See this post for details about OCaml sessions.