提问人:Marc L 提问时间:11/9/2022 更新时间:11/9/2022 访问量:20
在 MS SQL SERVER 中解析 xml 时获取空白结果
Getting blank result when parsing xml in MS SQL SERVER
问:
我正在尝试解析存储在 SQL Server 表中的 XML 数据。我尝试使用以下代码(调整以删除个人信息并显示设置),但它返回空白。有没有办法做到这一点来获得我的结果?预期结果应为
您的索赔已于 2022 年 10 月 22 日被拒绝。您的索赔已 拒绝。原因:这是一个非活动方案。请联系 客户服务中心123456789或发送电子邮件至 [email protected] 援助。
declare @tempxml as table (xmlstr varchar(max));
insert into @tempxml
values (replace('<?xml version="1.0" encoding="UTF-8"?>
<hb:MedicalAidMessage xmlns:hb="address.co.za/messaging"
Version="6.0.0">
<Claim>
<Details>
<Responses>
<Response Type="Error">
<Code>6</Code>
<Desc>Your claim has been rejected on 2022/10/22. Your claim has been rejected. Reason: This is an inactive scheme. Please contact the Client Service Centre on 123456789 or at [email protected] for assistance.</Desc>
</Response>
</Responses>
</Details>
</Claim>
</hb:MedicalAidMessage> ',':',''))
declare @XMLData xml
set @XMLData = (select * from @tempxml)
select [Reason] = n.value('Desc[1]', 'nvarchar(2000)')
from @XMLData.nodes('/hbMedicalAidMessage/Claim/Details/Reponses/Response') as a(n)
谢谢
答:
1赞
AlwaysLearning
11/9/2022
#1
请考虑以下查询,这些查询演示了正确访问命名空间引用元素的两种方法:
select [Reason] = Response.value('(Desc/text())[1]', 'nvarchar(2000)')
from @XMLData.nodes('
declare namespace foo = "address.co.za/messaging";
/foo:MedicalAidMessage/Claim/Details/Responses/Response') as a(Response);
with xmlnamespaces('address.co.za/messaging' as foo)
select [Reason] = Response.value('(Desc/text())[1]', 'nvarchar(2000)')
from @XMLData.nodes('/foo:MedicalAidMessage/Claim/Details/Responses/Response') as a(Response);
请注意,查询中的命名空间前缀与原始 XML 中的前缀不匹配。需要匹配 XML 的前缀不是前缀,而是它们引用的命名空间,在所有情况下,这些命名空间都是 .foo
hb
address.co.za/messaging
评论
hbMedicalAidMessage
MedicalAidMessage
hb