2011-09-22 78 views
3

我的postgres數據庫在UTF-8和客戶端在UTF-8也。
當我嘗試:Postgres xmlelement編碼

select xmlelement(name pampam, xmlattributes('русский' as "top")); 

我回去:

<pampam top="&#x440;&#x443;&#x441;&#x441;&#x43A;&#x438;&#x439;"/>` 

但我想回去屬性作爲是(俄文UTF-8 IE),而不是&#x440 ....
我該怎麼做?


這是解決不了問題的,我用xmleliment構造XML從數據中獲得回來查詢。 而我找不到另一種方式來做到這一點...

回答

1

這似乎是不可能的。打印的值傳遞給libxml,這就是它選擇打印它的方式。

0

也許這是不是最好的解決方案,但這個工作對我來說:

SELECT xmlparse(CONTENT '<element attribute=''áéíóú´ñÇ`''>value</element>') 

我回去:

<element attribute='áéíóú´ñÇ`'>value</element> 
0

我剛剛創建了一個字符實體字符轉換功能,entity2char

select 
    xmlparse(content 
     entity2char((
      xmlelement(name pampam, xmlattributes('русский' as "top")) 
     )::text) 
    ); 
     xmlparse   
------------------------- 
<pampam top="русский"/> 
0

我用plpythonu寫函數使用lxml

CREATE OR REPLACE FUNCTION xmlelementpy (
       nodename_in varchar, 
       nodetext_in text, 
       attrname_in varchar [], 
       attrvalue_in varchar [] 
) 
RETURNS xml AS 
$body$ 
from lxml import etree 
noattr = 0 
isxml = 1 
if (attrname_in is None) or (attrname_in is None): 
    noattr = 1 
elif len(attrname_in) <> len(attrvalue_in): 
    return None; 

newnode = etree.Element(nodename_in) 
if nodetext_in is not None: 
    try: 
     schema = etree.fromstring(nodetext_in) 
    except: 
     newnode.text = nodetext_in 
     isxml = 0 
    if isxml <> 0: 
     newnode.append(etree.XML(nodetext_in)) 

if noattr <> 1: 
    for i in range(len(attrname_in)): 
     if (attrvalue_in[i] is not None): 
      attrvalue = attrvalue_in[i].decode('UTF-8') 
      newnode.set(attrname_in[i], attrvalue) 

return etree.tounicode(newnode, pretty_print=True) 
$body$ 
LANGUAGE 'plpythonu' 
VOLATILE 
CALLED ON NULL INPUT 
SECURITY INVOKER 
COST 100;