具有默认命名空间的 SQL Server XQuery

2022-10-20数据库问题
3

本文介绍了具有默认命名空间的 SQL Server XQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 XML 列的 SQL Server 表中有一些 XML 数据,如下所示:

I've got some XML Data in a SQL Server Table in an XML Column as follows:

<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 属性,有些则没有...

You'll note that some of the elements have an xmlns attribute and some don't...

我需要编写查询来返回数据 - 更重要的是向业务分析师展示如何编写自己的查询来获取她需要的数据,因此我希望它尽可能简单.

I need to write queries to return the data - and more importantly show a business analyst how to write her own queries to get the data she needs so I want it to be as simple as possible.

我可以使用 WITH XMLNAMESPACES 元素轻松查询数据,如下所示:

I can query the data easily using the WITH XMLNAMESPACES element as follows:

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: 部分添加到查询中会使其看起来过于复杂,我希望对业务分析师保持简单.

But adding the x: part to the query makes it look overly complicated, and I want to keep it simple for the business analyst.

我尝试添加:

WITH XMLNAMESPACES (DEFAULT 'urn:callcredit.co.uk/soap:affordabilityapi2' )

并从 XQuery 中删除 x: - 但这会返回 null(可能是因为根元素上缺少 xmlns?)

and removing the x: from the XQuery - but this returns null (possibly because of the lack of the xmlns on the root element?)

有什么方法可以简化这些查询,无论是否使用默认命名空间?

Is there any way I can simplify these queries either with or without the default namespace?

推荐答案

如果命名空间在你的用例中并不重要,你可以使用命名空间通配符选择器 *:,它既选择不带和具有任意命名空间.

If namespaces are not important in your use case, you could use the namespace wildcard selector *:, which both selects nodes without and with arbitrary namespaces.

一个示例查询可能是

(/*:AffordabilityResults/*:matchlevel)[1]

业务分析师仍然需要在每个节点测试之前添加选择器,但它始终是相同的前缀",唯一预期的错误是忘记在某处使用它.

The business analyst will still have to add the selector in front of every node test, but it's the same "prefix" all the time and the only error to be expected is forgetting to use it somewhere.

这篇关于具有默认命名空间的 SQL Server XQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer SQL Server

相关推荐

如何在 sql server 2012 中部署现有的 SSIS 包?
How to deploy a existing SSIS Package in sql server 2012?(如何在 sql server 2012 中部署现有的 SSIS 包?)...
2024-04-16 数据库问题
11

“无法连接到本地 MySQL 服务器"在 docker-compose 中
quot;Can#39;t connect to local MySQL serverquot; in docker-compose(“无法连接到本地 MySQL 服务器在 docker-compose 中)...
2024-04-16 数据库问题
51

在 SQL Server 中设计 1:1 和 1:m 关系
Designing 1:1 and 1:m relationships in SQL Server(在 SQL Server 中设计 1:1 和 1:m 关系)...
2024-04-16 数据库问题
2

主键的 Sql 数据类型 - SQL Server?
Sql Data Type for Primary Key - SQL Server?(主键的 Sql 数据类型 - SQL Server?)...
2024-04-16 数据库问题
4

SQL Server:如何限制表包含单行?
SQL Server: how to constrain a table to contain a single row?(SQL Server:如何限制表包含单行?)...
2024-04-16 数据库问题
3

SQL Server 中数据库范围的唯一但简单的标识符
Database-wide unique-yet-simple identifiers in SQL Server(SQL Server 中数据库范围的唯一但简单的标识符)...
2024-04-16 数据库问题
4