2

Let's say I have a table named "content" and a column named "images" with data type xml.

I have a row in which its column "images" already has a value:

<Pictures>
  <Picture ID="1">
    <big>../srcs/Big_Buck_Bunny.jpg</big>
  </Picture>
  <Picture ID="2">
    <big>../srcs/pic_jpeg.jpeg</big>
  </Picture>
</Pictures>

and I would like to add the elements below as the last child of < Pictures>

<Picture ID="3">
  <big>../srcs/pic_gif.gif</big>
</Picture>

In DB2, I understand it can be done as shown in Update 11 of IBM's Update XML in DB2 but how do I do this using PostgreSQL?

I also found a similar question to this but no one has answered it.

Community
  • 1
  • 1
knightowl
  • 21
  • 3

3 Answers3

0

Select everything in the images column and then in your php, add the "...". Then update content set images= katong new xml na added na ang katong picture with id=3 where id=id sa row na imong iupdate.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
0

If the structure of the XML content of the 'images' column is predefined, in your sql statement, you can make use of every new entry as 'xmlelement' and then operating on xml type in postgres, you can make use of the xml processing functions such as xmlconcat(see documentation). It's all about operating on proper types supported by the RDBMS of your choice.

James
  • 1
  • 1
  • 1
  • 1
0

I had the problem today, 2 years later, and work out something like this:

SELECT xmlelement(name "Pictures",
              xmlagg(t1.x), 
    '<Picture ID="3"><big>../srcs/pic_gif.gif</big></Picture>'::xml) xrec   -- option 1
     --xmlelement(name "Picture", xmlattributes(3 as id), xmlelement(name big,'../srcs/pic_gif.gif'))) xrex        -- option 2     
FROM (       
SELECT unnest(XPATH('/Pictures/Picture', images)) X
FROM content WHERE UNQKEY = 8) t1; 

There are two options to add the new element, either via a string or via another element; options 1 and 2. (Un)Comment the line you want to use.

Nico Botha
  • 21
  • 1
  • 4