简体   繁体   中英

Parse data from xml field in Oracle sql

I am looking to extract parts of an xml field in an Oracle database using sql. 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. I have laid out what I'm trying which produces no results and I've made the XML look as readable as possible. In this instance I need to pull out the year/month/day/hour/minute/second/milliseconds from DateTimeReceived and 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.

  ... 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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