4

В таблице есть столбец clob, в котором хранится текст XML такого вида:

<Address>
 <Element type="district">БРЕСТСКАЯ</Element>
 <Element type="region"></Element>
 <Element type="ctype" key="1">Город</Element>
 <Element type="city">КОБРИН</Element>
 <Element type="stype" key="1">Улица</Element>
 <Element type="street">СУВОРОВА</Element>
 <Element type="house">33</Element>
 <Element type="building"></Element>
 <Element type="flat">6</Element>
 <Element type="postcode">225306</Element>
</Address>

Хочется разобрать это силами SQL, получив в результате склейку значений. В данном случае хочу получить на выходе

БРЕСТСКАЯ КОБРИН УлицаСУВОРОВА 33 6

Подскажите, как можно добиться такого результата?

4 ответа 4

4
with t as
(select 
'<Address>
 <Element type="district">БРЕСТСКАЯ</Element>
 <Element type="region"></Element>
 <Element type="ctype" key="1">Город</Element>
 <Element type="city">КОБРИН</Element>
 <Element type="stype" key="1">Улица</Element>
 <Element type="street">СУВОРОВА</Element>
 <Element type="house">33</Element>
 <Element type="building"></Element>
 <Element type="flat">6</Element>
 <Element type="postcode">225306</Element>
</Address>' as xml_ 
from dual)
SELECT  extractvalue(s.column_value, '/*/Element[@type=''district'']')||' '||
        extractvalue(s.column_value, '/*/Element[@type=''city'']')||' '||
        'Улица '||extractvalue(s.column_value, '/*/Element[@type=''street'']')||' '||
        extractvalue(s.column_value, '/*/Element[@type=''house'']')||' '||
        extractvalue(s.column_value, '/*/Element[@type=''flat'']') as VAL
FROM t, table(xmlsequence(EXTRACT(XMLTYPE(t.xml_), '/Address'))) s
3
with x as (
  select xmltype('
<Address>
 <Element type="district">БРЕСТСКАЯ</Element>
 <Element type="region"></Element>
 <Element type="ctype" key="1">Город</Element>
 <Element type="city">КОБРИН</Element>
 <Element type="stype" key="1">Улица</Element>
 <Element type="street">СУВОРОВА</Element>
 <Element type="house">33</Element>
 <Element type="building"></Element>
 <Element type="flat">6</Element>
 <Element type="postcode">225306</Element>
</Address>') xfield
  from dual
)  
select address
from XMLTable('
  let $r := 
  <address>
    {$doc/Address/Element[@type="district"]},{$doc/Address/Element[@type="city"]},{$doc/Address/Element[@type="stype"]},{$doc/Address/Element[@type="street"]},{$doc/Address/Element[@type="house"]}, {$doc/Address/Element[@type="flat"]}
  </address> 
  return $r'
  passing (select xfield from x) as "doc"
  columns address varchar2(1000) path '/address'
);

БРЕСТСКАЯ,КОБРИН,Улица,СУВОРОВА,33, 6

3

Функция xmlSequence объявлена устаревшей. Вместо неё рекомендуется пользоваться xmlTable в новом коде:

with data as (
    select xmlType('
    <Address>
        <Element type="district">БРЕСТСКАЯ</Element>
        <Element type="region"></Element>
        <Element type="ctype" key="1">Город</Element>
        <Element type="city">КОБРИН</Element>
        <Element type="stype" key="1">Улица</Element>
        <Element type="street">СУВОРОВА</Element>
        <Element type="house">33</Element>
        <Element type="building"></Element>
        <Element type="flat">6</Element>
        <Element type="postcode">225306</Element>
    </Address>
    ') as xml from dual
)
select pcode||' '||district||' '||ctype||' '||city||' '||stype||' '||street||' '||house||'/'||flat  
from data d 
cross join xmlTable ('/Address' passing d.xml columns
pcode    varchar2 (8)  path '/Address/Element[@type="postcode"]', 
district varchar2 (64) path '/Address/Element[@type="district"]', 
ctype    varchar2 (32) path '/Address/Element[@type="ctype"][@key="1"]', 
city     varchar2 (64) path '/Address/Element[@type="city"]', 
stype    varchar2 (16) path '/Address/Element[@type="stype"][@key="1"]',
street   varchar2 (64) path '/Address/Element[@type="street"]',
house    number        path '/Address/Element[@type="house"]', 
flat     number        path '/Address/Element[@type="flat"]' 
);

Вывод:

225306 БРЕСТСКАЯ Город КОБРИН Улица СУВОРОВА 33/6

0

Основываясь на https://stackoverflow.com/a/4715997/1646082

Колонка у вас должна быть типа XMLTYPE(т.е. вам надо сделать конвертацию) и тогда можно выполнить SQL типа:

SELECT
  extractvalue(xmlcol, '/*/Element') txtaddress
FROM yourtable
1
  • Колонка типа XMLTYPE в таблице совсем не обязательна, CLOB замечательно приводится к XMLTYPE на лету.
    – Stawros
    1 сен 2015 в 12:22

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.