Oracle从xml截断值中提取

时间:2014-01-02 03:42:22

标签: xml oracle xpath plsql xmltype

我们有一个CLOB列,我们在那里存储相当简单的XML,我需要从中提取其中一个标记的属性。我相信select语句是正确的,因为我得到了一部分值,但它将它截断为80个字符。

这里是XML的片段......

<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <customer>
         <customer_id>ABCDE12346</customer_id>
         <first_name>FAKE</first_name>
         <last_name>CUSTOMER</last_name>
      </customer>
...
    <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here" thumbnail="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here_thumb"/>
</images>
...

...这里是SQL的片段......

select
    xmltype(contact_data).extract('/service_order/@order_number').getStringVal ordnum
   ,extract(xmltype(contact_data) ,'//images/image/@src') imgsrc
from
    my_table
where
    trunc(contact_date) = trunc(sysdate)
and extractvalue(xmltype(contact_data) ,'/service_order/@order_number') = '&ordnum'

URL的开头返回但是,如前所述,URL被截断为80个字符。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

你在sqlplus中执行这个吗? Extract返回XMLType实例,并根据long显示 变量,默认为80.如果增加该值,则可以看到完整的URL。

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src') as url
from
    x;  

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_b

增加long变量的值后,

SQL> set long 120

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src') as url
from
    x;  

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here

您可以使用getStringVal函数将XMLType转换为varchar2,它不依赖于long变量。

SQL> set long 80

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src').getStringval() as url
from
    x; 

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here