DougWebb has asked for the wisdom of the Perl Monks concerning the following question:

I've been taking an xQuery class this week, and it occurs to me that I'd really rather not learn a new programming language. It seems like most of what xQuery does could also be done by adding an 'xmlnode' datatype to perl (along with the relevant subtypes) and also adding XPath as a native expression type (like regex.)

With these two additions, you could most of what xQuery lets you do, except you'd need a persistence library / database to perform operations on XML that's not currently in memory. A DBI-like set of modules could let you plug that in.

Does anything like this already exist, in either Perl 5 or Perl 6?

Doug.

Replies are listed 'Best First'.
Re: xQuery functionality in Perl?
by jZed (Prior) on May 19, 2005 at 15:39 UTC
    My DBD::AnyData module uses the excellent XML::Twig to allow DBI support for some XML (either files or strings). In other words, it supports using SQL to query XML. Ok, that's not xQuery, however there's no reason that a DBD couldn't be made to handle xQuery rather than SQL (in fact, once you had the xQuery engine, it would be very easy). I don't know if this is mixing fish and bicycles, but it would save having to reinvent an interface and could piggyback on the many DBI plugin and extension modules.

      That's not really what I'm looking for, but it's pretty neat.

      What I'm looking for is something like this:

      my $xml_fragment = <records> <record id="001">foo</record> <record id="002">bar</record> </records>; my @nodeset = $xml_fragment =~ qx!//records[@id = '001']!; die "ids should be unique!\n" if @nodeset > 1;

      Note that no quoting is needed around the xml fragment; the angle-brackets should be sufficient to identify it as an xmlnode (including the whitespace). Also note that I'm inventing a qx quote operator to wrap an xpath expression, and overloading =~ to allow me to execute the xpath expression on my $xml_fragment scalar, which contains an xmlnode data element. The return value of the qx operator is a nodeset, which is just an array of xmlnode scalars.

      Doug.

        I guess I don't see the advantage of having something like this built into the language. It's both easy and reasonable to implement it in an extension (i.e. a "library" or "module").

        (PS: qx is already in use.)

        Also, angle brackets are already used as well, so that would not suffice as identifying an xmlnode.

        It is curious that you'd want these elements built in to perl, since (with only minor modification) your example code could be made to work with existing tools anyway.

        =oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV