1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
17 (** The different kind of XML nodes and utility functions *)
22 Document | Element | Text | Comment | Attribute
23 | ProcessingInstruction | Node
27 Document -> "document"
28 | Element -> "element"
29 | Attribute -> "attribute"
31 | Comment -> "comment"
32 | ProcessingInstruction -> "processing-instruction"
35 let print ppf k = Format.fprintf ppf "%s" (to_string k)
39 k1 == Node || k2 == Node || k1 == k2
45 (* Pack into an integer the result of the is_* and has_ predicates
57 let is_left (s : t) : bool =
60 let is_right (s : t) : bool =
63 let has_left (s : t) : bool =
66 let has_right (s : t) : bool =
69 let kind (s : t) : NodeKind.t =
72 let make is_left is_right has_left has_right kind =
73 (int_of_bool is_left) lor
74 ((int_of_bool is_right) lsl 1) lor
75 ((int_of_bool has_left) lsl 2) lor
76 ((int_of_bool has_right) lsl 3) lor
77 ((Obj.magic kind) lsl 4)
81 (** Signatures for trees *)
83 exception Parse_error of string
88 (** The type of a tree node *)
91 (** The type of trees *)
94 (** Return the number of nodes *)
97 (** Nil node, denoting the first/second child of a leaf or the parent of
101 (** Dummy node that is guaranteed to never occur in any tree *)
103 val load_xml_file : in_channel -> t
104 (** Takes a file descriptor and returns the XML data stored in the
105 corresponding file. Start at the current position in the file
106 descriptor (which is not necessarily the begining of file)
109 val load_xml_string : string -> t
110 (** Loads XML data stored in a string *)
112 val print_xml : out_channel -> t -> node -> unit
113 (** Outputs the tree as an XML document on the given output_channel *)
116 (** Returns the root of the tree *)
118 val first_child : t -> node -> node
119 (** [first_child t n] returns the first child of node [n] in tree [t].
120 Returns [nil] if [n] is a leaf. Returns [nil] if [n == nil].
123 val next_sibling : t -> node -> node
124 (** [next_sibling t n] returns the next_sibling of node [n] in tree [t].
125 Returns [nil] if [n] is the last child of a node.
126 Returns [nil] if [n == nil].
129 val parent : t -> node -> node
130 (** [next_sibling t n] returns the parent of node [n] in tree [t].
131 Returns [nil] if [n] is the root of the tree.
132 Returns [nil] if [n == nil].
135 val tag : t -> node -> QName.t
136 (** Returns the label of a given node *)
138 val data : t -> node -> string
139 (** Returns the character data associated with a node.
140 The only node having character data are those whose label is
141 QName.text, QName.cdata_section or QName.comment
144 val kind : t -> node -> NodeKind.t
145 (** Returns the kind of the given node *)
147 val summary : t -> node -> NodeSummary.t
148 (** Returns the summary of the given node *)
150 val preorder : t -> node -> int
151 (** [preorder t n] returns the pre-order position of [n] in [t].
152 [preodrder t (root t) == 0] and [preorder t nil < 0].
155 val by_preorder : t -> int -> node
156 (** [by_preorder t i] returns the node with preorder [i]
158 val print_node : Format.formatter -> node -> unit