简体   繁体   English

查询XML CLOB列以获取该列中的子XML

[英]Query XML CLOB column to get sub XML in the column

I have a CLOB column in my table which contains an XML. 我的表中有一个包含XML的CLOB列。 I want to fetch the xml after a specific tag to its ending tag ie 我想在特定标签到其结束标签后获取xml,即

Full XML in CLOB column CLOB列中的完整XML

<ParentTag>
 <Location>ABC XYZ ....</Location>
 <Person>
  <Name>Mohsin</Name>
  <Age>23</Age>
 </Person>
</ParentTag>

What I am trying to fetch is something like this: 我想要获取的是这样的:

<Person>
  <Name>Mohsin</Name>
  <Age>23</Age>
</Person>

I have tried to use dbms_lob.substr and dbms_lob.getlength but that doesnt help as the sub XML may contain the <Person> tag starting at different bytes in different scenarios. 我尝试使用dbms_lob.substrdbms_lob.getlength,但这无济于事,因为子XML可能包含<Person>标记,在不同情况下从不同字节开始。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

Don't try to parse the node out yourself with substrings. 不要尝试使用子字符串自己解析节点。 Oracle has extensive XML support built-in . Oracle内置了广泛的XML支持 You can do this with an XMLQuery : 您可以使用XMLQuery来做到这一点:

select xmlquery('/ParentTag/Person' passing xmltype(xml_clob) returning content)
  as xml_value
from your_table;

XML_VALUE                                                                      
--------------------------------------------------------------------------------
<Person><Name>Mohsin</Name><Age>23</Age></Person>

If your XML document (in the CLOB) can have multiple person nodes then you can use XMLTable instead to extract them all. 如果您的XML文档(在CLOB中)可以具有多个人节点,则可以使用XMLTable来提取它们。

And if you want it to be a formatted string matching what you've shown, rather than an XML document, you can use an XMLSerialize wrapper call: 而且,如果您希望它是与显示的内容匹配的格式化字符串,而不是XML文档,则可以使用XMLSerialize包装器调用:

select xmlserialize(content
  xmlquery('/ParentTag/Person' passing xmltype(xml_clob) returning content)
    as varchar2(100) indent size=2) as string_value
from your_table;

STRING_VALUE                                                                   
--------------------------------------------------------------------------------
<Person>                                                                        
  <Name>Mohsin</Name>                                                           
  <Age>23</Age>                                                                 
</Person>

Following up a comment, if you have anamespace you can declare that as part of the XPath : 跟进注释,如果您有命名空间,则可以将其声明为XPath的一部分

select xmlquery('declare namespace NS4 = "http://soa.comptel.com/2011/02/instantlink"; /ParentTag/NS4:Person'
  passing xmltype(prov_request) returning content) as xml_value
from your_table;

select xmlserialize(content
  xmlquery('declare namespace NS4 = "http://soa.comptel.com/2011/02/instantlink"; /ParentTag/NS4:Person'
      passing xmltype(prov_request) returning content)
    as varchar2(150) indent size=2) as string_value
from your_table;

The extracted Person node will still have that namespace information though: 但是,提取的Person节点仍将具有该名称空间信息:

STRING_VALUE                                                                   
--------------------------------------------------------------------------------
<NS4:Person xmlns:NS4="http://soa.comptel.com/2011/02/instantlink">             
  <NS4:Name>Mohsin</NS4:Name>                                                   
  <NS4:Age>23</NS4:Age>                                                         
</NS4:Person>

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

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