我有一个存储在clob字段中的XML。 xml具有重复节点。我的提取在没有重复节点的情况下工作(一个提交带有clob)。我收到错误EXTRACT Value返回一个节点的值。考虑到我的Oracle版本以及输入字段在我的Oracle表上的CLOB类型这一事实,我有哪些选择?我已经尝试过XMLtable,但是无法让它工作,我不确定它是否能够将我的数据存储在clob中。
我的表:
CREATE TABLE "LEAD_REPORTING_CLOB"
("FILENAME" VARCHAR2(80),
"SHARED_XML" CLOB);
我的clob:
<?xml version="1.0"?>
<LeadReport xmlns="http://www.xxx.yyy/efile">
<Lead>
<AssociatedState>
<StateOrCityCode>AZST</StateOrCityCode>
</AssociatedState>
<AssociatedState>
<StateOrCityCode>MIST</StateOrCityCode>
</AssociatedState>
<PatternDescription>MI state</PatternDescription>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>98.999.99.115</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-25T10:12:19-07:00</IPTs>
</SourceIPAddress>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>192.888.888.888</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-25T10:32:10-07:00</IPTs>
</SourceIPAddress>
<AdditionalInformation>
<ReturnSubmStatus>U</ReturnSubmStatus>
<EmailAddressDomain>emailchair.33xxxx.com</EmailAddressDomain>
<PhoneNumberLastFour>5553</PhoneNumberLastFour>
</AdditionalInformation>
</Lead>
<Lead>
<AssociatedState>
<StateOrCityCode>AZST</StateOrCityCode>
</AssociatedState>
<AssociatedState>
<StateOrCityCode>MIST</StateOrCityCode>
</AssociatedState>
<PatternDescription>MI state2</PatternDescription>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>98.444.44.444</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-25T10:03:43-07:00</IPTs>
</SourceIPAddress>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>192.333.33.333</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-25T10:20:33-07:00</IPTs>
</SourceIPAddress>
<AdditionalInformation>
<ReturnSubmStatus>U</ReturnSubmStatus>
<EmailAddressDomain>emailchair.33xxxx.com</EmailAddressDomain>
<PhoneNumberLastFour>1744</PhoneNumberLastFour>
</AdditionalInformation>
</Lead>
<Lead>
<SubmissionId>9999988861170019999</SubmissionId>
<AssociatedState>
<StateOrCityCode>AZST</StateOrCityCode>
</AssociatedState>
<AssociatedState>
<StateOrCityCode>MIST</StateOrCityCode>
</AssociatedState>
<PatternDescription>MI/AZ</PatternDescription>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>98.222.22.222</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-23T22:07:16-07:00</IPTs>
</SourceIPAddress>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>192.666.66.666</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-25T21:59:21-07:00</IPTs>
</SourceIPAddress>
<SourceIPAddress>
<IPAddress>
<IPv4AddressTxt>198.111.111.111</IPv4AddressTxt>
</IPAddress>
<IPTs>2016-04-26T00:12:37-07:00</IPTs>
</SourceIPAddress>
<AdditionalInformation>
<ReturnSubmStatus>R</ReturnSubmStatus>
<EmailAddressDomain>emailchair.33xxxx.com</EmailAddressDomain>
<PhoneNumberLastFour>0503</PhoneNumberLastFour>
</AdditionalInformation>
</Lead>
</LeadReport>
我的选择:
select
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead/SubmissionId',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "SubmissionId",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//StateSubmissionId',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "state_submission_id",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//StateOrCityCode',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "StateOrCityCode",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead/PatternDescription',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "PatternDescription",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//IPv4AddressTxt',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "IPv4AddressTxt",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//TCPPortNumber',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "TCPPortNumber",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//IPTs',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "IPTs",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//DeviceId',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "DeviceId",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//DeviceIdTs',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "DeviceIdTs",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//ReturnSubmStatus',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "ReturnSubmStatus",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//EFIN',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "EFIN",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//PTIN',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "PTIN",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//EmailAddressDomain',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "EmailAddressDomain",
nvl(EXTRACTVALUE(xmltype(ocr.shared_xml),
'//Lead//PhoneNumberLastFour',
'xmlns="http://www.xxx.yyy/efile"'),
'') as "PhoneNumberLastFour"
FROM
lead_reporting_clob ocr;
答案 0 :(得分:1)
无论如何,extractvalue()
function已被弃用。并且nvl()
调用你的代码是多余的,因为你用空字符串替换null,这与Oracle中的null相同。
您可以使用XMLTable()
从多个节点中提取信息:
select x.*
from lead_reporting_clob ocr
cross join xmltable(xmlnamespaces(default 'http://www.xxx.yyy/efile'),
'/LeadReport/Lead'
passing xmltype(ocr.shared_xml)
columns "SubmissionId" varchar2(20) path 'SubmissionId',
"state_submission_id" varchar2(20) path 'AssociatedState/StateSubmissionId',
"StateOrCityCode" varchar2(4) path 'AssociatedState/StateOrCityCode',
"PatternDescription" varchar2(3) path 'PatternDescription',
"IPv4AddressTxt" varchar2(15) path 'SourceIPAddress/IPAddress/IPv4AddressTxt',
"TCPPortNumber" number path 'SourceIPAddress/TCPPortNumber',
"IPTs" varchar2(25) path 'SourceIPAddress/IPTs',
"DeviceId" varchar2(48) path 'SourceDeviceId/DeviceId',
"DeviceIdTs" varchar2(25) path 'SourceDeviceId/DeviceIdTs',
"ReturnSubmStatus" varchar2(1) path 'AdditionalInformation/ReturnSubmStatus',
"EFIN" varchar2(10) path 'AdditionalInformation/EFIN',
"PTIN" varchar2(10) path 'AdditionalInformation/PTIN',
"EmailAddressDomain" varchar2(16) path 'AdditionalInformation/EmailAddressDomain',
"PhoneNumberLastFour" varchar2(4) path 'AdditionalInformation/PhoneNumberLastFour'
) x;
SubmissionId state_submission_id Stat Pat IPv4AddressTxt TCPPortNumber IPTs DeviceId DeviceIdTs R EFIN PTIN EmailAddressDoma Phon
-------------------- -------------------- ---- --- --------------- ------------- ------------------------- ------------------------------------------------ ------------------------- - ---------- ---------- ---------------- ----
530153999999zdhxxx OLF 999.99.99.999 443 2016-02-05T13:16:55-05:00 74B3A80AE5C68F2B65753C17XX59E5XX3779999A 2016-02-05T13:16:55-05:00 A 555555 GMAIL.COM 1899
9999992016108floxxx2 9999992016108xhaxxx2 GAST OLF 172.56.5.100 443 2016-04-17T08:07:54-04:00 6B9D4C4B0155EEE8FA30391BC8A012950009B366 2016-04-17T08:07:54-04:00 A 444444 YAHOO.COM 3822
您需要设置每个columns
条目的数据类型和大小,以匹配您实际期望的内容;我从样本数据中得到了近似值。
使用更新的示例XML,每个引导下都有两个重复节点,因此您需要最初将它们作为自己的XMLTypes,然后进一步调用XMLTable来解构它们:
select xl."SubmissionId", xas."state_submission_id", xas."StateOrCityCode",
xl."PatternDescription", xsi."IPv4AddressTxt", xsi."TCPPortNumber", xsi."IPTs",
xl."DeviceId", xl."DeviceIdTs", xl."ReturnSubmStatus", xl."EFIN", xl."PTIN",
xl."EmailAddressDomain", xl."PhoneNumberLastFour"
from lead_reporting_clob ocr
cross join xmltable(xmlnamespaces(default 'http://www.xxx.yyy/efile'),
'/LeadReport/Lead'
passing xmltype(ocr.shared_xml)
columns "SubmissionId" varchar2(20) path 'SubmissionId',
associatedstate xmltype path 'AssociatedState',
"PatternDescription" varchar2(10) path 'PatternDescription',
sourceipaddress xmltype path 'SourceIPAddress',
"DeviceId" varchar2(48) path 'SourceDeviceId/DeviceId',
"DeviceIdTs" varchar2(25) path 'SourceDeviceId/DeviceIdTs',
"ReturnSubmStatus" varchar2(1) path 'AdditionalInformation/ReturnSubmStatus',
"EFIN" varchar2(10) path 'AdditionalInformation/EFIN',
"PTIN" varchar2(10) path 'AdditionalInformation/PTIN',
"EmailAddressDomain" varchar2(16) path 'AdditionalInformation/EmailAddressDomain',
"PhoneNumberLastFour" varchar2(4) path 'AdditionalInformation/PhoneNumberLastFour'
) xl
cross join xmltable(xmlnamespaces(default 'http://www.xxx.yyy/efile'),
'/AssociatedState'
passing xl.associatedstate
columns "state_submission_id" varchar2(20) path 'StateSubmissionId',
"StateOrCityCode" varchar2(4) path 'StateOrCityCode'
) xas
cross join xmltable(xmlnamespaces(default 'http://www.xxx.yyy/efile'),
'/SourceIPAddress'
passing xl.sourceipaddress
columns "IPv4AddressTxt" varchar2(15) path 'IPAddress/IPv4AddressTxt',
"TCPPortNumber" number path 'TCPPortNumber',
"IPTs" varchar2(25) path 'IPTs'
) xsi;
SubmissionId state_submission_id Stat PatternDes IPv4AddressTxt TCPPortNumber IPTs DeviceId DeviceIdTs R EFIN PTIN EmailAddressDoma Phon
-------------------- -------------------- ---- ---------- --------------- ------------- ------------------------- ------------------------------------------------ ------------------------- - ---------- ---------- ---------------- ----
AZST MI state 98.999.99.115 2016-04-25T10:12:19-07:00 U emailchair.33xxx 5553
AZST MI state 192.888.888.888 2016-04-25T10:32:10-07:00 U emailchair.33xxx 5553
MIST MI state 98.999.99.115 2016-04-25T10:12:19-07:00 U emailchair.33xxx 5553
MIST MI state 192.888.888.888 2016-04-25T10:32:10-07:00 U emailchair.33xxx 5553
AZST MI state2 98.444.44.444 2016-04-25T10:03:43-07:00 U emailchair.33xxx 1744
AZST MI state2 192.333.33.333 2016-04-25T10:20:33-07:00 U emailchair.33xxx 1744
MIST MI state2 98.444.44.444 2016-04-25T10:03:43-07:00 U emailchair.33xxx 1744
MIST MI state2 192.333.33.333 2016-04-25T10:20:33-07:00 U emailchair.33xxx 1744
9999988861170019999 AZST MI/AZ 98.222.22.222 2016-04-23T22:07:16-07:00 R emailchair.33xxx 0503
9999988861170019999 AZST MI/AZ 192.666.66.666 2016-04-25T21:59:21-07:00 R emailchair.33xxx 0503
9999988861170019999 AZST MI/AZ 198.111.111.111 2016-04-26T00:12:37-07:00 R emailchair.33xxx 0503
9999988861170019999 MIST MI/AZ 98.222.22.222 2016-04-23T22:07:16-07:00 R emailchair.33xxx 0503
9999988861170019999 MIST MI/AZ 192.666.66.666 2016-04-25T21:59:21-07:00 R emailchair.33xxx 0503
9999988861170019999 MIST MI/AZ 198.111.111.111 2016-04-26T00:12:37-07:00 R emailchair.33xxx 0503
14 rows selected
交叉连接将其转换为比您预期更多的行;您可以将重复IP(例如,使用listagg)等内容组合在一起,或者将重复节点提取到单独的关系表中 - 这实际上取决于您打算如何处理查询结果。