使用 Oracle SQL 解析 XML 内部的 JSON
Parsing JSON inside of XML with Oracle SQL
我有从 Oracle 原子提要返回的 xml,我将它暂时存储在名为 'ATOM' 的 apex_collection 中。
XML 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>atomserver:newhire:feed</id>
<title type="text">New Hire</title>
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire" rel="self" />
<updated>2013-12-16T18:17:56.108Z</updated>
<entry>
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire/EMP300000133185858" rel="edit" />
<id>atomservlet:newhire:EMP300000133185858</id>
<title type="text">Ford, Laura Hired</title>
<updated>2016-10-20T12:45:03.000Z</updated>
<author>
<name>BETTY.ANDERSON</name>
</author>
<summary type="text">Employee Period of Service Created</summary>
<published>2016-10-20T12:45:03.000Z</published>
<link href="https://ucf5-fap0357-hcm.oracledemos.com:443/hcmCoreApi/resources/latest/emps?q=PersonId=300000133184715&effectiveDate=2015-01-01" rel="related" />
<content type="text">{ "Context" : [ { "PrimaryPhoneNumber" : "44 1 781895862", "PersonId" : "300000133184715", "PersonName" : "Ford, Laura", "EffectiveStartDate" : "2015-01-01", "EffectiveDate" : "2015-01-01", "WorkerType" : "EMP", "PeriodType" : "E", "PersonNumber" : "3686", "WorkEmail" : "laura.ford@noreply.com" } ] }</content>
</entry>
</feed>
到目前为止,我的 SQL 查询是这样的,并且工作正常:
with t as (select clob001 from apex_collections where collection_name = 'ATOM')
SELECT title, summary, content FROM t,
XMLTable( XMLNamespaces(
'http://www.w3.org/2005/Atom' AS "ns0"
), 'ns0:feed/ns0:entry'
PASSING XMLTYPE.createXML(t.CLOB001)
COLUMNS title VARCHAR2(4000) path 'ns0:title' ,
summary VARCHAR2(240) path 'ns0:summary',
content VARCHAR2(4000) path 'ns0:content');
但我需要获取内容标签内的信息,它看起来好像是 JSON。我知道如何用 SQL 解析 JSON 但我不知道如何解析 JSON 如果它在 XML 文档中。
Oracle 的 JSON 点符号(例如 content.Context[0].WorkEmail
)is pretty strict。它在这里不起作用,因为 (a) 您没有将 table 别名放在开头(您的 XMLTable 甚至没有 table 别名),但 (b) 更重要的是,您的 content
列的类型为 VARCHAR2(4000)
,而不是 JSON
,并且它没有 IS_JSON
检查约束。我认为您甚至不能向 XMLTable 添加约束。所有这些都是使用点符号所必需的。
根据您的评论,更简单的选择是使用 JSON 函数,例如
with t as (select clob001 from apex_collections where collection_name = 'ATOM')
SELECT title, summary,
json_value(content, '$.Context[0].WorkEmail') as work_email
FROM t,
XMLTable( XMLNamespaces(
'http://www.w3.org/2005/Atom' AS "ns0"
), 'ns0:feed/ns0:entry'
PASSING XMLTYPE.createXML(t.CLOB001)
COLUMNS title VARCHAR2(4000) path 'ns0:title' ,
summary VARCHAR2(240) path 'ns0:summary',
content VARCHAR2(4000) path 'ns0:content');
我有从 Oracle 原子提要返回的 xml,我将它暂时存储在名为 'ATOM' 的 apex_collection 中。
XML 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>atomserver:newhire:feed</id>
<title type="text">New Hire</title>
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire" rel="self" />
<updated>2013-12-16T18:17:56.108Z</updated>
<entry>
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire/EMP300000133185858" rel="edit" />
<id>atomservlet:newhire:EMP300000133185858</id>
<title type="text">Ford, Laura Hired</title>
<updated>2016-10-20T12:45:03.000Z</updated>
<author>
<name>BETTY.ANDERSON</name>
</author>
<summary type="text">Employee Period of Service Created</summary>
<published>2016-10-20T12:45:03.000Z</published>
<link href="https://ucf5-fap0357-hcm.oracledemos.com:443/hcmCoreApi/resources/latest/emps?q=PersonId=300000133184715&effectiveDate=2015-01-01" rel="related" />
<content type="text">{ "Context" : [ { "PrimaryPhoneNumber" : "44 1 781895862", "PersonId" : "300000133184715", "PersonName" : "Ford, Laura", "EffectiveStartDate" : "2015-01-01", "EffectiveDate" : "2015-01-01", "WorkerType" : "EMP", "PeriodType" : "E", "PersonNumber" : "3686", "WorkEmail" : "laura.ford@noreply.com" } ] }</content>
</entry>
</feed>
到目前为止,我的 SQL 查询是这样的,并且工作正常:
with t as (select clob001 from apex_collections where collection_name = 'ATOM')
SELECT title, summary, content FROM t,
XMLTable( XMLNamespaces(
'http://www.w3.org/2005/Atom' AS "ns0"
), 'ns0:feed/ns0:entry'
PASSING XMLTYPE.createXML(t.CLOB001)
COLUMNS title VARCHAR2(4000) path 'ns0:title' ,
summary VARCHAR2(240) path 'ns0:summary',
content VARCHAR2(4000) path 'ns0:content');
但我需要获取内容标签内的信息,它看起来好像是 JSON。我知道如何用 SQL 解析 JSON 但我不知道如何解析 JSON 如果它在 XML 文档中。
Oracle 的 JSON 点符号(例如 content.Context[0].WorkEmail
)is pretty strict。它在这里不起作用,因为 (a) 您没有将 table 别名放在开头(您的 XMLTable 甚至没有 table 别名),但 (b) 更重要的是,您的 content
列的类型为 VARCHAR2(4000)
,而不是 JSON
,并且它没有 IS_JSON
检查约束。我认为您甚至不能向 XMLTable 添加约束。所有这些都是使用点符号所必需的。
根据您的评论,更简单的选择是使用 JSON 函数,例如
with t as (select clob001 from apex_collections where collection_name = 'ATOM')
SELECT title, summary,
json_value(content, '$.Context[0].WorkEmail') as work_email
FROM t,
XMLTable( XMLNamespaces(
'http://www.w3.org/2005/Atom' AS "ns0"
), 'ns0:feed/ns0:entry'
PASSING XMLTYPE.createXML(t.CLOB001)
COLUMNS title VARCHAR2(4000) path 'ns0:title' ,
summary VARCHAR2(240) path 'ns0:summary',
content VARCHAR2(4000) path 'ns0:content');