从 XML 获取数据 - XMLGet 函数使用属性名称及其在雪花中的值
Posted
技术标签:
【中文标题】从 XML 获取数据 - XMLGet 函数使用属性名称及其在雪花中的值【英文标题】:Fetch data from XML - XMLGet function using with Attribute Name and its value in Snowflake 【发布时间】:2021-11-20 23:42:24 【问题描述】:我有一个需要使用 Snowflake SQL 读取的 xml。我需要使用属性名称和 lits 值来获取数据。例如 -> id 和 "bk101" 来获取内容。
<catalog issue="spring">
<Books>
<book id="bk101">The Good Book</book>
<book id="bk102">The OK Book</book>
<book id="bk103">The NOT Ok Book</book>
<book id="bk104">All OK Book</book>
<book id="bk105">Every OK Book</book>
</Books>
</catalog>
<catalog issue="spring">
<Books>
<book id="bk102">The OK Book1</book>
<book id="bk103">The NOT Ok Book1</book>
<book id="bk104">All OK Book1</book>
</Books>
</catalog>
<catalog issue="spring">
<Books>
<book id="bk101">The Good Book2</book>
<book id="bk103">The NOT Ok Book2</book>
<book id="bk104">All OK Book2</book>
<book id="bk105">Every OK Book2</book>
</Books>
</catalog>
CREATE TABLE BooksXML
(
xml VARIANT
);
SELECT * FROM BooksXML
我目前正在使用以下查询-
SELECT
XMLGET(XMLGET(xml,'Books'),'book',0):"$" :: VARCHAR(100) bk101
,XMLGET(XMLGET(xml,'Books'),'book',1):"$" :: VARCHAR(100) bk102
,XMLGET(XMLGET(xml,'Books'),'book',2):"$" :: VARCHAR(100) bk103
,XMLGET(XMLGET(xml,'Books'),'book',3):"$" :: VARCHAR(100) bk104
,XMLGET(XMLGET(xml,'Books'),'book',4):"$" :: VARCHAR(100) bk105
FROM BooksXML T1
在这种情况下,我传递索引 0-4 来获取数据不会产生正确的结果。
我需要使用属性名称及其值(id="bk101")来获取记录
Following is the result I am looking for
BK101,BK102,BK103,BK104,BK105
The Good Book,The OK Book,The NOT Ok Book,All OK Book,Every OK Book
NULL,The OK Book1,The NOT Ok Book1,All OK Book1,NULL
The Good Book2,NULL,The NOT Ok Book2,All OK Book2,Every OK Book2
【问题讨论】:
【参考方案1】:这将为您提供每本书的 id 和价值:
select xx.seq, xx.value:"@id" id, xx.value:"$" title
from BooksXML, table(flatten(xml:"$":"$")) xx
然后枢轴显示所需的结果:
select *
from (
select xx.seq, xx.value:"@id" id, xx.value:"$" title
from BooksXML, table(flatten(xml:"$":"$")) xx
)
pivot(max(title) for id in ('bk101', 'bk102', 'bk103', 'bk104', 'bk105')) as p
order by seq
表设置:
CREATE temp TABLE BooksXML
as
select parse_xml('<catalog issue="spring">
<Books>
<book id="bk101">The Good Book</book>
<book id="bk102">The OK Book</book>
<book id="bk103">The NOT Ok Book</book>
<book id="bk104">All OK Book</book>
<book id="bk105">Every OK Book</book>
</Books>
</catalog>') xml
union all select parse_xml('
<catalog issue="spring">
<Books>
<book id="bk102">The OK Book1</book>
<book id="bk103">The NOT Ok Book1</book>
<book id="bk104">All OK Book1</book>
</Books>
</catalog>')
union all select parse_xml('
<catalog issue="spring">
<Books>
<book id="bk101">The Good Book2</book>
<book id="bk103">The NOT Ok Book2</book>
<book id="bk104">All OK Book2</book>
<book id="bk105">Every OK Book2</book>
</Books>
</catalog>');
【讨论】:
以上是关于从 XML 获取数据 - XMLGet 函数使用属性名称及其在雪花中的值的主要内容,如果未能解决你的问题,请参考以下文章
Groovy自定义 Xml 生成器 BuilderSupport ( 构造 Xml 节点类 | 封装节点名称节点值节点属性子节点 | 将封装的节点数据转为 Xml 字符串 )