我正在尝试从oracle数据库中的XML中选择各种数据。然后我可以将这些项插入到另一个表中以进行正常的SQL查询(即刷新XML)。所以我在下面的XML数据中列出了我的oracle数据库中的Clob列。我用this示例来做到这一点。
XML数据
<?xml version="1.0" encoding="utf-8"?>
<CarParkDataImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.transportdirect.info/carparking B:/CODE/carparks/CarParking.xsd" xmlns="http://www.transportdirect.info/carparking">
<CarPark>
<CarParkRef>3</CarParkRef>
<CarParkName>Nunnery Lane</CarParkName>
<Location>York</Location>
<Address>Nunnery Lane--York--North Yorkshire</Address>
<Postcode>YO23 1AA</Postcode>
<Telephone>01904551309</Telephone>
<MinCostPence>200</MinCostPence>
<IsParkAndRide>false</IsParkAndRide>
<StayType>Short</StayType>
</CarPark>
</CarParkDataImport>
我从这个查询开始
select car.ref as car_ref
, car.loc as car_loc
from XML_DOCUMENTS
, xmltable('/CarParkDataImport/CarPark'
passing XML_DOCUMENTS.XMLDOC
columns
"ref" varchar2(30) path 'CarParkRef'
, "loc" varchar2(30) path 'Location'
) car
但是我收到了错误消息,
ORA-00932: inconsistent datatypes: expected - got CLOB
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
Error at Line: 5 Column: 24
所以它期望该列成为XMLTYPE列,现在我尝试使用此列创建一个表并将XML插入到该列中,但所有列都说是(XMLTYPE),我想我必须注册一个模式这个工作,但寄存器架构抱怨不是有效的xml,所以我放弃了回到CLOB。
使用提取值的第二个查询似乎有效,但是带回了空值
SELECT EXTRACTVALUE(xmltype(xmldoc), '/CarParkDataImport/CarPark/Location')
FROM xml_documents;
我在XMLDOC列中有9行,它表示找到了9行,但都是null。然后我尝试了另一个解决方案,如下所示
with src as (select xmltype(to_clob(XMLDOC)) /*)*/ AS messagetext FROM xml_documents)
select car.REF as car_ref
, car.LOC as car_loc
from src s
, xmltable('/CarParkDataImport/CarPark'
passing S.messagetext
columns
"REF" varchar2(30) path 'CarParkRef'
, "LOC" varchar2(30) path 'Location'
) car
;
这似乎在它运行时具有相同的效果,但却没有带来任何效果。
我错过了什么?我是否必须声明命名空间以及如何?我的路径是否已被破坏,或者我应该尝试返回加载到XMLTYPE列中?
答案 0 :(得分:1)
xmltabe需要xmltype数据。因此,将clob转换为xmltype。您还必须声明xml命名空间。
SQL> create table xml_documents(
xmldoc clob
);
Table created.
SQL> insert into xml_documents values(
'<?xml version="1.0" encoding="utf-8"?>
<CarParkDataImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.transportdirect.info/carparking B:/CODE/carparks/CarParking.xsd" xmlns="http://www.transportdirect.info/carparking">
<CarPark>
<CarParkRef>3</CarParkRef>
<CarParkName>Nunnery Lane</CarParkName>
<Location>York</Location>
<Address>Nunnery Lane--York--North Yorkshire</Address>
<Postcode>YO23 1AA</Postcode>
<Telephone>01904551309</Telephone>
<MinCostPence>200</MinCostPence>
<IsParkAndRide>false</IsParkAndRide>
<StayType>Short</StayType>
</CarPark>
</CarParkDataImport>'
);
commit;
1 row created.
Commit complete.
SQL> select car.ref as car_ref
, car.loc as car_loc
from xml_documents
, xmltable(xmlnamespaces(default 'http://www.transportdirect.info/carparking'),'CarParkDataImport/CarPark'
passing xmltype(xml_documents.xmldoc)
columns
ref varchar2(30) path 'CarParkRef'
,loc varchar2(30) path 'Location'
) car;
CAR_REF CAR_LOC
------------------------------ ------------------------------
3 York