使用 xmltable 和 XMLNAMESPACES 提取数据

pull data using xmltable and XMLNAMESPACES

我正在尝试使用 Oracle SQL.

从下面的 XML 中提取值,作为 XML 类型存储在 xml_table
select x.*
from xml_table t  
 , xmltable( XMLNAMESPACES (
  'http://schemas.xmlsoap.org/soap/envelope/' AS "env",
  'urn:com.workday/bsvc' as "wd"
),  'for $i in //wd:Get_Worker_Costing_Allocations_Response return $i' 
 --,'env:Envelope/env:Body/wd:Get_Worker_Costing_Allocations_Response/wd:Response_Data/wd:Worker_Costing_Allocations_Data'  
     passing t.raw_xml  
     columns Employee_ID number        path 'wd:Response_Data/wd:Worker_Costing_Allocations_Data/wd:Worker_Reference/wd:ID[2]'
            ,Position_ID        varchar2(100) path 'wd:Response_Data/wd:Worker_Costing_Allocations_Data/wd:Position_Reference/wd:ID[2]'
            --,start_Date     date path   'wd:Response_Data/wd:Worker_Costing_Allocations_Data/wd:Worker_Costing_Allocation_Interval_Data/wd:Start_Date' 
   ) x 

当我试图从 Worker_Costing_Allocation_Interval_Data 获取基金 ID 和 Cost_Center_Reference_ID 等其他详细信息时。最终我必须遍历所有 Worker_Costing_Allocation_Interval_Data 个节点。

select x.*
   from xml_table t  
     , xmltable( XMLNAMESPACES (
      'http://schemas.xmlsoap.org/soap/envelope/' AS "env",
      'urn:com.workday/bsvc' as "wd"
    ),  'for $i in //wd:Worker_Costing_Allocation_Detail_Data return $i' 
     --,'env:Envelope/env:Body/wd:Get_Worker_Costing_Allocations_Response/wd:Response_Data/wd:Worker_Costing_Allocations_Data'  
         passing t.raw_xml  
         columns order_1     varchar2(20) path   'wd:Order'
                 ,fund_id     varchar2(20) path   'wd:Costing_Override_Worktag_Reference/wd:ID[@Fund_ID]' 
       ) x

预期输出:

employee_id Position_Id Start_Date  End_Date    Order   Fund_ID Project_ID  Program_ID  Cost_Center_Reference_ID    Custom_Organization_Reference_ID    Distribution_Percent
11111   P11111  7/1/2018    6/30/2050   a   FD89    PJ122201    PG11130 CC12340     0.4
11111   P11111  7/1/2018    6/30/2050   b   FD89    PJ122201    PG11130 CC1234      0.6
11111   P11111  7/1/2017    6/30/2018   a   FD30    PJ000001    PG00003 CC0565  UD000004    1
11111   P11111  1/1/2017    6/30/2017   a   FD30    PJ000001    PG00003 CC0565  UD000004    1

下面是原始 XML 插入到 xml_table:

<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
        <wd:Get_Worker_Costing_Allocations_Response xmlns:wd="urn:com.workday/bsvc" wd:version="v15">
            <wd:Request_Criteria>
                <wd:Costing_Override_Criteria>
                    <wd:Worker_Reference wd:Descriptor="First, Last">
                        <wd:ID wd:type="WID">WID1234</wd:ID>
                        <wd:ID wd:type="Employee_ID">11111</wd:ID>
                    </wd:Worker_Reference>
                </wd:Costing_Override_Criteria>
            </wd:Request_Criteria>
            <wd:Response_Group>
                <wd:Exclude_Allocation_Detail_Data>0</wd:Exclude_Allocation_Detail_Data>
            </wd:Response_Group>
            <wd:Response_Results>
                <wd:Total_Results>1</wd:Total_Results>
                <wd:Total_Pages>1</wd:Total_Pages>
                <wd:Page_Results>1</wd:Page_Results>
                <wd:Page>1</wd:Page>
            </wd:Response_Results>
            <wd:Response_Data>
                <wd:Worker_Costing_Allocations_Data>
                    <wd:Worker_Reference wd:Descriptor="First, Last">
                        <wd:ID wd:type="WID">WID1234</wd:ID>
                        <wd:ID wd:type="Employee_ID">11111</wd:ID>
                    </wd:Worker_Reference>
                    <wd:Position_Reference wd:Descriptor="P11111 Student - First, Last.">
                        <wd:ID wd:type="WID">WID2345</wd:ID>
                        <wd:ID wd:type="Position_ID">P11111</wd:ID>
                    </wd:Position_Reference>
                    <wd:Worker_Costing_Allocation_Interval_Data>
                        <wd:Costing_Override_ID>COSTING_OVERRIDE-X-ZZZZ</wd:Costing_Override_ID>
                        <wd:Start_Date>2018-07-01-07:00</wd:Start_Date>
                        <wd:End_Date>2050-06-30-07:00</wd:End_Date>
                        <wd:Worker_Costing_Allocation_Detail_Data>
                            <wd:Order>a</wd:Order>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="FD89 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Fund_ID">FD89</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PJ122201 XXXXX YYYYY">
                                <wd:ID wd:type="WID">d18edc6167911037c23bcdd377765bf3</wd:ID>
                                <wd:ID wd:type="Project_ID">PJ122201</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PG11130 XXXXX YYYYY">
                                <wd:ID wd:type="WID">d18edc61679110372db25f84888138fc</wd:ID>
                                <wd:ID wd:type="Program_ID">PG11130</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="CC12340 XXXXX YYYYY">
                                <wd:ID wd:type="WID">d18edc61679110376fe5ad0c91c3805f</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">CC12340</wd:ID>
                                <wd:ID wd:type="Cost_Center_Reference_ID">CC12340</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Distribution_Percent>0.4</wd:Distribution_Percent>
                        </wd:Worker_Costing_Allocation_Detail_Data>
                        <wd:Worker_Costing_Allocation_Detail_Data>
                            <wd:Order>b</wd:Order>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="CC1234 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">CC1234</wd:ID>
                                <wd:ID wd:type="Cost_Center_Reference_ID">CC1234</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="FD89 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Fund_ID">FD89</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PJ122201 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Project_ID">PJ122201</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PG11130 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Program_ID">PG11130</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Distribution_Percent>0.6</wd:Distribution_Percent>
                        </wd:Worker_Costing_Allocation_Detail_Data>
                    </wd:Worker_Costing_Allocation_Interval_Data>
                    <wd:Worker_Costing_Allocation_Interval_Data>
                        <wd:Costing_Override_ID>COSTING_OVERRIDE-X-YYYY</wd:Costing_Override_ID>
                        <wd:Start_Date>2017-07-01-07:00</wd:Start_Date>
                        <wd:End_Date>2018-06-30-07:00</wd:End_Date>
                        <wd:Worker_Costing_Allocation_Detail_Data>
                            <wd:Order>a</wd:Order>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="FD30 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Fund_ID">FD30</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="CC0565 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">CC0565</wd:ID>
                                <wd:ID wd:type="Cost_Center_Reference_ID">CC0565</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PG00003 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Program_ID">PG00003</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="UD000004 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">UD000004</wd:ID>
                                <wd:ID wd:type="Custom_Organization_Reference_ID">UD000004</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PJ122201 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Project_ID">PJ122201</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Distribution_Percent>1</wd:Distribution_Percent>
                        </wd:Worker_Costing_Allocation_Detail_Data>
                    </wd:Worker_Costing_Allocation_Interval_Data>
                    <wd:Worker_Costing_Allocation_Interval_Data>
                        <wd:Costing_Override_ID>COSTING_OVERRIDE-X-YYYY</wd:Costing_Override_ID>
                        <wd:Start_Date>2017-01-01-08:00</wd:Start_Date>
                        <wd:End_Date>2017-06-30-07:00</wd:End_Date>
                        <wd:Worker_Costing_Allocation_Detail_Data>
                            <wd:Order>a</wd:Order>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="FD30 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Fund_ID">FD30</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="LOA">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">LOA</wd:ID>
                                <wd:ID wd:type="Custom_Organization_Reference_ID">LOA</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="CC0565 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">CC0565</wd:ID>
                                <wd:ID wd:type="Cost_Center_Reference_ID">CC0565</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PG00003 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Program_ID">PG00003</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="UD000004 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Organization_Reference_ID">UD000004</wd:ID>
                                <wd:ID wd:type="Custom_Organization_Reference_ID">UD000004</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Costing_Override_Worktag_Reference wd:Descriptor="PJ122201 XXXXX YYYYY">
                                <wd:ID wd:type="WID">WIDXXXXX</wd:ID>
                                <wd:ID wd:type="Project_ID">PJ122201</wd:ID>
                            </wd:Costing_Override_Worktag_Reference>
                            <wd:Distribution_Percent>1</wd:Distribution_Percent>
                        </wd:Worker_Costing_Allocation_Detail_Data>
                    </wd:Worker_Costing_Allocation_Interval_Data>
                </wd:Worker_Costing_Allocations_Data>
            </wd:Response_Data>
        </wd:Get_Worker_Costing_Allocations_Response>
    </env:Body>
</env:Envelope>

如果您需要从多个级别获取数据,每个级别都有多个节点,您可以使用多个 XMLTable 结构:

select x1.employee_id, x1.position_id,
  to_date(x2.start_date, 'YYYY-MM-DD-HH24:MI') as start_date,
  x2.order_1, x2.fund_id
from xml_table t
cross join xmltable (
  xmlnamespaces ('http://schemas.xmlsoap.org/soap/envelope/' AS "env",
      'urn:com.workday/bsvc' as "wd"),
    '/env:Envelope/env:Body/wd:Get_Worker_Costing_Allocations_Response/wd:Response_Data/wd:Worker_Costing_Allocations_Data'
    passing t.raw_xml  
    columns Employee_ID number path 'wd:Worker_Reference/wd:ID[@wd:type="Employee_ID"]',
      Position_ID varchar2(100) path 'wd:Position_Reference/wd:ID[@wd:type="Position_ID"]',
      Interval_Data xmltype path 'wd:Worker_Costing_Allocation_Interval_Data'
) x1
cross join xmltable (
  xmlnamespaces ('urn:com.workday/bsvc' as "wd"),
    '/wd:Worker_Costing_Allocation_Interval_Data/wd:Worker_Costing_Allocation_Detail_Data'
      passing x1.Interval_Data
      columns start_Date varchar2(16) path './../wd:Start_Date',
      order_1 varchar2(20) path 'wd:Order',
      fund_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Fund_ID"]' 
) x2
/

EMPLOYEE_ID POSITION_ID START_DATE          ORDER_1 FUND_ID
----------- ----------- ------------------- ------- -------
      11111 P11111      2018-07-01 07:00:00 a       FD89   
      11111 P11111      2018-07-01 07:00:00 b       FD89   
      11111 P11111      2017-07-01 07:00:00 a       FD30   
      11111 P11111      2017-01-01 08:00:00 a       FD30   

第一个 XMLTable 获取员工和职位 ID,以及它自己的 XMLType,即 Worker_Costing_Allocation_Interval_Data 片段。第二个 XMLTable 传递给该片段,并提取该级别的多个值。如果需要,您可以下更多级别。

请注意,某些节点的特定实例的过滤器已更改;例如,我没有使用 [2] 来获取第二个工作人员参考 ID,而是使用 [@wd:type="Employee_ID"] 等寻找特定的属性值。并且针对基金 ID 提出了类似的构造;您的原始代码正在寻找一个名为 Fund_ID 的属性,而不是具有该类型值的属性。


可以在一个层次上完成,但是备份树的引用可能会有点混乱。这将获取您想要的所有字段:

select x.employee_id, x.position_id,
  to_date(x.start_date, 'YYYY-MM-DD-HH24:MI') as start_date,
  to_date(x.end_date, 'YYYY-MM-DD-HH24:MI') as end_date,
  x.order_1, x.fund_id, x.project_id, x.program_id, x.Cost_Center_Reference_ID,
  x.custom_organization_ref_id, x.distribution_percent
from xml_table t
cross join xmltable (
  xmlnamespaces ('http://schemas.xmlsoap.org/soap/envelope/' AS "env",
      'urn:com.workday/bsvc' as "wd"),
    '/env:Envelope/env:Body/wd:Get_Worker_Costing_Allocations_Response/wd:Response_Data/wd:Worker_Costing_Allocations_Data/wd:Worker_Costing_Allocation_Interval_Data/wd:Worker_Costing_Allocation_Detail_Data'
    passing t.raw_xml  
    columns employee_id number path './../../wd:Worker_Reference/wd:ID[@wd:type="Employee_ID"]',
      position_id varchar2(100) path './../../wd:Position_Reference/wd:ID[@wd:type="Position_ID"]',
      start_date varchar2(16) path './../wd:Start_Date',
      end_date varchar2(16) path './../wd:End_Date',
      order_1 varchar2(20) path 'wd:Order',
      fund_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Fund_ID"]',
      project_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Project_ID"]',
      program_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Program_ID"]',
      cost_center_reference_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Cost_Center_Reference_ID"]',
      custom_organization_ref_id varchar2(20) path 'wd:Costing_Override_Worktag_Reference/wd:ID[@wd:type="Custom_Organization_Reference_ID" and starts-with(text(), "UD")]/text()',
      distribution_percent number path 'wd:Distribution_Percent'
) x
/

EMPLOYEE_ID POSITION_ID START_DATE          END_DATE            ORDER_1 FUND_ID PROJECT_ID PROGRAM_ID COST_CENTE CUSTOM_ORG DISTRIBUTION_PERCENT
----------- ----------- ------------------- ------------------- ------- ------- ---------- ---------- ---------- ---------- --------------------
      11111 P11111      2018-07-01 07:00:00 2050-06-30 07:00:00 a       FD89    PJ122201   PG11130    CC12340                                0.4
      11111 P11111      2018-07-01 07:00:00 2050-06-30 07:00:00 b       FD89    PJ122201   PG11130    CC1234                                 0.6
      11111 P11111      2017-07-01 07:00:00 2018-06-30 07:00:00 a       FD30    PJ122201   PG00003    CC0565     UD000004                    1.0
      11111 P11111      2017-01-01 08:00:00 2017-06-30 07:00:00 a       FD30    PJ122201   PG00003    CC0565     UD000004                    1.0

我已经注释掉了 Custom_Organization_Reference_ID 因为有重复项 - LOA 和 UD000004 - 所以你可能不得不返回到多个 XMLTables 来处理它。

根据您的评论,这只会找到以 UD 开头的 Custom_Organization_Reference_ID