[Libguestfs] [PATCH 1/5] v2v: Add convenience functions for parsing xpath expressions.

Richard W.M. Jones rjones at redhat.com
Fri Aug 28 13:19:06 UTC 2015


---
 v2v/Makefile.am |  2 +-
 v2v/utils.ml    | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 93d225b..fc893bd 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -69,8 +69,8 @@ SOURCES_MLI = \
 SOURCES_ML = \
 	stringMap.ml \
 	types.ml \
-	utils.ml \
 	xml.ml \
+	utils.ml \
 	domainxml.ml \
 	DOM.ml \
 	kvmuid.ml \
diff --git a/v2v/utils.ml b/v2v/utils.ml
index 4e6befc..e07f7a9 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -58,6 +58,40 @@ let uri_quote str =
   done;
   String.concat "" (List.rev !xs)
 
+(* Parse an xpath expression and return a string/int.  Returns
+ * Some v or None if the expression doesn't match.
+ *)
+let xpath_string xpathctx expr =
+  let obj = Xml.xpath_eval_expression xpathctx expr in
+  if Xml.xpathobj_nr_nodes obj < 1 then None
+  else (
+    let node = Xml.xpathobj_node obj 0 in
+    Some (Xml.node_as_string node)
+  )
+let xpath_int xpathctx expr =
+  let obj = Xml.xpath_eval_expression xpathctx expr in
+  if Xml.xpathobj_nr_nodes obj < 1 then None
+  else (
+    let node = Xml.xpathobj_node obj 0 in
+    let str = Xml.node_as_string node in
+    try Some (int_of_string str)
+    with Failure "int_of_string" ->
+      error (f_"expecting XML expression to return an integer (expression: %s, matching string: %s)")
+            expr str
+  )
+
+(* Parse an xpath expression and return a string/int; if the expression
+ * doesn't match, return the default.
+ *)
+let xpath_string_default xpathctx expr default =
+  match xpath_string xpathctx expr with
+  | None -> default
+  | Some s -> s
+let xpath_int_default xpathctx expr default =
+  match xpath_int xpathctx expr with
+  | None -> default
+  | Some i -> i
+
 external drive_name : int -> string = "v2v_utils_drive_name"
 external drive_index : string -> int = "v2v_utils_drive_index"
 
-- 
2.5.0




More information about the Libguestfs mailing list