modify subst_child (may raise Not_found)

add subst_or_add_child
This commit is contained in:
Pascal Brisset
2009-08-30 19:39:14 +00:00
parent b416bf3fc0
commit a9d144d583
2 changed files with 22 additions and 5 deletions
+17 -3
View File
@@ -178,12 +178,26 @@ let subst_attrib = fun attrib value xml ->
let subst_child = fun ?(select= fun _ -> true) t x xml ->
match xml with
Xml.Element (tag, attrs, children) ->
Xml.Element (tag,
attrs,
List.map (fun xml -> if tag_is xml t && select xml then x else xml) children)
let found = ref false in
let new_children =
List.map
(fun xml -> if tag_is xml t && select xml then (found := true; x) else xml)
children in
if !found then
Xml.Element (tag, attrs, new_children)
else
raise Not_found
| Xml.PCData _ -> xml
let subst_or_add_child = fun t x xml ->
try subst_child t x xml with Not_found ->
match xml with
Xml.Element (tag, attrs, children) ->
Xml.Element (tag, attrs, x::children)
| Xml.PCData _ -> xml
let remove_child = fun ?(select= fun _ -> true) t xml ->
match xml with
Xml.Element (tag, attrs, children) ->
+5 -2
View File
@@ -58,11 +58,14 @@ val subst_attrib : string -> string -> Xml.xml -> Xml.xml
val subst_child :
?select:(Xml.xml -> bool) -> string -> Xml.xml -> Xml.xml -> Xml.xml
(** [subst_child ?select child_tag new_child xml] *)
(** [subst_child ?select child_tag new_child xml] May raise Not_found *)
val subst_or_add_child : string -> Xml.xml -> Xml.xml -> Xml.xml
(** [subst_or_add_child child_tag new_child xml] *)
val remove_child :
?select:(Xml.xml -> bool) -> string -> Xml.xml -> Xml.xml
(** [delete_child ?select child_tag xml] *)
(** [delete_child ?select child_tag xml] Returns [xml] if not found *)
val parse_file : ?noprovedtd:bool -> string -> Xml.xml
(** Identical to Xml.parse_file with Failure exceptions. [nodtdprove] default is false. *)