简体   繁体   English

使用 SQL 提取 XML 数据

[英]Extracting XML data using SQL

I would like to be able to extract specific data from a XML type using Oracle in my example for the customer named "Arshad Ali"我希望能够在我的示例中为名为“Arshad Ali”的客户使用 Oracle 从 XML 类型中提取特定数据

This is my xml data that was inserted:这是我插入的 xml 数据:

  <Customers>
    <Customer CustomerName="Arshad Ali" CustomerID="C001">
      <Orders>
        <Order OrderDate="2012-07-04T00:00:00" OrderID="10248">
          <OrderDetail Quantity="5" ProductID="10" />
          <OrderDetail Quantity="12" ProductID="11" />
          <OrderDetail Quantity="10" ProductID="42" />
        </Order>
      </Orders>
      <Address> Address line 1, 2, 3</Address>
    </Customer>
    <Customer CustomerName="Paul Henriot" CustomerID="C002">
      <Orders>
        <Order OrderDate="2011-07-04T00:00:00" OrderID="10245">
          <OrderDetail Quantity="12" ProductID="11" />
          <OrderDetail Quantity="10" ProductID="42" />
        </Order>
      </Orders>
      <Address> Address line 5, 6, 7</Address>
    </Customer>
    <Customer CustomerName="Carlos Gonzlez" CustomerID="C003">
      <Orders>
        <Order OrderDate="2012-08-16T00:00:00" OrderID="10283">
          <OrderDetail Quantity="3" ProductID="72" />
        </Order>
      </Orders>
      <Address> Address line 1, 4, 5</Address>
    </Customer>
  </Customers>
</ROOT>

using get clob I was able to extract all of the customers.使用 get clob我能够提取所有客户。

Was wondering if anyone could help me extract data for a specific customer.. tried using the following but was unsuccessful想知道是否有人可以帮助我为特定客户提取数据.. 尝试使用以下但不成功

SELECT extract(OBJECT_VALUE, '/root/Customers') "customer"
  FROM mytable2
  WHERE existsNode(OBJECT_VALUE, '/customers[CustomerName="Arshad Ali" CustomerID="C001"]')
        = 1;  

The case and exact names of the XML nodes matter: XML 节点的大小写和确切名称很重要:

SELECT extract(OBJECT_VALUE,
  '/ROOT/Customers/Customer[@CustomerName="Arshad Ali"][@CustomerID="C001"]') "customer"
FROM mytable2
WHERE existsnode (OBJECT_VALUE,
  '/ROOT/Customers/Customer[@CustomerName="Arshad Ali"][@CustomerID="C001"]') = 1

db<>fiddle 数据库<>小提琴

If you only want to search by name then only use that attribute:如果您只想按名称搜索,则仅使用该属性:

SELECT extract(OBJECT_VALUE,
  '/ROOT/Customers/Customer[@CustomerName="Arshad Ali"]') "customer"
FROM mytable2
WHERE existsnode (OBJECT_VALUE,
  '/ROOT/Customers/Customer[@CustomerName="Arshad Ali"]') = 1

But extract() and existsnode() are deprecated;但是extract()existsnode()已被弃用; use xmlquery() and xmlexists() instead:使用xmlquery()xmlexists()代替:

SELECT xmlquery('/ROOT/Customers/Customer[@CustomerName="Arshad Ali"][@CustomerID="C001"]'
  passing object_value
  returning content) "customer"
FROM mytable2
WHERE xmlexists('/ROOT/Customers/Customer[@CustomerName="Arshad Ali"][@CustomerID="C001"]'
  passing object_value)

db<>fiddle 数据库<>小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM