从CLOB字段中的重复元素中提取少量元素:

时间:2015-08-17 03:38:24

标签: oracle clob xmltype xmltable

我需要从CLOB字段中的重复元素中提取几个元素:

我正在处理的输入XML字段是:

<OneOrder>
   <OrderType>ABC</OrderType>
    <OrderSubType>CBA</OrderSubType>
<Order>
 <LineNumber>1</LineNumber>
        <LineItem>
            <Quantity>1</Quantity>
                <Item>
                    <Name>NAme-1<Name>           
                </Item>
        </LineItem>
</Order>
<Order>
<LineNumber>2</LineNumber>
        <LineItem>
            <Quantity>1</Quantity>
                <Item>
                    <Name>NAme-2</Name>              
                </Item>
        </LineItem>
</Order>
<Order>
<LineNumber>3</LineNumber>
        <LineItem>
            <Quantity>1</Quantity>
                <Item>
                    <Name>NAme-3</Name>
                </Item>
        </LineItem>
</Order>
</OneOrder>

Query used:

   select 
    x.OrderSubType, -- other x columns, but not LineItems
      y.LineNumber, z.OfferName
    from order_table t,
      xmltable('/OneOrder'
        passing XMLTYPE(t.order_data)
        columns OrderType varchar2(300) path 'OrderType',
        OrderSubType varchar2(20) path '//OrderSubType',
        Order XMLType path '//Order'
      ) x,
      xmltable('/Order'
        passing x.Order
        columns LineNumber varchar(10) path '//LineNumber'
      ) y,
       xmltable('/Order/LineItem'
        passing y.LineItem
        columns Quantity varchar(10) path '//Quantity'
      ) a,
      xmltable('/Item'
        passing a.Item
        columns OfferName varchar(10) path '//Name'
      ) z;

获取错误:ORA-00904:&#34; Y&#34;。&#34; LINEITEM&#34;:无效的标识符 00904. 00000 - &#34;%s:无效的标识符&#34;

请建议。

1 个答案:

答案 0 :(得分:0)

此查询对我有用:

WITH order_table AS (SELECT XMLTYPE('
<OneOrder>
  <OrderType>ABC</OrderType>
  <OrderSubType>CBA</OrderSubType>
  <Order>
    <LineNumber>1</LineNumber>
    <LineItem>
      <Quantity>1</Quantity>
      <Item>
        <Name>NAme-1</Name>
      </Item>
    </LineItem>
  </Order>
  <Order>
    <LineNumber>2</LineNumber>
    <LineItem>
      <Quantity>1</Quantity>
      <Item>
        <Name>NAme-2</Name>              
      </Item>
    </LineItem>
  </Order>
  <Order>
    <LineNumber>3</LineNumber>
    <LineItem>
      <Quantity>1</Quantity>
      <Item>
        <Name>NAme-3</Name>
      </Item>
    </LineItem>
  </Order>
</OneOrder>') order_data FROM dual)
SELECT x.OrderSubType, 
       y.LineNumber, 
       y.OfferName
FROM order_table t,
     XMLTABLE('/OneOrder'
       PASSING t.order_data
       COLUMNS
          OrderType VARCHAR2(300) PATH 'OrderType',
          OrderSubType VARCHAR2(20) PATH 'OrderSubType'
        ) x,
      XMLTABLE('/OneOrder/Order'
        PASSING t.order_data
        COLUMNS 
          LineNumber VARCHAR2(10) PATH 'LineNumber',
          OfferName VARCHAR2(10) PATH 'LineItem/Item/Name'
        ) y