[Libguestfs] [PATCH v5 09/10] mllib: add XPath helper xpath_get_nodes()

Pino Toscano ptoscano at redhat.com
Tue Apr 4 14:23:29 UTC 2017


On Thursday, 23 March 2017 10:02:47 CEST Cédric Bosdonnat wrote:
> This function will allow more OCAML-ish processing of xpath queries
> with multiple results.

s/OCAML/OCaml/
s/xpath/XPath/

> ---
>  mllib/xpath_helpers.ml  | 9 +++++++++
>  mllib/xpath_helpers.mli | 4 ++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/mllib/xpath_helpers.ml b/mllib/xpath_helpers.ml
> index 8648596a4..f12156f45 100644
> --- a/mllib/xpath_helpers.ml
> +++ b/mllib/xpath_helpers.ml
> @@ -53,3 +53,12 @@ let xpath_eval_default parsefn xpath expr default =
>  let xpath_string_default = xpath_eval_default identity
>  let xpath_int_default = xpath_eval_default int_of_string
>  let xpath_int64_default = xpath_eval_default Int64.of_string
> +
> +let xpath_get_nodes xpathctx expr =
> +  let obj = Xml.xpath_eval_expression xpathctx expr in
> +  let nodes = ref [] in
> +  for i = 0 to Xml.xpathobj_nr_nodes obj - 1 do
> +    let node = Xml.xpathobj_node obj i in
> +    nodes := List.append !nodes [node]

You can use the utilities from Common_utils for manipulating list
references, so:

  push_back nodes node;

The other option to not use a list reference to collect the nodes would
be to use a tail-recursive function to iterate the xpathobj nodes, but
IMHO would be more complicated than actually needed.

> +  done;
> +  !nodes
> diff --git a/mllib/xpath_helpers.mli b/mllib/xpath_helpers.mli
> index 7434ba645..ab176351f 100644
> --- a/mllib/xpath_helpers.mli
> +++ b/mllib/xpath_helpers.mli
> @@ -31,3 +31,7 @@ val xpath_int_default : Xml.xpathctx -> string -> int -> int
>  val xpath_int64_default : Xml.xpathctx -> string -> int64 -> int64
>  (** Parse an xpath expression and return a string/int; if the expression
>      doesn't match, return the default. *)
> +
> +val xpath_get_nodes : Xml.xpathctx -> string -> Xml.node list
> +(** Parse an xpath expression and return a list with the matching
> +    XML nodes. *)

s/xpath/XPath/

LGTM with the changes above.

Thanks,
-- 
Pino Toscano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20170404/b85b6573/attachment.sig>


More information about the Libguestfs mailing list