【问题标题】:如何将 XML clob 提取到行 Oracle DB? [关闭]
【发布时间】:2022-01-23 08:12:49
【问题描述】:

我正在尝试使用 SQL 从以下 XML 中提取文档元素:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definition>
  <bpmn:documentation id="Task_Documentation">
   doc1
  </bpmn:documentation>
  <bpmn:documentation id="Task_2_Documentation">
   doc2
  </bpmn:documentation>
  <bpmn:documentation id="Task_3_Documentation">
   doc3
  </bpmn:documentation>
</bpmn:definitions>

我试过了:

SELECT
   XMLTYPE(t.diag).EXTRACT('/bpmn:definition/bpmn:documentation/text()').getStringVal()
FROM
   table_name t;

但我得到了:

ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00601: Invalid token in: '/bpmn:definition/bpmn:documentation/text()'

【问题讨论】:

  • 您的问题不清楚有几个原因:1)这不是有效的xml。你的开始和结束元素不一样,2)没有命名空间前缀定义,3)你要提取元素文本,比如“doc1”还是id属性? 4)。提取(选择)与插入表是分开的。请编辑问题并进行所有适当的更正,以便我们提供适当的帮助。谢谢
  • 我想从 CLOB 中选择 XML,我添加了示例 XML 可能不正确。
  • 你的意思是select XMLTYPE(your_clob) from...
  • 是的,我想从 XML 中选择 doc1、doc2、doc3
  • 我添加了我尝试过的查询并得到了错误

标签: sql xml oracle


【解决方案1】:

您的 XML 示例仍然不正确,但这是一个更正、有效的示例,其中命名空间已修复。根据需要更改:

with table_name as ( select '
<bpmn:definitions xmlns:bpmn="http://test.com">
  <bpmn:documentation id="Task_Documentation">
   doc1
  </bpmn:documentation>
  <bpmn:documentation id="Task_2_Documentation">
   doc2
  </bpmn:documentation>
  <bpmn:documentation id="Task_3_Documentation">
   doc3
  </bpmn:documentation>
</bpmn:definitions>' test_data from dual )
SELECT b.doc
from table_name t,
       XMLTABLE(xmlnamespaces('http://test.com' as "bpmn"),
       '/bpmn:definitions/bpmn:documentation' PASSING xmltype(t.test_data)
                       COLUMNS doc VARCHAR2(30) PATH './text()') b

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-21
    • 2015-11-21
    • 2016-05-27
    • 2019-02-24
    • 2019-07-05
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    相关资源
    最近更新 更多