diff --git a/sw/lib/ocaml/extXml.ml b/sw/lib/ocaml/extXml.ml index 1658d43c1d..246d72a160 100644 --- a/sw/lib/ocaml/extXml.ml +++ b/sw/lib/ocaml/extXml.ml @@ -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) -> diff --git a/sw/lib/ocaml/extXml.mli b/sw/lib/ocaml/extXml.mli index a6eb90443e..51baa6150d 100644 --- a/sw/lib/ocaml/extXml.mli +++ b/sw/lib/ocaml/extXml.mli @@ -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. *)