mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-06-06 07:53:43 +08:00
modify subst_child (may raise Not_found)
add subst_or_add_child
This commit is contained in:
+17
-3
@@ -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) ->
|
||||
|
||||
@@ -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. *)
|
||||
|
||||
Reference in New Issue
Block a user