1
This example demonstrates namespaces: it's a tiny macro preprocessor.
2
You can put <macro:define> statements into your XML code, and later
3
expand macros by <macro:use> statements. See macro.dtd for the DTD
4
and sample.xml for an example in XHTML.
6
Using the preprocessor: Do
8
./preprocess sample.xml
10
which prints the converted text to stdout.
12
Note that the preprocessor runs in a mode which is more than
13
well-formedness mode and less than validating mode. The DTD
14
contained in the preprocessed XML text is replaced by macro.dtd
15
(except the general entities which are copied), and macro.dtd
16
specifies that the known parts of the macro language are validated,
17
and that all other elements and attributes are not validated.
18
For example, the preprocessor does not check whether sample.xml
21
The replacement of the DTD is performed by the ~transform_dtd
22
parameter of parse_document_entity (see preprocess.ml).
24
Notes to the namespaces:
26
(1) The program uses two namespaces for its own purposes:
27
"http://www.ocaml-programming.de/macro" is the identifier for the
28
namespace containing the <macro:define> and <macro:use> statements.
30
"http://www.ocaml-programming.de/macro/use" is the identifier for
31
the namespace containing the defined macros.
33
(2) In preprocess.ml you will find something like
35
if node # node_type = T_element "macro:define" then ...
37
This works even if the namespace "http://www.ocaml-programming.de/macro"
38
is bound to a different prefix than "macro", because the parser
39
rewrites the prefixes automatically. The DTD macro.dtd contains a
40
processing instruction
42
<?pxp:dtd namespace prefix="macro"
43
uri="http://www.ocaml-programming.de/macro"?>
45
letting the parser replace all prefixes declared for the
46
namespace identifier "http://www.ocaml-programming.de/macro"
49
The namespaces chapter of the manual explains the rationale
52
(3) You find another test
54
node # namespace_uri = macro_use_uri
56
where macro_use_uri is "http://www.ocaml-programming.de/macro/use".
57
This is just another way to check whether an element belongs
58
to a certain namespace.
62
(1) The preprocessor outputs the file always in UTF-8 encoding.
64
(2) If the input file has a DOCTYPE line, this line is reproduced
65
in the output. However, the preprocessor does never output
66
the internal subset of the DTD, if any.
68
(3) Although the preprocessor does not validate the input fully,
69
the specified DTD is completely read in. This is necessary to
70
get all the general entities.