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