Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / PostgreSQL [игнор отключен] [закрыт для гостей] / Как разобрать XML? / 1 сообщений из 1, страница 1 из 1
30.08.2016, 10:56
    #39300061
Ролг Хупин
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Как разобрать XML?
Есть два хмл:

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


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