使用Xquery和多个嵌套命名空间查询Oracle数据库XML数据类型列

时间:2021-10-17 07:53:14

I am sorry to have to come cap in hand to the community with this, I have been banging my head against this problem for a week, reading and researching the Oracle documentation and these forums. The closest model answer I could get was this

我很遗憾不得不带着这个问题与社区联系,我一直在讨论这个问题一周,阅读和研究Oracle文档和这些论坛。我能得到的最接近的模特答案就是这个

I have this XML in an oracle colum with datatype XML:

我在oracle colum中使用数据类型XML的XML:

    <TravelReservationRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schema.atravelcompany.com/TravelReservationTransaction" transactionType="TICKETED" creationDateTime="2015-06-30T21:01:09.0405878Z">
      <CWTTravelReservation xmlns="http://schema.atravelcompany.com/TravelReservation">
        <Reservation accountNumber="12345678" lastModifiedTimestamp="2015-06-30T21:01:09.0717888Z" recordLocatorNumber="ABCDEFG" supplierService="SUPPLIER" reservationType="Business" InternationalFlag="false" bookingDate="2015-06-30">
          <VendorList>
            <Vendor vendorCode="AD" vendorClassCode="1" vendorClassification="Air"/>
            <Vendor vendorCode="RT" vendorClassCode="3" vendorName="SOME HOTEL SOMEWHERE" vendorClassification="Hotel"/>
          </VendorList>
          <TravelerList>
            <Traveler sequence="1" guid="A:GUID">
              <Person birthDate="XXXX-XX-XX">
                <Name xmlns="http://schema.atravelcompany.com/V1" namePrefix="MR" firstName="JOHN " lastName="DOE"/>
              </Person>
              <TravelerAddress type="Alternate" line1="A HOUSE" stateProvince="A PLACE"/>
              <Client>
                <ClientTop xmlns="http://schema.atravelcompany.com/V1" guid="ANOTHER:GUID"/>
                <ClientSub xmlns="http://schema.atravelcompany.com/V1"/>
              </Client>
              <TravelerEmail emailAddress="email@address.com"/>
              <TravelerPhone usage="Mobile"/>
              <TravelerType description="SOMETHING"/>
              <EmergencyContactList>
                <EmergencyContact>
                  <EmergencyContactPhone/>
                  <EmergencyContactEmail/>
                  <UnparsedGDSContent/>
                </EmergencyContact>
              </EmergencyContactList>
              <PassportList>
                <Passport xmlns="http://schema.atravelcompany.com/V1" number="XXXXXXXX" issuingCountry="BR"/>
              </PassportList>
            </Traveler>
          </TravelerList>
          <SegmentGroupList>
            <SegmentGroup lowFareAmount="0">
              <SegmentList>
                <Segment segmentEndTimestamp="2015-07-01T07:15:00" segmentTravelDurationValue="0117" segmentTypeDescription="Air" segmentBeginTimestamp="2015-07-01T05:58:00" vendorCode="AD" vendorClassCode="1" supplierStatusCode="YK" confirmationNumber="SOMETHING" segmentNumber="1" manualBookingFlag="false">
                  <SegmentTravelerList>
                    <SegmentTraveler travelerIdRef="1"/>
                  </SegmentTravelerList>
                  <AirSegment aircraftTypeCode="E90" beginAirportCode="VCP" endAirportCode="FLN" classOfServiceCode="V" connectionType="false" eTicketFlag="true" flightNumber="4050" inFlightMealDescription="" travelDistanceValue="320">
                    <CodeShare/>
                  </AirSegment>
                </Segment>
                <Segment segmentEndTimestamp="2015-07-03T12:38:00" segmentTravelDurationValue="0120" segmentTypeDescription="Air" segmentBeginTimestamp="2015-07-03T11:18:00" vendorCode="AD" vendorClassCode="1" supplierStatusCode="YK" confirmationNumber="SOMETHING" segmentNumber="3" manualBookingFlag="false">
                  <SegmentTravelerList>
                    <SegmentTraveler travelerIdRef="1"/>
                  </SegmentTravelerList>
                  <AirSegment aircraftTypeCode="E90" beginAirportCode="A" endAirportCode="B" classOfServiceCode="G" connectionType="false" eTicketFlag="true" flightNumber="4064" inFlightMealDescription="" travelDistanceValue="320">
                    <CodeShare/>
                  </AirSegment>
                </Segment>
              </SegmentList>
              <InvoiceList>
                <Invoice ticketNumber="8003674469" travelerRefId="1" exchangeTransactionFlag="false"/>
              </InvoiceList>
            </SegmentGroup>
            <SegmentGroup>
              <SegmentList>
                <Segment segmentEndTimestamp="2015-07-03T11:18:00" segmentTypeDescription="Hotel" reservedUnitQuantity="1" segmentBeginTimestamp="2015-07-01T07:15:00" vendorCode="RT" vendorClassCode="3" supplierStatusCode="HK" confirmationNumber="000000000" currencyCode="BRL" segmentNumber="2" rateFareAmount="218.70" manualBookingFlag="false">
                  <HotelSegment cityCode="C" hotelPropertyCode="1234" propertyName="A HOTEL" roomAdultQuantity="1" roomTypeCode="C2T01J">
                    <HotelAddressList>
                      <HotelAddress type="Alternate" line1="AN ADDRESS"/>
                    </HotelAddressList>
                    <HotelEmailAddress/>
                    <HotelFaxNumber phoneNumber="1234"/>
                    <HotelPhoneNumber phoneNumber="1234"/>
                  </HotelSegment>
                </Segment>
              </SegmentList>
            </SegmentGroup>
          </SegmentGroupList>
          <ReservationPCCList>
            <ReservationPCC pseudoCityCode="CI6C" supplierService="Sabre" pseudoCityType="Booking"/>
          </ReservationPCCList>
          <BookingAgencyList>
            <BookingAgency>
              <AgencyPhoneList>
                <AgencyPhone phoneNumber="321321321321"/>
              </AgencyPhoneList>
            </BookingAgency>
          </BookingAgencyList>
        </Reservation>
      </CWTTravelReservation>
    </TravelReservationRequest>

I need to be able to retrieve values and possibly lists of values from this XML using something like the query in the model answer however try as I might I cannot get this working and I need some hand-holding.

我需要能够使用类似于模型答案中的查询来检索此XML中的值和可能的值列表,但请尝试我可能无法使其工作,我需要一些手持。

My latest attempt looks like this, based on the example and just does not work at all.

根据示例,我的最新尝试看起来像这样,根本不起作用。

    select t.*
            from 
            sch_edw_stg.mdf_audit a, 
            xmltable(xmlnamespaces('http://schema.acompany.com/V1' as "v1"
                                        ,'http://schema.acompany.com/TravelReservation' as "resr"
                                        ,'http://schema.acompany.com/TravelReservationTransaction' as "trans"
                                        ,'http://www.w3.org/2001/XMLSchema' as "xsd"
                                        ,'http://www.w3.org/2001/XMLSchema-instance' as "xsi")
                          ,'for $d in resr:/CWTTravelReservation:Value
                            return $d' passing a.mdf_xml_text columns value varchar2(100) path '/') as t;  

and I have tried many other permutations and examples from here and elsewhere. The best I can do is return two null values and I am pretty sure this is down to not being able to precisely identify the node I need relative to the base of the XML.

我从这里和其他地方尝试了许多其他的排列和例子。我能做的最好的事情是返回两个空值,我很确定这是因为无法精确识别我需要的节点相对于XML的基础。

This is the code that brings back a bunch of nulls but at least runs:

这是带回一堆空值但至少运行的代码:

    SELECT xt.*
    FROM   sch_edw_stg.mdf_audit x,
           XMLTABLE(XMLNAMESPACES ('http://schema.acompany.com/TravelReservationTransaction' AS "e"),'/'
             PASSING x.mdf_xml_text
             COLUMNS 
               "VENDORCODE"    VARCHAR2(255)  PATH 'e:vendorCode',
               "VENDORCLASSCODE" VARCHAR2(255) PATH 'e:vendorClassCode',
               "VENDORCLASSIFICATION" VARCHAR2(255) PATH 'e:vendorClassification',
               "VENDORNAME" VARCHAR2(255) PATH 'e:vendorName'

             ) xt;

Can anyone give me a working xquery example based on this XML and give me some pointers as to why my attempts are failing?

任何人都可以给我一个基于这个XML的工作xquery示例,并给我一些指示,为什么我的尝试失败了?

Thanks.

谢谢。

Ed.

埃德。

1 个解决方案

#1


0  

You need to give the full path down to your Vendor nodes, including specifying the relevant namespace at each level:

您需要向供应商节点提供完整路径,包括在每个级别指定相关的命名空间:

/trans:TravelReservationRequest/resr:CWTTravelReservation/resr:Reservation/resr:VendorList/resr:Vendor

And you are accessing the attributes of those nodes so you need to use the @ syntax to see them in your PATH:

并且您正在访问这些节点的属性,因此您需要使用@语法在PATH中查看它们:

select t.*
from mdf_audit a
cross join xmltable(
  xmlnamespaces('http://schema.atravelcompany.com/V1' as "v1"
      ,'http://schema.atravelcompany.com/TravelReservation' as "resr"
      ,'http://schema.atravelcompany.com/TravelReservationTransaction' as "trans"
      ,'http://www.w3.org/2001/XMLSchema' as "xsd"
      ,'http://www.w3.org/2001/XMLSchema-instance' as "xsi")
    ,'/trans:TravelReservationRequest/resr:CWTTravelReservation/resr:Reservation/resr:VendorList/resr:Vendor'
  passing a.mdf_xml_text
  columns 
    "VENDORCODE" VARCHAR2(255) PATH '@vendorCode',
    "VENDORCLASSCODE" VARCHAR2(255) PATH '@vendorClassCode',
    "VENDORCLASSIFICATION" VARCHAR2(255) PATH '@vendorClassification',
    "VENDORNAME" VARCHAR2(255) PATH '@vendorName'
) t;

VENDORCODE VENDORCLAS VENDORCLAS VENDORNAME                   
---------- ---------- ---------- ------------------------------
AD         1          Air                                      
RT         3          Hotel      SOME HOTEL SOMEWHERE          

SQl Fiddle.

SQl小提琴。

In your latest attempt the resr:/CWTTravelReservation:Value is just malformed. In the working-but-null you're only looking at the top-level TravelReservationRequest, which doesn't have child nodes called vendorCode etc., hence the null results. And you're looking for nodes, not attributes, with that path syntax anyway. (You also have a discrepancy between atravelcompany.com and acompany.com but I suspect you introduced that forming the question and hiding the real values).

在您的最新尝试中,resr:/ CWTTravelReservation:值只是格式错误。在work-but-null中,您只查看*的TravelReservationRequest,它没有名为vendorCode等的子节点,因此结果为null。无论如何,你正在寻找具有该路径语法的节点,而不是属性。 (你也有atravelcompany.com和acompany.com之间的差异,但我怀疑你介绍了形成问题并隐藏真实价值)。

#1


0  

You need to give the full path down to your Vendor nodes, including specifying the relevant namespace at each level:

您需要向供应商节点提供完整路径,包括在每个级别指定相关的命名空间:

/trans:TravelReservationRequest/resr:CWTTravelReservation/resr:Reservation/resr:VendorList/resr:Vendor

And you are accessing the attributes of those nodes so you need to use the @ syntax to see them in your PATH:

并且您正在访问这些节点的属性,因此您需要使用@语法在PATH中查看它们:

select t.*
from mdf_audit a
cross join xmltable(
  xmlnamespaces('http://schema.atravelcompany.com/V1' as "v1"
      ,'http://schema.atravelcompany.com/TravelReservation' as "resr"
      ,'http://schema.atravelcompany.com/TravelReservationTransaction' as "trans"
      ,'http://www.w3.org/2001/XMLSchema' as "xsd"
      ,'http://www.w3.org/2001/XMLSchema-instance' as "xsi")
    ,'/trans:TravelReservationRequest/resr:CWTTravelReservation/resr:Reservation/resr:VendorList/resr:Vendor'
  passing a.mdf_xml_text
  columns 
    "VENDORCODE" VARCHAR2(255) PATH '@vendorCode',
    "VENDORCLASSCODE" VARCHAR2(255) PATH '@vendorClassCode',
    "VENDORCLASSIFICATION" VARCHAR2(255) PATH '@vendorClassification',
    "VENDORNAME" VARCHAR2(255) PATH '@vendorName'
) t;

VENDORCODE VENDORCLAS VENDORCLAS VENDORNAME                   
---------- ---------- ---------- ------------------------------
AD         1          Air                                      
RT         3          Hotel      SOME HOTEL SOMEWHERE          

SQl Fiddle.

SQl小提琴。

In your latest attempt the resr:/CWTTravelReservation:Value is just malformed. In the working-but-null you're only looking at the top-level TravelReservationRequest, which doesn't have child nodes called vendorCode etc., hence the null results. And you're looking for nodes, not attributes, with that path syntax anyway. (You also have a discrepancy between atravelcompany.com and acompany.com but I suspect you introduced that forming the question and hiding the real values).

在您的最新尝试中,resr:/ CWTTravelReservation:值只是格式错误。在work-but-null中,您只查看*的TravelReservationRequest,它没有名为vendorCode等的子节点,因此结果为null。无论如何,你正在寻找具有该路径语法的节点,而不是属性。 (你也有atravelcompany.com和acompany.com之间的差异,但我怀疑你介绍了形成问题并隐藏真实价值)。