如何从Oracle表中检索特定的XML节点?

时间:2010-10-05 11:22:03

标签: xml oracle

我有一个oracle表,它有一个以CLOB格式存储XML的列。 XML具有以下语法:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
 <BaseXML Version="2009.4">
 <InvoiceCanvasDetails>
     <Grouping>
       <ParentGroup Name=”Parent group 1” ID=”100”>
        <ParentGroupLineItems>
         <Item ID="461616" Name=”Item 1”>
           <Papers Quantity=10000>
                          <Paper Name="UNCOATED GROUNDWOOD SCA+ (25X38)"    
                                      ID="126287" Weight="1268" Type=”A4” /> 
                          <Paper Name="COATED GROUNDWOOD SCA+ (25X38)"    
                                      ID="126288" Weight="1290" Type=”A4” />
                     </Papers>
              </Item>
       </ParentGroupLineItems>
    </ParentGroup>
  </Grouping>
 </InvoiceCanvasDetails>
</BaseXML>

现在,我想只检索与每个项目对应的纸张信息。即,给定项目ID,使用查询检索与其关联的所有论文。请指导我这是最好的方法。

2 个答案:

答案 0 :(得分:6)

您可以结合使用extractextractvalue

SQL> SELECT extractvalue(column_value, 'Paper/@Name') NAME,
  2         extractvalue(column_value, 'Paper/@ID') ID,
  3         extractvalue(column_value, 'Paper/@Weight') Weight,
  4         extractvalue(column_value, 'Paper/@Type') TYPE
  5    FROM TABLE (SELECT xmlsequence(XMLTYPE(a).extract('BaseXML/' ||
  6                                             'InvoiceCanvasDetails/' ||
  7                                             'Grouping/ParentGroup/' ||
  8                                             'ParentGroupLineItems/' ||
  9                                             'Item/Papers/Paper'))
 10                   FROM t);

NAME                                     ID         WEIGHT     TYPE
---------------------------------------- ---------- ---------- -----
UNCOATED GROUNDWOOD SCA+ (25X38)         126287     1268       A4
COATED GROUNDWOOD SCA+ (25X38)           126288     1290       A4

如果您要查找特定ID,可以直接在extract函数中过滤数据:

SQL> SELECT extract(XMLTYPE(a),
  2                  'BaseXML/InvoiceCanvasDetails/Grouping/' ||
  3                  'ParentGroup/ParentGroupLineItems/' ||
  4                  'Item/Papers/Paper[@ID="126287"]') ext
  5    FROM t;

EXT
--------------------------------------------------------------------------------
<Paper Name="UNCOATED GROUNDWOOD SCA+ (25X38)" ID="126287" Weight="1268" Type="A
4"/>

答案 1 :(得分:0)

看一下在Oracle中使用XPATH expressions