Oracle - 如何使用xmltable解析xml

时间:2014-06-24 09:31:28

标签: xml oracle soap xml-namespaces xmltable

xml看起来像这样:

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
   <soap12:Body>
     <BlockUpload xmlns="http://mftsolutions.com/">
       <data><Block xmlns="http://mftsolutions.com/BlockUploader/Block"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://mftsolutions.com/BlockUploader/Block BlockSchema.xsd">
     <Header>
         <Identifier>ID10567</Identifier>
     </Header>
 </Block></data>
     </BlockUpload>
   </soap12:Body>
 </soap12:Envelope>

我想获取元素标识符(ID10567)的值。

我尝试过这样的一些查询,但没有结果。可能我在xmlNamespaces声明中缺少一些东西:

select h_id
from test_xml, 
  xmltable(xmlNamespaces ( default 'http://mftsolutions.com/BlockUploader/Block' 
                          ,'http://mftsolutions.com/' as "mft"
                          ,'http://www.w3.org/2001/XMLSchema-instance' as "xsi"
                          ,'http://www.w3.org/2001/XMLSchema' as "xsd"
                          ,'http://www.w3.org/2003/05/soap-envelope' as "soap12"),
            '//soap12:Envelope/soap12:Body/BlockUpload/data/Block/Header'
                   PASSING XMLTYPE(xml)
                   COLUMNS
                     h_id VARCHAR2(1000) PATH 'Identifier')

我错过了什么?

此查询的示例表:

create table test_xml (xml clob);

insert into test_xml values ('<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
   <soap12:Body>
     <BlockUpload xmlns="http://mftsolutions.com/">
       <data><Block xmlns="http://mftsolutions.com/BlockUploader/Block"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://mftsolutions.com/BlockUploader/Block BlockSchema.xsd">
     <Header>
         <Identifier>ID10567</Identifier>
     </Header>
 </Block></data>
     </BlockUpload>
   </soap12:Body>
 </soap12:Envelope>');

1 个答案:

答案 0 :(得分:0)

XML文档中的<BlockUpload>节点属于名称空间"http://mftsolutions.com/"。在Xpath表达式中错过了对此命名空间的引用。

此外,文档中的<data>节点隐式属于同一名称空间,因此查询的正确XPath必须如下所示:

'//soap12:Envelope/soap12:Body/mft:BlockUpload/mft:data/Block/Header'