2017-03-01 95 views
0

美好的一天!任何人都可以幫忙在XML解析attrbute的價值和重點,如:的XPath獲得的價值和屬性鍵

隨着

SELECT xpath('/el:GRID/@*', 
'<el:GRID xmlns:el="http://example.com" xmlns:p="http://example1.com" p:title="Joda" p_titler="Dookoo"> 
    217 
    <p:columns> 
     <el:GRDCOL p:length="1">226 
      <el:BOX p:kory="dory">228 
      </el:BOX> 
     </el:GRDCOL> 
    </p:columns> 
</el:GRID>', 
    ARRAY[ARRAY['el', 'http://example.com'],array['p','http://example1.com']]); 

我得到{Joda,Dookoo}

但我想造成這樣的:

{p:title Joda, p_titler Dookoo} 

我需要它來解析動態創建的XML,在這裏我不知道所有的屬性名稱...

回答

0

還有就是如何獲得的名稱/值對XML屬性:

with t(x,ns) as (values(
'<el:GRID xmlns:el="http://example.com" xmlns:p="http://example1.com" p:title="Joda" p_titler="Dookoo"> 
    217 
    <p:columns> 
     <el:GRDCOL p:length="1">226 
      <el:BOX p:kory="dory">228 
      </el:BOX> 
     </el:GRDCOL> 
    </p:columns> 
</el:GRID>'::xml, ARRAY[ARRAY['el', 'http://example.com'],array['p','http://example1.com']])) 

select tn.*, tv.* 
    from t cross join lateral 
    (select * from unnest(xpath('/el:GRID/@*', t.x, t.ns)) with ordinality) as tv(v,i) cross join lateral 
     (select * from unnest(xpath('name(/el:GRID/@*['||tv.i||'])', x, t.ns))) as tn(v) 

,從而獲得所需的表示只是改變select tn.*, tv.* from ...select concat('{', string_agg(concat(tn.v, ' ', tv.v), ', '), '}') from ...

+0

豔光四射,謝謝 –