XMLGET() 获取多个内部标签值

问题描述 投票:0回答:1

下面是 xmldata 列中的 XML 数据。

<Inquiry>
  <MemberCode>56</MemberCode>
  <MemberName>COMP</MemberName>
  <Date>16</Date>
</Inquiry>

如何使用 XMLGET 从内部标记中仅提取值

SELECT TOP 100
    KEY,
    GET(b.VALUE,'@')::STRING as name, --returns Inquiry
    b.VALUE:"$"[1]."$"[1],--returns null
    GET(XMLGET(b.VALUE, GET(b.VALUE,'@')::STRING), '$') as value1,--returns null
    -- XMLGET(VALUE,GET(b.VALUE,'@')::STRING):"$" as value,--returns null
    b.value as children,-- returns full xmldata
    b.this
    
FROM
    (SELECT  
        KEY,
        VALUE
        ,this
    from
         FULL_XML,
        LATERAL FLATTEN(XMLGET(XMLGET(XML_DATA,'Report'),'Inquiries'):"$") )b ;

值列的预期输出是:

56COMP16
xml-parsing snowflake-cloud-data-platform
1个回答
0
投票

如果您的对象是单级事物,那么要访问该事物的成员,您只需命名它们,转换为字符串,然后连接它们:

with data_in_cte as (
    select parse_xml('<Inquiry>
  <MemberCode>56</MemberCode>
  <MemberName>COMP</MemberName>
  <Date>16</Date>
</Inquiry>') as XML_DATA
)
select
    get(xmlget(xml_data,'MemberCode'),'$')::text as a,
    get(xmlget(xml_data,'MemberName'),'$')::text as b,
    get(xmlget(xml_data,'Date'),'$')::text as  c,
    a || b || c as result
from data_in_cte

enter image description here

如果字段可能丢失,请将它们与给定的全部 null 连接起来,这样如果发生这种情况,可以使用 NVL:

with data_in_cte as (
    select parse_xml($1) as XML_DATA
    from values
    ('<Inquiry><MemberCode>56</MemberCode><MemberName>COMP</MemberName><Date>16</Date></Inquiry>'),
    ('<Inquiry><MemberCode>42</MemberCode><MemberName>NOCOMP</MemberName></Inquiry>')
)
select
    get(xmlget(xml_data,'MemberCode'),'$')::text as a,
    get(xmlget(xml_data,'MemberName'),'$')::text as b,
    get(xmlget(xml_data,'Date'),'$')::text as  c,
    a || b || c as result,
    nvl(a,'') || nvl(b,'') || nvl(c,'') as safe_result
from data_in_cte

enter image description here

然后,如果你有一个

Inquiy
数组,你会想将其展平。

© www.soinside.com 2019 - 2024. All rights reserved.