powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Как разобрать XML?
1 сообщений из 1, страница 1 из 1
Как разобрать XML?
    #39300061
Ролг Хупин
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Есть два хмл:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
DROP TABLE IF EXISTS t2;
CREATE TEMP TABLE t2 (id INT,x xml);

INSERT INTO t2 VALUES (0,
'<tbl>
<comments photo_id="123">
     <comment>this is the first 123 comment</comment>
     <description>this is description 123</description>     
</comments>
<comments photo_id="124">
     <comment>this is the first 124 comment</comment>
     <description>this is description 124</description>
</comments>
</tbl>'::xml);    

INSERT INTO t2 VALUES (1,
'<tbl>
<comments photo_id="123">
     <comment>this is the first 123 comment</comment>
     <description>this is description 123</description>     
</comments>
<comments photo_id="124">
     <comment>this is the first 124 comment</comment>
     <description/>
</comments>
</tbl>'::xml); 



Для id=0 - работает:

Код: sql
1.
2.
3.
4.
5.
6.
7.
SELECT (xpath('./@photo_id', c.node))[1] AS photo_id
     , unnest(xpath('./comment/text()', c.node)) AS comment
     , unnest(xpath('./description/text()', c.node)) AS description     
FROM  (             
    SELECT unnest(xpath('./comments', x)) AS node
    FROM   t2 where id=0
    ) c;




Как сделать, чтобы работала разборка для id=1? т.е. для случая, если какое-то из полей пустое и формат типа такого

<description/>
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Как разобрать XML?
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]