使用 XML 类型操作在 ORACLE 11 G 中提取 XML

EXTRACT XML IN ORACLE 11 G USING XMLTYPE OPERATIONS

几个月以来我一直在研究 Oracle 11g,所以没有足够的经验,感谢您帮助我解决这个问题。我正在尝试读取一个大 XML 文件并仅获取其中的 2 个值,问题是我读取的文档有点大。那么让我向您解释一下我在做什么:

创建一个 table 用于存储 xml:

CREATE TABLE xml_table OF XMLType;

然后我阅读了将其放入table的文档:

PROCEDURE prc_insertXmlFile( dir VARCHAR2, file VARCHAR2) IS
 BEGIN
   INSERT INTO xml_table
  VALUES (XMLType(bfilename(dir, file),
          nls_charset_id('AL32UTF8')));
  COMMIT;
END;

现在,这是文档的摘录(插入没问题):

<?xml version="1.0" encoding="UTF-8"?>
<Report xsi:schemaLocation="VehicleTripSummary http://lol/someServer?%2lol4%2FVehicleTripSummary&amp;rs%3ACommand=Render&amp;rs%3AFormat=XML&amp;rs%3ASessionID=prfwnw554uqweiz0c45eviftfu5&amp;rc%3ASchema=True" Name="VehicleTripSummary" txtReportTitle="Vista Viaje por Vehículo (Resumen)" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="VehicleTripSummary">
  <table1 textbox8="Publicado por:" User_Description="Operaciones qwwwq" textbox5="Grupo de  Vehículo:" VehicleGroup_Description="Grupo de Vehiculos-qweqe qweqe." textbox16="Período:" LocalTimestampRange="2015-02-03 00:00 .. 2015-02-03 23:59"/>
  <tblTripDetails Distance_Abbreviation="Distance(Km)" textbox17="Duración" textbox25="Ultima Conocida Odómetro" Textbox27="Ultima Conocida Hs. Motor" textbox47="Descripción de Vehículo" textbox1="Registro" textbox2="Núm. de Viajes" textbox3="Total" textbox11="Promedio" textbox19="Total" textbox14="Promedio" textbox18="Núm. de Choferes" textbox20="Fecha" textbox24="Lectura" Textbox28="Fecha" Textbox21="Lectura" textbox42="Totales:" textbox44="1987" textbox45="7040" textbox48="287:11:06">
    <Detail_Collection>
      <Detail Vehicle_Description_1="A144 CL149627" Vehicle_Registration="CL149627" NoOfTrips="0" TotalDistance="0" AverageDistance="0" TotalDuration_Description="00:00:00" AverageDuration_Description="00:00:00" NoOfDrivers="0" LastKnownOdometer="2015-01-30 10:02:58" LastKnownOdometerLocalTimestamp="46" Textbox29="--" Textbox31="--"/>
      <Detail Vehicle_Description_1="A38 CL124335 " Vehicle_Registration="CL124335" NoOfTrips="3" TotalDistance="0" AverageDistance="0" TotalDuration_Description="00:08:03" AverageDuration_Description="00:02:41" NoOfDrivers="0" LastKnownOdometer="2015-02-04 16:13:35" LastKnownOdometerLocalTimestamp="283252" Textbox29="--" Textbox31="--"/>
.
.
.
   </Detail_Collection>
  </tblTripDetails>
</Report>

现在,我想要的是读取这个以获得值 Datail/Vehicle_Registration 和 Datail/TotalDistance。 我这样做:

SELECT extractValue(OBJECT_VALUE,'/Report/tblTripDetail/Detail_Collection/Detail[1]//Vehicle_Registration') Detalles
FROM xml_table; 

但是我得不到我想要的数据。

提前致谢。

检查一下:XMLTYPE OPERATIONS

/Report/tblTripDetail/Detail_Collection/Detail[1]//@Vehicle_Registration

我得到了这个解决方案,希望对某人有所帮助! X) 问题出在 xml 名称 space 上,我们必须通过这样做来删除它: THE SOLUTION

然后我用这段代码访问了我需要的数据:

PROCEDURE PRC_PRINCIPAL IS
  y xmltype;
  x XMLType := XMLType(bfilename('PRUEBASTALLERXML', 'VehicleTripSummary.xml'),
          nls_charset_id('AL32UTF8'));
BEGIN
  SELECT XMLTRANSFORM(x, x.stylesheet)
  INTO Y
  FROM XMLSTYLESHEETS x
  WHERE x.CODE = 'REMNAM';
  FOR r IN (
    SELECT ExtractValue(Value(p),'/Detail/@Vehicle_Registration') as name


    FROM   TABLE(XMLSequence(Extract(y,'/Report/tblTripDetails/Detail_Collection//Detail'))) p
    ) LOOP
   htp.p(r.name);

  END LOOP;