Thread: xpath_list() question for contrib/xml2

xpath_list() question for contrib/xml2

From
Ron Mayer
Date:
Short summary:

   I want something like xpath_list() that returns an array
   (instead of a delimited, concatenated string) when multiple
   nodes exist in an XML file.  It feels to me like that'd
   be a better (cleaner) API.

   Before I write one, does anyone already have such a
   patch?  If not, would people be interested if I added
   xpath_array() that behaves like xpath_list() but returns
   an array instead of one big string?

   Or... is xpsql on gborg or some other postgresql-xml
   project a better place for me to be looking?


Longer:

 From the (very cool) xml2 package's README, I see
   xpath_list(document,query,separator) RETURNS text
    This function returns multiple values separated by the specified
    separator, e.g. Value 1,Value 2,Value 3 if separator=','.

   xpath_list(document,query) RETURNS text
    This is a wrapper for the above function that
    uses ',' as the separator.

And indeed, that's exactly what it returns:

   fl=# create temporary table x (xml text);

   flee=# insert into x values
          ('<a><b id="1">1</b><b id="2">2</b></a>');
   flee=# select xpath_list(xml,'/a/b') from x;
    xpath_list
   ------------
    1,2
   (1 row)

However to make use of each value, I'd then need to re-parse
that text string to make use of each of the values.

I realize I can get the effect I want using an uncommon
string as a delimiter and the string_to_array() function...
  fl=# select
     string_to_array(xpath_list(xml,'/a/b','##delim##')
                                          ,'##delim##') from x;
     string_to_array
     -----------------
     {1,2}
     (1 row)
... but that feels pretty ugly considering to guess what
delimiter won't be in the string.



While I'm at it, a version of xpath_nodeset:
   fl=# select xpath_nodeset(xml,'/a/b') from x;
          xpath_nodeset
   --------------------------------
    <b id="1">1</b><b id="2">2</b>
   (1 row)
that returned an array of matching XML fragments
instead of all the fragments concatenated together
might also be useful.


Thoughts?  Suggestions?

   Ron


Re: xpath_list() question for contrib/xml2

From
John Gray
Date:
On Mon, 24 Jan 2005 16:53:47 -0800, Ron Mayer wrote:

> Short summary:
>
>    I want something like xpath_list() that returns an array
>    (instead of a delimited, concatenated string) when multiple
>    nodes exist in an XML file.  It feels to me like that'd
>    be a better (cleaner) API.
>

Yes. It's been at the back of my head that it would be a nice idea - when
I first started on contrib/xml and /xml2 array support was rather
primitive.

>    Before I write one, does anyone already have such a
>    patch?  If not, would people be interested if I added
>    xpath_array() that behaves like xpath_list() but returns
>    an array instead of one big string?
>
>    Or... is xpsql on gborg or some other postgresql-xml project a better
>    place for me to be looking?
>

Well, if you like the way that contrib/xml2 works, I would add it there,
but I'm obviously biased. It could just be another wrapper around
pgxml_xpath but use its own traversal of the nodeset instead of
pgxmlnodesettotext. I can't speak for whether anyone else is doing
anything similar, but I haven't heard anyone say so!

Thanks for your interest

John


Re: xpath_list() question for contrib/xml2

From
Bruce Momjian
Date:
Added to TODO:

    * Add xpath_array() to /contrib/xml2 to return results as an array


---------------------------------------------------------------------------

John Gray wrote:
> On Mon, 24 Jan 2005 16:53:47 -0800, Ron Mayer wrote:
>
> > Short summary:
> >
> >    I want something like xpath_list() that returns an array
> >    (instead of a delimited, concatenated string) when multiple
> >    nodes exist in an XML file.  It feels to me like that'd
> >    be a better (cleaner) API.
> >
>
> Yes. It's been at the back of my head that it would be a nice idea - when
> I first started on contrib/xml and /xml2 array support was rather
> primitive.
>
> >    Before I write one, does anyone already have such a
> >    patch?  If not, would people be interested if I added
> >    xpath_array() that behaves like xpath_list() but returns
> >    an array instead of one big string?
> >
> >    Or... is xpsql on gborg or some other postgresql-xml project a better
> >    place for me to be looking?
> >
>
> Well, if you like the way that contrib/xml2 works, I would add it there,
> but I'm obviously biased. It could just be another wrapper around
> pgxml_xpath but use its own traversal of the nodeset instead of
> pgxmlnodesettotext. I can't speak for whether anyone else is doing
> anything similar, but I haven't heard anyone say so!
>
> Thanks for your interest
>
> John
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
>                http://archives.postgresql.org
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073