[Ocaml-pxp-users] [repost] Creating a simple document with namespaces
Gerd Stolpmann
info at gerd-stolpmann.de
Mon May 16 05:45:18 PDT 2005
Am Montag, den 16.05.2005, 12:44 +0100 schrieb Richard Jones:
> My last query wasn't perhaps focused enough to generate any responses.
> I'm finding the PXP API to be very hard to understand, even to do
> apparently simple things.
Well, it is validation-centric. That explains a lot of its complexity.
> I want to create and print out an XML document with namespaces. I
> don't need validation or DTDs at this point.
Keep in mind that PXP always needs a DTD object, even if validation is
disabled.
> Here is my test program:
>
> ----------------------------------------------------------------------
> (* Namespace test.
> * Compile with:
> * ocamlc -I +pcre -I +equeue -I +netstring -I +netclient -I +pxp-lex-iso88591 -I +pxp-engine str.cma unix.cma pcre.cma equeue.cma netstring.cma netclient.cma pxp_engine.cma pxp_lex_iso88591.cma pxp_lex_link_iso88591.cmo test.ml -o test
> *)
>
> let dtd =
> new Pxp_dtd.dtd Pxp_types.default_config.Pxp_types.warner
> Pxp_types.default_config.Pxp_types.encoding
>
> (* Helper functions for creating element and data nodes. *)
>
> let create_element, create_data =
> let dtd =
> let collect_warnings = Pxp_types.default_config.Pxp_types.warner in
> let rep_encoding = Pxp_types.default_config.Pxp_types.encoding in
> new Pxp_dtd.dtd collect_warnings rep_encoding in
>
> let exemplar = new Pxp_document.namespace_element_impl
> Pxp_tree_parser.default_extension in
>
> let create_element name =
> exemplar#create_element ~valcheck:false
> dtd (Pxp_document.T_element name) []
> in
> let create_data data =
> exemplar#create_data dtd data
This does not work, because [exemplar] is an element, not a data node.
You need something like
let data_exemplar = new data_impl Pxp_tree_parser.default_extension in
...
let create_data data =
data_exemplar # create_data dtd data
Furthermore, I recommend not to call the _methods_ create_xxx but the
_functions_ create_xxx_node. The functions have a much nicer interface.
You need a so-called specification to use them, but this is simply a
container for all your exemplars, e.g.
let spec = make_spec_from_alist
~data_exemplar:...
~default_element_exemplar:...
() in
...
create_element_node spec dtd name []
...
create_data_node spec dtd data
You can also use Pxp_tree_parser.default_spec for a reasonable default.
Btw, you should always use the same DTD object for all your nodes.
> in
> create_element, create_data
>
> let () =
> let env = create_element "Envelope" in
> let body = create_element "Body" in
> env#set_nodes [body];
> let call = create_element "hello" in
> body#set_nodes [call];
>
> let params =
> let id = create_element "id" in
> id#set_nodes [create_data "1"];
> let p1 = create_element "p1" in
> p1#set_nodes [create_data "2"];
> [ id; p1 ] in
>
> call#set_nodes params;
>
> let buf = Buffer.create 1024 in
> env#write (`Out_buffer buf) `Enc_utf8;
> let data = Buffer.contents buf in
>
> print_endline ("XML request = \n" ^ data);
> ----------------------------------------------------------------------
Another hint for people who want to create documents by programs: The
PXP preprocessor simplifies this a lot. For example, this program can be
shortened to:
let spec = Pxp_tree_parser.default_spec;;
let dtd = Pxp_dtd.create_dtd `Enc_iso88591;;
let env =
<:pxp_tree<
<Envelope>
[ <body>
[ <hello>
[ <id>[ "1" ] <p1>[ "2" ] ]
]
]
>>
The preprocessor supports the dynamic creation of nodes, too (with
non-constant names and contents). See the description in
doc/PREPROCESSOR in the PXP tarball.
Gerd
--
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany
gerd at gerd-stolpmann.de http://www.gerd-stolpmann.de
------------------------------------------------------------
More information about the Ocaml-pxp-users
mailing list