c968f105d99ca77f2c1f6611babd2ace0786d186
[SXSI/xpathcomp.git] / tree.ml
1 (******************************************************************************)
2 (*  SXSI : XPath evaluator                                                    *)
3 (*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
4 (*  Copyright NICTA 2008                                                      *)
5 (*  Distributed under the terms of the LGPL (see LICENCE)                     *)
6 (******************************************************************************)
7 (*INCLUDE "debug.ml" *)
8
9 type tree
10 type 'a node = int
11 type node_kind = [`Text | `Tree ]
12     
13 let compare_node : 'a node -> 'a node -> int = (-)
14 let equal_node : 'a node -> 'a node -> bool = (==)
15   
16 (* abstract type, values are pointers to a XMLTree C++ object *)
17
18 external int_of_node : 'a node -> int = "%identity"
19   
20 external parse_xml_uri : string -> int -> bool -> bool -> tree = "caml_call_shredder_uri"         
21 external parse_xml_string :  string -> int -> bool -> bool -> tree = "caml_call_shredder_string"
22   
23 external save_tree : tree -> string -> unit = "caml_xml_tree_save"
24 external load_tree : string -> int -> tree = "caml_xml_tree_load"
25   
26 external nullt : unit -> 'a node = "caml_xml_tree_nullt"
27
28 let nil : 'a node = Obj.magic (-1)
29
30 external text_get_text : tree -> [`Text] node -> string = "caml_text_collection_get_text" 
31                 
32 external text_is_empty : tree -> [`Text ] node -> bool = "caml_text_collection_empty_text" 
33
34 let text_is_empty t n =
35   (equal_node nil n) || text_is_empty t n
36     
37 external get_cached_text : tree -> [`Text ] node -> string = "caml_text_collection_get_cached_text" 
38   
39   
40 let text_get_text t n =
41   if equal_node nil n then ""
42   else get_cached_text t n
43
44 external text_size : tree -> int = "caml_text_collection_size" 
45 external text_is_contains : tree -> string -> bool = "caml_text_collection_is_contains" 
46 external text_count_contains : tree -> string -> int = "caml_text_collection_count_contains" 
47 external text_count : tree -> string -> int = "caml_text_collection_count" 
48 external text_contains : tree -> string -> [`Text ] node array = "caml_text_collection_contains" 
49
50
51 external tree_serialize : tree -> string -> unit = "caml_xml_tree_serialize" 
52
53 external tree_unserialize : string -> tree = "caml_xml_tree_unserialize"
54       
55 external tree_root : tree -> [`Tree] node = "caml_xml_tree_root" 
56
57 let tree_is_nil x = equal_node x nil
58
59 external tree_parent : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent" 
60 external tree_parent_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc" 
61 external tree_prev_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc" 
62 external tree_first_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child" 
63 external tree_next_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling" 
64 external tree_prev_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling" 
65 external tree_is_leaf : tree -> [`Tree] node -> bool = "caml_xml_tree_is_leaf" 
66     
67 (*    external tag : tree -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
68 external tree_tag_id : tree -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id" 
69
70 let tree_is_last t n = equal_node nil (tree_next_sibling t n)
71     
72 external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text" 
73
74 external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text" 
75 external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text" 
76 external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids" 
77 external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id" 
78 external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id" 
79 external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor" 
80 external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc" 
81 external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below" 
82 external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags" 
83 external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below" 
84 external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only" 
85 external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next" 
86 external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" 
87 external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" 
88   
89 type descr = 
90   | Nil 
91   | Node of [`Tree] node
92   | Text of [`Text] node * [`Tree] node
93       
94 type t = { doc : tree;            
95            node : descr;
96            ttable : (Tag.t,(Ptset.t*Ptset.t)) Hashtbl.t;           
97          }
98         
99
100 let update h t sb sa = 
101     let sbelow,safter = 
102       try
103         Hashtbl.find h t 
104       with
105         | Not_found -> Ptset.empty,Ptset.empty
106     in
107       Hashtbl.replace h t (Ptset.union sbelow sb, Ptset.union safter sa)
108
109
110
111
112
113 let collect_tags tree =
114   let h = Hashtbl.create 511 in
115   let rec loop id acc = 
116     if equal_node id nil
117     then (Ptset.singleton Tag.pcdata, Ptset.add Tag.pcdata acc)
118     else
119       let below2,after2 = loop (tree_next_sibling tree id) acc in
120       let below1,after1 = loop (tree_first_child tree id) after2 in
121       let tag = tree_tag_id tree id in
122         update h tag below1 after2;
123         Ptset.add tag (Ptset.union below1 below2), (Ptset.add tag after1)
124   in
125   let b,a = loop (tree_root tree) Ptset.empty in
126     update h Tag.pcdata b a;
127     h
128
129
130
131 let contains_array = ref [| |]
132   
133 let init_contains t s = 
134   let a = text_contains t.doc s 
135   in
136     Array.fast_sort (compare) a;
137     contains_array := a
138       
139 let init_naive_contains t s =
140   let i,j = tree_doc_ids t.doc (tree_root t.doc)
141   in
142   let regexp = Str.regexp_string s in
143   let matching arg = 
144     try
145       let _ = Str.search_forward regexp arg 0;
146       in true
147     with _ -> false
148   in
149   let rec loop n acc l = 
150     if n >= j then acc,l
151     else
152       let s = text_get_text t.doc n
153       in
154         if matching s 
155         then loop (n+1) (n::acc) (l+1) 
156         else loop (n+1) acc l
157   in
158   let acc,l = loop i [] 0 in
159   let a = Array.create l nil in
160   let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
161   in
162     contains_array := a
163           
164
165
166 module DocIdSet = struct
167   include Set.Make (struct type t = [`Text] node
168                            let compare = compare_node end)
169     
170 end
171 let is_nil t = match t.node with
172   | Nil -> true
173   | Node(i) -> equal_node i nil
174   | _ -> false
175
176 let is_node t =
177 match t.node with
178   | Node(i) -> not(equal_node i nil)
179   | _ -> false
180
181
182 let node_of_t t =
183   let _ = Tag.init (Obj.magic t) in
184   let table = collect_tags t 
185   in
186 (*
187   let _ = Hashtbl.iter (fun t (sb,sa) ->
188                           Printf.eprintf "'%s' -> { " (Tag.to_string t);
189                           Ptset.iter (fun i ->  Printf.eprintf "'%s' " (Tag.to_string i)) sb;
190                           Printf.eprintf "}\n { ";
191                           Ptset.iter (fun i ->  Printf.eprintf "'%s' " (Tag.to_string i)) sa;
192                           Printf.eprintf "} \n----------------------------------\n";
193                        ) table in
194 *)
195     { doc= t; 
196       node = Node(tree_root t);
197       ttable = table;
198     }
199
200
201 let parse_xml_uri str = node_of_t       
202   (parse_xml_uri str 
203      !Options.sample_factor 
204      !Options.index_empty_texts
205      !Options.disable_text_collection)
206      
207 let parse_xml_string str = node_of_t 
208   (parse_xml_string str
209      !Options.sample_factor 
210      !Options.index_empty_texts 
211      !Options.disable_text_collection)
212      
213 external pool : tree -> Tag.pool = "%identity"
214 let save t str = save_tree t.doc str
215 let load ?(sample=64) str = 
216   node_of_t (load_tree str sample)
217     
218
219
220
221 let tag_pool t = pool t.doc
222   
223 let compare a b = match a.node,b.node  with
224   | Nil, Nil -> 0
225   | Nil,_ -> 1
226   | _ , Nil -> -1
227   | Node(i),Node(j) -> compare_node i j
228   | Text(i,_), Text(j,_) -> compare_node i j
229   | Node(i), Text(_,j) -> compare_node i j
230   | Text(_,i), Node(j) -> compare_node i j 
231
232 let equal a b = (compare a b) == 0
233   
234   
235 let norm (n : [`Tree ] node ) =  if tree_is_nil n then Nil else Node (n)
236   
237 let nts = function
238     Nil -> "Nil"
239   | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
240   | Node (i) -> Printf.sprintf "Node (%i)"  i
241       
242 let mk_nil t = { t with node = Nil }              
243 let root n = { n with node = norm (tree_root n.doc) }
244
245 let is_root n = match n.node with
246   | Node(t) -> (int_of_node t) == 0 
247   | _ -> false
248       
249 let parent n =  
250   let node' = 
251     match n.node with (* inlined parent *)
252       | Node(t) when (int_of_node t)== 0 -> Nil
253       | Node(t) -> 
254           let txt = tree_prev_text n.doc t in
255             if text_is_empty n.doc txt then
256               let ps = tree_prev_sibling n.doc t in
257                 if tree_is_nil ps
258                 then
259                   Node(tree_parent n.doc t)
260                 else Node(ps)
261             else
262               Text(txt,t)
263       | Text(i,t) ->
264           let ps = tree_prev_doc n.doc i in
265             if tree_is_nil ps
266             then Node (tree_parent_doc n.doc i)
267             else Node(ps)
268       | _ -> failwith "parent"
269   in
270     { n with node = node' }
271
272 let node_child n =
273   match n.node with
274     | Node i ->  { n with node= norm(tree_first_child n.doc i) }
275     | _ -> { n with node = Nil }
276
277 let node_sibling n =
278   match n.node with
279     | Node i ->  { n with node= norm(tree_next_sibling n.doc i) }
280     | _ -> { n with node = Nil }
281
282 let node_sibling_ctx n _ = 
283   match n.node with
284     | Node i ->  { n with node= norm(tree_next_sibling n.doc i) }
285     | _ -> { n with node = Nil }
286
287
288 let first_child n = 
289   let node' = 
290     match n.node with
291       | Node (t) -> 
292           let fs = tree_first_child n.doc t in
293             if equal_node nil fs
294             then 
295               let txt = tree_my_text n.doc t in
296                 if equal_node nil txt
297                 then Nil
298                 else Text(txt,nil)
299             else
300               let txt = tree_prev_text n.doc fs in
301                 if equal_node nil txt
302                 then Node(fs)
303                 else Text(txt, fs) 
304       | Text(_,_) -> Nil
305       | Nil -> failwith "first_child"
306   in
307     { n with node = node'}
308       
309 let next_sibling n = 
310   let node' =
311     match n.node with
312       | Text (_,ns) -> norm ns
313       | Node(t) ->
314           let ns = tree_next_sibling n.doc t in
315           let txt = tree_next_text n.doc t in
316             if equal_node nil txt
317             then norm ns
318             else Text(txt, ns)
319       | Nil -> failwith "next_sibling"
320   in
321     { n with node = node'}
322           
323 let next_sibling_ctx n _ = next_sibling n
324           
325 let left = first_child 
326 let right = next_sibling
327     
328 let id t = 
329   match t.node with
330     | Node(n)  -> tree_node_xml_id t.doc n
331     | Text(i,_)  -> tree_text_xml_id t.doc i
332     | _ ->  -1 
333         
334 let tag t =
335   match t.node with 
336   | Text(_) -> Tag.pcdata
337   | Node(n) -> tree_tag_id t.doc n
338   | _ -> failwith "tag"
339     
340 (*
341     let string_below t id =
342       let strid = parent_doc t.doc id in
343         match t.node with
344           | Node(NC(i)) -> 
345               (Tree.equal i strid) || (is_ancestor t.doc i strid)
346           | Node(SC(i,_)) -> Text.equal i id
347           | _ -> false
348
349
350     let tagged_foll t tag =
351       if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
352       else match t with
353         | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
354         | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil } 
355         | { doc=d; node=Node(SC (_,n)) } ->
356             let nnode = 
357               if tag_id d n == tag then n
358               else 
359                 let n' = tagged_desc d n tag in
360                   if is_nil n' then tagged_foll d n tag
361                   else n'
362             in {t with node= norm nnode}
363         | _ -> { t with node=Nil }
364             
365
366     let tagged_desc t tag =
367       if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
368       else match t with
369         | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
370         | _ -> { t with node=Nil }
371
372 *)          
373 let select_next tb tf t s = 
374   match s.node  with
375     | Node (below) -> begin
376         match t.node with
377           | Node( n)  ->
378               { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
379           | Text (i,n)  when equal_node nil n ->
380               let p = tree_parent_doc t.doc i in
381                 { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
382           | Text(_,n)  ->
383               if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
384               then { t with node=Node(n) }
385               else
386                 let vb = Ptset.to_int_vector tb in
387                 let vf = Ptset.to_int_vector tf in
388                 let node = 
389                   let dsc = tree_select_below t.doc n vb vf in
390                     if equal_node nil dsc
391                     then tree_select_next t.doc n vb vf below
392                     else dsc
393                 in
394                   { t with node = norm node }
395           | _ -> {t with node = Nil }
396       end
397         
398     | _ -> { t with node = Nil }
399
400   
401
402
403   let select_foll_only  tf t s = 
404     match s.node  with
405       | Node (below)  -> 
406           begin
407             match t.node with
408             | Node(n) ->
409                 { t with node= norm (tree_select_foll_only t.doc n (Ptset.to_int_vector tf) below) }
410             | Text(i,n)  when equal_node nil n ->
411                 let p = tree_parent_doc t.doc i in
412                   { t with node= norm (tree_select_foll_only t.doc p (Ptset.to_int_vector tf) below) }
413             |  Text(_,n) ->
414                  if Ptset.mem (tree_tag_id t.doc n) tf
415                  then { t with node=Node(n) }
416                  else
417                    let vf = Ptset.to_int_vector tf in
418                    let node = 
419                      let dsc = tree_select_desc_only t.doc n vf in
420                        if tree_is_nil dsc
421                        then tree_select_foll_only t.doc n vf below
422                        else dsc
423                    in
424                      { t with node = norm node }
425             | _ -> { t with node = Nil }
426         end         
427       | _ -> {t with node=Nil }   
428
429 let select_below  tc td t=
430   match t.node with
431     | Node( n) -> 
432         let vc = Ptset.to_int_vector tc
433         in
434         let vd = Ptset.to_int_vector td
435         in
436           { t with node= norm(tree_select_below t.doc n vc vd) }
437     | _ -> { t with node=Nil }
438         
439         
440 let select_desc_only  td t =
441   match t.node with
442     | Node(n) -> 
443         let vd = Ptset.to_int_vector td
444         in
445           { t with node = norm(tree_select_desc_only t.doc n vd) }
446     | _ -> { t with node = Nil }
447
448
449 let tagged_desc tag t =
450   match t.node with
451     | Node(n) ->        
452           { t with node = norm(tree_tagged_desc t.doc n tag) }
453     | _ -> { t with node = Nil }
454
455
456 let tagged_foll_below tag t s =
457     match s.node  with
458       | Node (below)  -> 
459           begin
460             match t.node with
461             | Node(n) ->
462                 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
463             | Text(i,n)  when equal_node nil n ->
464                 let p = tree_prev_doc t.doc i in
465                   { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
466             |  Text(_,n) ->
467                  if (tree_tag_id t.doc n) == tag
468                  then { t with node=Node(n) }
469                  else
470                    let node = 
471                      let dsc = tree_tagged_desc t.doc n tag in
472                        if tree_is_nil dsc
473                        then tree_tagged_foll_below t.doc n tag below
474                        else dsc
475                    in
476                      { t with node = norm node }
477             | _ -> { t with node = Nil }
478           end       
479       | _ -> {t with node=Nil }   
480
481
482 let last_idx = ref 0
483 let array_find a i j =
484   let l = Array.length a in
485   let rec loop idx x y =
486     if x > y || idx >= l then nil
487         else
488           if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
489           else loop (idx+1) x y
490   in
491     if a.(0) > j || a.(l-1) < i then nil
492     else loop !last_idx i j 
493
494
495         
496 let text_below t = 
497   let l = Array.length !contains_array in
498     if l = 0 then { t with node=Nil }
499     else
500       match t.node with
501         | Node(n)  ->
502             let i,j = tree_doc_ids t.doc n in
503             let id = array_find !contains_array i j
504             in
505               if id == nil then  
506                 { t with  node=Nil }
507               else
508                 { t with  node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
509         | _ -> { t with node = Nil }
510             
511 let text_next t root =
512   let l = Array.length !contains_array in
513     if l = 0 then { t with node=Nil }
514     else
515       let inf = match t.node with
516         |  Node(n)  -> snd(tree_doc_ids t.doc n)+1
517         | Text(i,_)  -> i+1
518         | _ -> assert false
519       in
520         match root.node with
521           | Node (n)  ->
522               let _,j = tree_doc_ids t.doc n in             
523               let id = array_find !contains_array inf j
524               in
525                 if id == nil then  { t with node= Nil }
526                 else
527                   { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
528           | _ -> { t with node = Nil}
529                   
530
531 (*                
532     let subtree_tags t tag =
533       match t with 
534           { doc = d; node = Node(NC n) } -> 
535             subtree_tags d n tag
536         | _ -> 0
537
538     let select_desc_array = ref [| |]
539     let idx = ref 0
540
541     let init_tagged_next t tagid =
542       let l = subtree_tags (root t) tagid
543       in
544         tagged_desc_array := Array.create l { t with node= Nil };
545         let i = ref 0 in
546           let rec collect t =
547             if is_node t then begin
548               if tag t == tagid then
549                 begin
550                   !tagged_desc_array.(!i) <- t;
551                   incr i;
552                 end;
553               collect (first_child t);
554               collect (next_sibling t)
555             end;
556           in
557             collect t;
558             idx := 0
559
560     let print_id ppf v = 
561       let pr x= Format.fprintf ppf x in
562         match v with
563             { node=Nil } -> pr "NULLT: -1"
564           | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
565           | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
566               
567               
568           
569 (*    let tagged_next t tag = 
570       if !idx >= Array.length !tagged_desc_array 
571       then {t with node=Nil}
572       else
573         let r = !tagged_desc_array.(!idx) 
574         in
575           incr idx; r
576 *)                
577
578
579     let has_tagged_foll t tag = is_node (tagged_foll t tag)
580     let has_tagged_desc t tag = is_node (tagged_desc t tag)
581
582     let contains t s = 
583       Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
584
585
586     let contains_old t s = 
587       let regexp = Str.regexp_string s in
588       let matching arg = 
589         try
590           let _ = Str.search_forward regexp arg 0;
591           in true
592         with _ -> false
593       in
594       let rec find t acc = match t.node with
595         | Nil -> acc
596         | String i ->
597             if  matching (string t) then DocIdSet.add i acc else acc
598         | Node(_) ->  (find (left t )) ((find (right t))  acc)
599       in
600         find t DocIdSet.empty
601
602
603     let contains_iter t s = 
604       let regexp = Str.regexp_string s in
605       let matching arg = 
606         try
607           let _ = Str.search_forward regexp arg 0;
608           in true
609         with _ -> false
610       in
611       let size = Text.size t.doc in
612       let rec find acc n = 
613         if n == size then acc
614         else
615           find 
616             (if matching (Text.get_cached_text t.doc (Obj.magic n)) then 
617              DocIdSet.add (Obj.magic n) acc
618            else acc) (n+1)
619       in
620         find DocIdSet.empty 0
621
622
623
624
625     let count_contains t s =   Text.count_contains t.doc s
626 *)
627
628   let count t s = text_count t.doc s
629 (*
630     let is_left t =
631       if is_root t then false
632       else
633       if tag (parent t) == Tag.pcdata then false
634       else
635         let u = left (parent t) in
636           (id t) == (id u)
637 *)
638   let print_xml_fast outc t =
639     let rec loop ?(print_right=true) t = 
640       match t.node with 
641       | Nil -> ()    
642       | Text(i,n) -> output_string outc (text_get_text t.doc i);
643           if print_right
644           then loop (right t)
645       | Node (n) -> 
646           let tg = Tag.to_string (tag t) in
647           let l = left t 
648           and r = right t 
649           in
650             output_char outc  '<';
651             output_string outc  tg;
652             ( match l.node with
653                   Nil -> output_string outc  "/>"
654                 | Node(_) when Tag.equal (tag l) Tag.attribute -> 
655                     (loop_attributes (left l);
656                      match (right l).node with
657                        | Nil -> output_string outc  "/>"
658                        | _ -> 
659                            output_char outc  '>'; 
660                            loop (right l);
661                            output_string outc  "</";
662                            output_string outc  tg;
663                            output_char outc '>' )
664                 | _ ->
665                     output_char outc  '>'; 
666                     loop l;
667                     output_string outc "</";
668                     output_string outc tg;
669                     output_char outc '>'
670             );if print_right then loop r
671     and loop_attributes a =      
672         match a.node with 
673           | Node(_) ->
674               let value =
675                 match (left a).node with
676                   | Text(i,_) -> text_get_text a.doc i
677                   | _ -> assert false
678               in
679                 output_char outc ' ';
680                 output_string outc (Tag.to_string (tag a));
681                 output_string outc "=\"";
682                 output_string outc value;
683                 output_char outc '"';
684                 loop_attributes (right a)
685           | _ -> ()
686     in
687         loop ~print_right:false t
688           
689           
690     let print_xml_fast outc t = 
691       if Tag.to_string (tag t) = "" then
692         print_xml_fast outc (first_child t)
693       else print_xml_fast outc t
694         
695
696 let tags_below t tag = 
697   fst(Hashtbl.find t.ttable tag)
698
699 let tags_after t tag = 
700   snd(Hashtbl.find t.ttable tag)
701
702 let tags t tag = Hashtbl.find t.ttable tag