使用Oracle读取xml

时间:2015-04-20 12:22:18

标签: oracle

我们必须使用pl / sql读取xml。 xml的前几行粘贴在下面。在xml中,对于一个节点,有一个设备。对于一台设备,有多个机柜。对于一个机柜,有多个Subrack&对于一个子架,有多个板。

我们开发了以下查询来解析。

步骤1:

create table emp_xml of xmltype xmltype store as securefile binary xml;

步骤2:

insert into emp_xml values (xmltype(bfilename('XML_DIR','ahm_2015_04_01_172428.xml'), nls_charset_id('AL32UTF8') ));

步骤-3:

select * from emp_xml;

步骤-4:

select x.*
  from emp_xml t,
       xmltable(xmlnamespaces(default     'http://www.ericsson.com/axe/export/hw'(http :/ /
                                                                            www.ericsson.com / axe /
                                                                             export /
                                                                            hw%27)),
            '/NetworkInventory/Node' passing t.object_value columns
            SiteName varchar2(10) path '@Name',
            SiteType varchar2(10) path '@Type',
            BuildingPractice varchar2(10) path
            '//Equipment/@BuildingPractice') x;

此查询工作正常。但是当我尝试获取内阁或子架细节时,我们会收到以下错误。

ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton    sequence - got     multi-item sequence
ORA-06512: at line 33
19279. 00000 -  "XQuery dynamic type mismatch: expected singleton sequence -   got multi-    item sequence" 
*Cause:    The XQuery sequence passed in had more than one item.
*Action:   Correct the XQuery expression to return a single item sequence.

下面给出了几行XML。

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<NetworkInventory xmlns="http://www.ericsson.com/axe/export/hw" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ericsson.com/axe/export/hw  file:/opt/ericsson/nms_smo_srv/etc/export.xsd">
    <Description>AXE HARDWARE INVENTORY DATA</Description>
    <ExportDateTime Date="2015-04-01" Time="17:24:28"/>
    <Node AdjustDate="2015-03-21" FunctionType=" " Name="BSC20" Site=" " Type="AXE" UserLabel="">
        <Equipment BuildingPractice="BYB501">
            <Cabinet Position="CabNumber=1">
                <Subrack Name="FAN-1" Position="X=3,Y=2" Type="CP">
                    <Board Name=" " SlotPosition="255" Type="CP">
                        <ProductData FirstOperationDate="2013-11-20" LastChangedDate="2013-11-20" ManufacturedDate=" " ProductName=" " ProductNumber=" " ProductRevision=" " SerialNumber=" " Supplier="Ericsson AB"/>
                    </Board>
                    <Board Name=" " SlotPosition="255" Type="CP">
                        <ProductData FirstOperationDate="2013-11-20" LastChangedDate="2013-11-20" ManufacturedDate=" " ProductName=" " ProductNumber=" " ProductRevision=" " SerialNumber=" " Supplier="Ericsson AB"/>
                    </Board>
                </Subrack>

2 个答案:

答案 0 :(得分:0)

因为你的橱柜没有设置一个迭代 如果要以关系格式显示重复组,则必须提取主XQuery表达式中的项目序列 然后将每个项目传递给COLUMNS子句,以进一步粉碎成列。

答案 1 :(得分:0)

您正在尝试扩展一种类似于嵌套表的构造。您的设备节点可以有多个机柜,因此要从您需要的那些提取详细信息到第二个XMLTable:

select x.SiteName, x.SiteType, x.BuildingPractice, y.Position
from emp_xml t
cross join xmltable(
  xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
    '/NetworkInventory/Node' passing t.object_value columns
      SiteName varchar2(10) path '@Name',
      SiteType varchar2(10) path '@Type',
      BuildingPractice varchar2(10) path 'Equipment/@BuildingPractice',
      Equipment XMLType path 'Equipment'
) x
cross join xmltable(
  xmlnamespaces(default 'http://www.ericsson.com/axe/export/hw'),
    '//Cabinet' passing x.Equipment columns
      Position varchar2(15) path '@Position'
) y;

SITENAME   SITETYPE   BUILDINGPRACTICE POSITION      
---------- ---------- ---------------- ---------------
BSC20      AXE        BYB501           CabNumber=1    

要获取SubRack数据,您需要将其传递给XMLTable的第三级等。