0

我想在 Oracle 中使用 xmltable 进行查询。
一切正常,但 xml 节点“article_title”有多个 (n) 结果。对于每一行,"<string>Article Name1</string><string>Article Name 2</string>...都返回结果。但我希望每个文章名称都作为单行返回。
我怎么能意识到这一点?

SELECT 
  X.* 
FROM 
  myTable C, 
                          xmltable (
                           '$cust//member' PASSING C.STAT_XML as "cust"
                           COLUMNS 
                           name VARCHAR(25) PATH '/member/name',
                           article_title XMLTYPE PATH '//string/text()'
                          ) as X

WHERE X.name = 'articles';
4

2 回答 2

1

我也有这个问题。我有一个 XML,它应该将货物数据从我们的仓库管理系统发送回我们的订单管理系统,它有各种不同的东西,有多个。整个 XML 消息有一个 ShipConfirmHeader 部分,因此很容易取出。我遇到麻烦的地方是它有一个 ShipConfirmDetail/Orders 部分,并且可能列出任意数量的订单。在每个订单中,可以有任意数量的订单行。我可以将 ShipConfirmHeader 和 ShipConfirmDetail/Orders 拉在一起,或者我可以将 ShipConfirmHeader 和 ShipConfirmDetail/Orders/OrderLineItem 拉在一起,但是当我尝试将 Orders 与 OrderLineItem 拉在一起时,我看不到加入这些,所以我最终得到一个笛卡尔积。

我在下面包含了我的 XML 示例。在这个示例中,只有一个 Order、一个 OrderLine 和一个 Carton(在 XML 中称为 LPN),因为我已经删除了所有其他内容(原始 XML 超过 4000 行长)。

从 ShipConfirmHeader 中提取内容相对容易,如下所示:

xmltable('/tXML/Message/ShipConfirm/ShipConfirmSummary/ShipConfirmHeaderInfo/'
        passing xmltype(msg_xml.full_xml)
        columns
          invoice_batch       varchar2(20)      path 'InvcBatchNbr'
        ) sc_hdr

但是当我想包含任何倍数时,它会给我带来问题。我尝试了各种各样的事情:

-- This gives the error "ORA-22950: cannot ORDER objects without MAP or ORDER method"
xmltable('/tXML/Message/ShipConfirm'
  passing xmltype(msg_xml.full_xml)
  columns
    invoice_batch       varchar2(20)      path 'ShipConfirmSummary/ShipConfirmHeaderInfo/InvcBatchNbr',
    order_dtl           xmltype           path 'ShipConfirmDetails/Orders'
  ) sc_hdr
  
  
-- This doesn't like the "../" in the XPATH
xmltable('/tXML/Message/ShipConfirm/ShipConfirmDetails/Orders/OrderLineItem'
  passing xmltype(msg_xml.full_xml)
  columns
    invoice_batch       varchar2(20)      path '../../../ShipConfirmSummary/ShipConfirmHeaderInfo/InvcBatchNbr',
    order_id            varchar2(20)      path '../TcOrderId',
    order_line_id       varchar2(20)      path 'TcOrderLineId',
    item_name           varchar2(20)      path 'ItemName'
  ) sc_hdr

-- This gives a cartesian product.
xmltable('/tXML/Message/ShipConfirm/ShipConfirmSummary/ShipConfirmHeaderInfo'
  passing xmltype(msg_xml.full_xml)
  columns
    invoice_batch       varchar2(20)      path 'InvcBatchNbr'
  ) sc_hdr,
xmltable('/tXML/Message/ShipConfirm/ShipConfirmDetails/Orders'
  passing xmltype(msg_xml.full_xml)
  columns
    order_id            varchar2(20)      path 'TcOrderId'
  ) sc_ord_hdr,
xmltable('/tXML/Message/ShipConfirm/ShipConfirmDetails/Orders/OrderLineItem'
  passing xmltype(msg_xml.full_xml)
  columns
    order_line_id       varchar2(20)      path 'TcOrderLineId',
    item_name           varchar2(20)      path 'ItemName'
  ) sc_ord_dtl

这是示例 XML:

<?xml version="1.0" encoding="UTF-8"?>
<tXML>
  <Header>
    <Source>warehouse management system</Source>
    <Action_Type></Action_Type>
    <Sequence_Number></Sequence_Number>
    <Batch_ID></Batch_ID>
    <Reference_ID></Reference_ID>
    <User_ID>CRONUSER</User_ID>
    <Password></Password>
    <Message_Type>ShipConfirm</Message_Type>
    <Company_ID>1</Company_ID>
    <Msg_Locale>English (United States)</Msg_Locale>
    <Msg_Time_Zone>America/Denver</Msg_Time_Zone>
    <Version>2018</Version>
  </Header>
  <Message>
    <ShipConfirm>
      <ShipConfirmSummary>
        <CompanyName>Blah</CompanyName>
        <FacilityName>Blah</FacilityName>
        <ShipConfirmHeaderInfo>
          <InvcBatchNbr>123456</InvcBatchNbr>
          <LastInvcDttm>5/27/21 05:45</LastInvcDttm>
          <ShippedDttm>5/27/21 05:45</ShippedDttm>
          <DateCreated>5/27/21 05:45</DateCreated>
          <StoreNbr></StoreNbr>
          <ShipVia>ST</ShipVia>
          <SchedDeliveryDate></SchedDeliveryDate>
          <ProNbr></ProNbr>
          <AppointmentNbr></AppointmentNbr>
          <ManifestNbr></ManifestNbr>
          <SealNbr></SealNbr>
          <AppointmentDate></AppointmentDate>
          <PartialShipConfirmStatus>5</PartialShipConfirmStatus>
          <PreBillStatus>0</PreBillStatus>
          <ApptMadeByID></ApptMadeByID>
          <BillOfLading></BillOfLading>
          <CancelQuantity>0.0</CancelQuantity>
          <NbrOfLpns>26</NbrOfLpns>
          <NbrOfPlts>0</NbrOfPlts>
          <NbrOfOrders>26</NbrOfOrders>
          <TotalWt>61.72</TotalWt>
          <UserID>USER</UserID>
        </ShipConfirmHeaderInfo>
      </ShipConfirmSummary>
      <ShipConfirmDetails>
        <Orders>
          <BatchCtrlNbr>123456</BatchCtrlNbr>
          <DistributionShipVia>ST</DistributionShipVia>
          <DoType>Customer Order</DoType>
          <DsgShipVia>ST</DsgShipVia>
          <OriginalShipVia>ST</OriginalShipVia>
          <IncotermLocAvaTimeZoneId>America/New_York</IncotermLocAvaTimeZoneId>
          <InvcBatchNbr>123456</InvcBatchNbr>
          <IsBackOrdered>1</IsBackOrdered>
          <MajorOrderCtrlNbr></MajorOrderCtrlNbr>
          <OrderType>ECOMM_ORDER</OrderType>
          <ShipDate>5/27/21 05:45</ShipDate>
          <OrderStatus>Unplanned</OrderStatus>
          <DoStatus>Shipped</DoStatus>
          <TcCompanyId>1</TcCompanyId>
          <TcOrderId>MYORDERID</TcOrderId>
          <TotalNbrOfLpn>1</TotalNbrOfLpn>
          <TotalNbrOfPlt>0</TotalNbrOfPlt>
          <TotalNbrOfUnits>1</TotalNbrOfUnits>
          <LineHaulShipVia>ST</LineHaulShipVia>
          <PartialShipConfirmStatus>5</PartialShipConfirmStatus>
          <PreBillStatus>0</PreBillStatus>
          <OrderBillToInfo>
            <BillToAddress1>Snip</BillToAddress1>
            <BillToAddress2></BillToAddress2>
            <BillToAddress3></BillToAddress3>
            <BillToCity>Snip</BillToCity>
            <BillToContact></BillToContact>
            <BillToContactName></BillToContactName>
            <BillToCountryCode>CA</BillToCountryCode>
            <BillToCounty></BillToCounty>
            <BillToFacilityName></BillToFacilityName>
            <BillToName>Snip</BillToName>
            <BillToPhoneNumber>Snip</BillToPhoneNumber>
            <BillToPostalCode>Snip</BillToPostalCode>
            <BillToStateProv>ON</BillToStateProv>
          </OrderBillToInfo>
          <OrderDestInfo>
            <DestAddress1>Snip</DestAddress1>
            <DestAddress2></DestAddress2>
            <DestAddress3></DestAddress3>
            <DestCity>Snip</DestCity>
            <DestContact>Snip</DestContact>
            <DestCountryCode>CA</DestCountryCode>
            <DestCounty></DestCounty>
            <DestDockDoorId>0</DestDockDoorId>
            <DestFacilityAliasId></DestFacilityAliasId>
            <DestFacilityId>0</DestFacilityId>
            <DestFacilityName></DestFacilityName>
            <DestName>Snip</DestName>
            <DestPhoneNumber>Snip</DestPhoneNumber>
            <DestPostalCode>Snip</DestPostalCode>
            <DestStateProv>ON</DestStateProv>
          </OrderDestInfo>
          <OrderOriginInfo>
            <OriginAddress1>Snip</OriginAddress1>
            <OriginAddress2></OriginAddress2>
            <OriginAddress3></OriginAddress3>
            <OriginCity>Snip</OriginCity>
            <OriginContact></OriginContact>
            <OriginCountryCode>CA</OriginCountryCode>
            <OriginFacilityAliasId>Snip</OriginFacilityAliasId>
            <OriginFacilityId>1</OriginFacilityId>
            <OriginFacilityName>Snip</OriginFacilityName>
            <OriginPhoneNumber></OriginPhoneNumber>
            <OriginPostalCode>Snip</OriginPostalCode>
            <OriginStateProv>AB</OriginStateProv>
          </OrderOriginInfo>
          <OrderInfoFields>
            <SplInstrCode1>MW</SplInstrCode1>
            <SplInstrCode2>MW</SplInstrCode2>
          </OrderInfoFields>
          <OrderLineItem>
            <InvcBatchNbr>123456</InvcBatchNbr>
            <ItemId>159331</ItemId>
            <ItemName>MYITEMNAME</ItemName>
            <LineItemId>12053970</LineItemId>
            <OrderQty>1</OrderQty>
            <OrderQtyUom>Unit</OrderQtyUom>
            <OrigItemId>159331</OrigItemId>
            <OrigItemName>MYITEMNAME</OrigItemName>
            <OrigOrderLineItemId>1</OrigOrderLineItemId>
            <OrigOrderQty>1</OrigOrderQty>
            <OrigOrderQtyUom>Unit</OrigOrderQtyUom>
            <OutptOrderLineItemId>3782033</OutptOrderLineItemId>
            <Price>15.39</Price>
            <PriceTktType></PriceTktType>
            <RetailPrice>0.0</RetailPrice>
            <ShippedQty>1</ShippedQty>
            <TcCompanyId>1</TcCompanyId>
            <TcOrderLineId>1</TcOrderLineId>
            <UnitVol>0.0744</UnitVol>
            <UnitWt>0.58</UnitWt>
            <Uom>Unit</Uom>
            <UserCanceledQty>0</UserCanceledQty>
            <OrderLineItemDefn>
              <ItemStyle>Snip</ItemStyle>
              <ItemStyleSfx>Snip</ItemStyleSfx>
            </OrderLineItemDefn>
          </OrderLineItem>
          <Lpn>
            <BillOfLadingNumber></BillOfLadingNumber>
            <CFacilityAliasId>Snip</CFacilityAliasId>
            <EstimatedWeight>0.58</EstimatedWeight>
            <FinalDestFacilityAliasId></FinalDestFacilityAliasId>
            <InvcBatchNbr>123456</InvcBatchNbr>
            <LoadedDttm></LoadedDttm>
            <ManifestNbr></ManifestNbr>
            <MasterBolNbr></MasterBolNbr>
            <NonInventoryLpnFlag>0</NonInventoryLpnFlag>
            <NonMachineable></NonMachineable>
            <OutptLpnId>730888</OutptLpnId>
            <PackerUserid>USER</PackerUserid>
            <ProcDttm>5/27/21 05:45</ProcDttm>
            <ProcStatCode>0</ProcStatCode>
            <QtyUom>Unit</QtyUom>
            <ServiceLevel></ServiceLevel>
            <ShipVia>ST</ShipVia>
            <ShippedDttm>5/27/21 05:45</ShippedDttm>
            <StaticRouteId></StaticRouteId>
            <TcCompanyId>1</TcCompanyId>
            <TcLpnId>98765</TcLpnId>
            <TcOrderId>Snip</TcOrderId>
            <TcParentLpnId></TcParentLpnId>
            <TcShipmentId></TcShipmentId>
            <TotalLpnQty>1</TotalLpnQty>
            <TrackingNbr>Snip</TrackingNbr>
            <VolumeUom>cu ft</VolumeUom>
            <Weight>0.58</Weight>
            <WeightUom>Lbs</WeightUom>
            <LoadSequence>0</LoadSequence>
            <oLPNXRefNbr></oLPNXRefNbr>
            <LpnDetail>
              <InvcBatchNbr>123456</InvcBatchNbr>
              <ItemId>159331</ItemId>
              <ItemName>MYITEMNAME</ItemName>
              <LpnDetailId>20153787</LpnDetailId>
              <OutptLpnDetailId>3689518</OutptLpnDetailId>
              <QtyUom>Unit</QtyUom>
              <SizeValue>1</SizeValue>
              <TcCompanyId>1</TcCompanyId>
              <TcLpnId>98765</TcLpnId>
              <DistroNumber></DistroNumber>
              <TcOrderLineId>1</TcOrderLineId>
              <MinorOrderNbr>Snip</MinorOrderNbr>
              <MinorPoNbr></MinorPoNbr>
            </LpnDetail>
          </Lpn>
        </Orders>
      </ShipConfirmDetails>
    </ShipConfirm>
  </Message>
</tXML>
于 2021-05-27T18:01:31.253 回答
0

尝试这样的事情:

SELECT X.*
FROM my_table C,
     xmltable('for $i in $cust//string , $j in $cust//member[./string=$i]/name return <member>{$j}{$i}</member>' 
    passing c.stat_xml AS "cust" 
    columns name varchar2(25) path '/member/name', 
    article_title xmltype path '//string') AS X
WHERE X.name = 'articles';

是一个小提琴

我假设对于每一个member你有一个name但可能有很多strings

于 2012-06-19T15:21:27.370 回答