Skip to content
Advertisement

Postgresql – How to read xmlattributes on xml?

I Have xml :

<?xml version="1.0" encoding="UTF-8"?>
<attachments>
  <entry file="cewe_gw.jpg" name="cewe_gw.jpg"/>
  <entry file="wp1827515.png" name="wp1827515.png"/>
</attachments>

I want to get list file? ex:

cewe_gw.jpg
wp1827515.png

Advertisement

Answer

demo:db<>fiddle

To select any element or attribute, you can use XPath:

SELECT 
    xpath('//entry/@file',xml)
FROM mydata

This returns an array of file attributes. You can extract them using unnest()

SELECT 
    unnest(xpath('//entry/@file',xml))
FROM mydata
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement