简体   繁体   English

从 Oracle sql 中的 xml 字段解析数据

[英]Parse data from xml field in Oracle sql

I am looking to extract parts of an xml field in an Oracle database using sql.我希望使用 sql 提取 Oracle 数据库中的 xml 字段的一部分。 I have looked at numerous posts and I think I am on the right track and maybe it is just I'm not correctly identifying the right path.我看过很多帖子,我认为我走在正确的轨道上,也许只是我没有正确识别正确的道路。 I'm having trouble following the XML path to the field.我在遵循 XML 到现场的路径时遇到了麻烦。 I have laid out what I'm trying which produces no results and I've made the XML look as readable as possible.我已经列出了我正在尝试但没有产生任何结果的内容,并且我已经使 XML 看起来尽可能可读。 In this instance I need to pull out the year/month/day/hour/minute/second/milliseconds from DateTimeReceived and DateTimeCompleted.在这种情况下,我需要从 DateTimeReceived 和 DateTimeCompleted 中提取年/月/日/小时/分钟/秒/毫秒。

SELECT 
stbl.EXT_TRANSACTIONIDTXT,  
stbl.EXT_RESPONSETEXT, 
xt.* 
FROM sample_table stbl, 
    xmltable('/ResponseEx/Response/TransactionDetailsEx/TransactionDetails'
    PASSING XMLTYPE(stbl.EXT_RESPONSETEXT)
        COLUMNS 
            Year  PATH 'DateTimeReceived/Year',
            Month  PATH 'DateTimeReceived/Month'
            )xt;

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ResponseEx xmlns="urn:lnrisk:ws:testing:ruleplan@ver=1">
 <Response> 
 <Messages>
 <Message>
 <Type>G</Type>
 <Code>C6</Code>
 <Message>No Data Found.
 </Message>
 </Message>
 <Message>
 <Type>G</Type>
 <Message>ORDER NUMBER: AQF6LSL
 </Message>
 </Message>
 </Messages>
 <RequesterInformation>
 <Name>BIGBOB</Name>
 <AccountNumber>1111ABC</AccountNumber>
 </RequesterInformation>
 <TransactionDetailsEx>
 <TransactionDetails>
 <RuleplanId>111112</RuleplanId>
 <DateTimeReceived>
 <Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 <Hour24>12</Hour24>
 <Minute>58</Minute>
 <Second>17</Second>
 <MilliSeconds>317</MilliSeconds>
 </DateTimeReceived>
 <DateTimeCompleted>
 <Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 <Hour24>12</Hour24>
 <Minute>58</Minute>
 <Second>17</Second>
 <MilliSeconds>563</MilliSeconds>
 </DateTimeCompleted>
 <QuoteBacks/>
 </TransactionDetails>
 <ProcessingStatus>Complete with Errors</ProcessingStatus>
 <TransactionId>ABC895CL</TransactionId>
 </TransactionDetailsEx>
 <SearchBy>
 <Subjects/>
 <Vehicles>
 <InquiryVehicle vehicleId="VEH1">
 <PlateNumber>EEEE44444</PlateNumber>
 <PlateState>AB</PlateState>
 </InquiryVehicle></Vehicles>
 </SearchBy><Products>
 <ClaimsDataFill>
 <InquiryClaimsDataFill>
 <CarrierName>BIGBOB</CarrierName>
 <CarrierPolicyNumber>9999999999</CarrierPolicyNumber>
 <ClaimNumber>cc:98486965</ClaimNumber>
 <ClaimState>CA</ClaimState>
 <ParticipantNumber>001</ParticipantNumber>
 <DateofLoss><Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 </DateofLoss>
 <ParticipantType>Claimant</ParticipantType>
 <ParticipantRole>Owner</ParticipantRole>
 <SearchBy>
 <Vehicles>
 <Vehicle ref="VEH1">VEH1</Vehicle>
 </Vehicles>
 </SearchBy>
 </InquiryClaimsDataFill>
 </ClaimsDataFill>
 </Products>
 <ProductResults>
 <ClaimsDataFillResults/>
 </ProductResults>
 </Response>
 </ResponseEx>

xmlns="urn:lnrisk:ws:testing:ruleplan@ver=1" - your xml has no standard default namespace, to query this data you have to include the information about in xmltable statement. xmlns="urn:lnrisk:ws:testing:ruleplan@ver=1" - 您的 xml 没有标准的默认命名空间,要查询此数据,您必须在 xmltable 语句中包含有关信息。

  ... xmltable(xmlnamespaces( default 'urn:lnrisk:ws:testing:ruleplan@ver=1'), '/ResponseEx/Response/TransactionDetailsEx/TransactionDetails'...

SELECT 

xt.* 
FROM     xmltable(xmlnamespaces( default 'urn:lnrisk:ws:testing:ruleplan@ver=1'), '/ResponseEx/Response/TransactionDetailsEx/TransactionDetails'
    PASSING XMLTYPE('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ResponseEx xmlns="urn:lnrisk:ws:testing:ruleplan@ver=1">
 <Response> 
 <Messages>
 <Message>
 <Type>G</Type>
 <Code>C6</Code>
 <Message>No Data Found.
 </Message>
 </Message>
 <Message>
 <Type>G</Type>
 <Message>ORDER NUMBER: AQF6LSL
 </Message>
 </Message>
 </Messages>
 <RequesterInformation>
 <Name>BIGBOB</Name>
 <AccountNumber>1111ABC</AccountNumber>
 </RequesterInformation>
 <TransactionDetailsEx>
 <TransactionDetails>
 <RuleplanId>111112</RuleplanId>
 <DateTimeReceived>
 <Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 <Hour24>12</Hour24>
 <Minute>58</Minute>
 <Second>17</Second>
 <MilliSeconds>317</MilliSeconds>
 </DateTimeReceived>
 <DateTimeCompleted>
 <Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 <Hour24>12</Hour24>
 <Minute>58</Minute>
 <Second>17</Second>
 <MilliSeconds>563</MilliSeconds>
 </DateTimeCompleted>
 <QuoteBacks/>
 </TransactionDetails>
 <ProcessingStatus>Complete with Errors</ProcessingStatus>
 <TransactionId>ABC895CL</TransactionId>
 </TransactionDetailsEx>
 <SearchBy>
 <Subjects/>
 <Vehicles>
 <InquiryVehicle vehicleId="VEH1">
 <PlateNumber>EEEE44444</PlateNumber>
 <PlateState>AB</PlateState>
 </InquiryVehicle></Vehicles>
 </SearchBy><Products>
 <ClaimsDataFill>
 <InquiryClaimsDataFill>
 <CarrierName>BIGBOB</CarrierName>
 <CarrierPolicyNumber>9999999999</CarrierPolicyNumber>
 <ClaimNumber>cc:98486965</ClaimNumber>
 <ClaimState>CA</ClaimState>
 <ParticipantNumber>001</ParticipantNumber>
 <DateofLoss><Year>2020</Year>
 <Month>5</Month>
 <Day>19</Day>
 </DateofLoss>
 <ParticipantType>Claimant</ParticipantType>
 <ParticipantRole>Owner</ParticipantRole>
 <SearchBy>
 <Vehicles>
 <Vehicle ref="VEH1">VEH1</Vehicle>
 </Vehicles>
 </SearchBy>
 </InquiryClaimsDataFill>
 </ClaimsDataFill>
 </Products>
 <ProductResults>
 <ClaimsDataFillResults/>
 </ProductResults>
 </Response>
 </ResponseEx>')
        COLUMNS 
            Year  PATH 'DateTimeReceived/Year',
            Month  PATH 'DateTimeReceived/Month'
            )xt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM