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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

I like the idea but is there any way to do this in Postgres?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.