Version française
Home     About     Download     Resources     Contact us    
Browse thread
xpath or alternatives
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] xpath or alternatives
On Wed, Sep 30, 2009 at 04:51:01PM +0200, Alain Frisch wrote:
> Richard Jones wrote:
> >  let devs = {{ map [xml] with
> >    | <domain..>[<devices..>[<disk..>[<source dev=(Latin1 & s) ..>_]]]
> >    | <domain..>[<devices..>[<disk..>[<source file=(Latin1 & s) ..>_]]] -> 
> >    [s]
> >    | _ -> [] }} in
> 
> The following should work:
> 
>   let l = {{ [xml] }} in
>   let l = {{ map l with <domain..>l -> l | _ -> [] }} in
>   let l = {{ map l with <devices..>l -> l | _ -> [] }} in
>   let l = {{ map l with <disk..>l -> l | _ -> [] }} in
>   let l = {{ map l with <source dev=(Latin1 & s) ..>_
>                       | <source file=(Latin1 & s) ..>_-> s
>                       | _ -> [] }} in
>   ...
> 
> let () =
>   let l = {{ [xml] }} in
>   let l = {{ (((l.(<domain..>_)) / .(<devices..>_)) / .(<disk..>_)) / }} in
>   let l = {{ map l with <source dev=(Latin1 & s) ..>_
>                       | <source file=(Latin1 & s) ..>_ -> s
>                       | _ -> [] }} in
>   ..

Thanks Alain.  My latest attempt was similar to your version 1 above,
and it works :-)

Now my code looks like your version 2:

  let xml = from_string xml in
  let xs = {{ [xml] }} in
  let xs = {{ (((xs.(<domain..>_)) / .(<devices..>_)) / .(<disk..>_)) / }} in
  let xs = {{ map xs with
	      | <source dev=(Latin1 & s) ..>_
              | <source file=(Latin1 & s) ..>_ -> [s]
              | _ -> [] }} in
  {: xs :}

(plus the boilerplate for interfacing xml-light and CDuce).

We're getting close to the xpath/perl solution (8 lines vs 3 lines),
with some added type safety and the possibility of validating the XML.

On the other hand, the code is hard to understand.  It's not clear to
me what the .(  ) syntax means, nor why there is an apparently trailing
/ character.

> This uses the constructions e/ and e.(t) as described in the manual.
> 
> That said, using OCamlDuce for this kind of XML data-extraction seems 
> just crazy to me.

I have some comments:

(A) "Subtyping failed" is a very common error, but is only mentioned
briefly in the manual.  I have no idea what these errors mean, so they
should have more explanation.  Here is a simple one which was caused
by me using a value instead of a list (but that is not at all obvious
from the error message):

  Error: Subtyping failed Latin1 <= [ Latin1* ]
  Sample:
  [ Latin1Char ]

(B) I think the interfacing code here:

  http://yquem.inria.fr/~frisch/ocamlcduce/samples/expat/
  http://yquem.inria.fr/~frisch/ocamlcduce/samples/pxp/
  http://yquem.inria.fr/~frisch/ocamlcduce/samples/xmllight/

should be distributed along with ocamlduce.

Rich.

-- 
Richard Jones
Red Hat