我在 XML 列的 SQL Server 表中有一些 XML 数据,如下所示:
<AffordabilityResults>
<matchlevel xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">IndividualMatch</matchlevel>
<searchdate xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">2013-07-29T11:20:53</searchdate>
<searchid xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">{E40603B5-B59C-4A6A-92AB-98DE83DB46E7}</searchid>
<calculatedgrossannual xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">13503</calculatedgrossannual>
<debtstress xmlns="urn:callcredit.co.uk/soap:affordabilityapi2">
<incomedebtratio>
<totpaynetincome>0.02</totpaynetincome>
<totamtunsecured>0.53</totamtunsecured>
<totamtincsec>0.53</totamtincsec>
</incomedebtratio>
</debtstress>
</AffordabilityResults>
您会注意到有些元素具有 xmlns 属性,而有些则没有...
我需要编写查询来返回数据——更重要的是向业务分析师展示如何编写自己的查询来获取她需要的数据,所以我希望它尽可能简单。
我可以使用 WITH XMLNAMESPACES 元素轻松查询数据,如下所示:
WITH XMLNAMESPACES (N'urn:callcredit.co.uk/soap:affordabilityapi2' as x )
SELECT
ResponseXDoc.value('(/AffordabilityResults/x:matchlevel)[1]','varchar(max)' ) AS MatchLevel
, ResponseXDoc.value('(/AffordabilityResults/x:debtstress/x:incomedebtratio/x:totamtunsecured)[1]','nvarchar(max)' ) AS UnsecuredDebt
FROM [NewBusiness].[dbo].[t_TacResults]
但是在查询中添加 x: 部分会使它看起来过于复杂,我想让业务分析师保持简单。
我尝试添加:
WITH XMLNAMESPACES (DEFAULT 'urn:callcredit.co.uk/soap:affordabilityapi2' )
并从 XQuery 中删除 x: - 但这会返回 null(可能是因为根元素上缺少 xmlns?)
有没有什么方法可以在有或没有默认命名空间的情况下简化这些查询?