gpt4 book ai didi

SQL XML 命名空间

转载 作者:行者123 更新时间:2023-12-04 17:06:56 25 4
gpt4 key购买 nike

我一直在使用 sql查询生成 xml输出。
我已经设置了WITH XMLNAMESPACES(DEFAULT 'http://schemas.nav.gov.hu/2013/szamla', 'http://www.w3.org/2001/XMLSchema' as xs)设置命名空间。

WITH XMLNAMESPACES(DEFAULT 'http://schemas.nav.gov.hu/2013/szamla', 'http://www.w3.org/2001/XMLSchema' as xs)  
SELECT CAST(getdate() as date) AS export_datuma
,@noOfResults AS export_szla_db
,@fromDate AS kezdo_ido
,@toDate AS zaro_ido
,@minInvoiceNo AS kezdo_szla_szam
,@maxInvoiceNo AS zaro_szla_szam
,@transactionXml AS [*]
FOR XML PATH('szamlak');

到目前为止,这工作正常,但在上面的这个查询中,变量 @transactionXml已经是 xml数据类型。所以上面这个查询的输出看起来像这样:
<szamlak xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.nav.gov.hu/2013/szamla">
<export_datuma>2018-01-12</export_datuma>
<export_szla_db>21</export_szla_db>
<kezdo_ido>2018-01-01</kezdo_ido>
<zaro_ido>2018-01-12</zaro_ido>
<kezdo_szla_szam>40003753</kezdo_szla_szam>
<zaro_szla_szam>70000219</zaro_szla_szam>
<szamla xmlns="">
<fejlec>
<szlasorszam>40003753</szlasorszam>
<szlatipus>Rechnung</szlatipus>
<szladatum>2018-01-02</szladatum>
<teljdatum>2017-12-21</teljdatum>
</fejlec>
...

我现在的问题是,我该如何避免,每个 szamla entry 获取属性 xmlns=""
<szamla xmlns="">

它应该是这样的:
<szamla>

在此先感谢您的帮助。

最佳答案

一种方法是简单地将其加载到 xml 变量中,然后从中删除不需要的属性

例如:

declare @noOfResults int = 12;
declare @fromDate date = '2018-01-01';
declare @toDate date = '2018-01-12';
declare @minInvoiceNo int = 40003753;
declare @maxInvoiceNo int = 70000219;
declare @transactionXml xml = '<szamla>
<fejlec>
<szlasorszam>40003753</szlasorszam>
<szlatipus>Rechnung</szlatipus>
<szladatum>2018-01-02</szladatum>
<teljdatum>2017-12-21</teljdatum>
</fejlec>
</szamla>';

declare @xml xml;

WITH XMLNAMESPACES (DEFAULT 'http://schemas.nav.gov.hu/2013/szamlas', 'http://www.w3.org/2001/XMLSchema' as xs)
select @xml = (
SELECT
CAST(getdate() as date) AS export_datuma
,@noOfResults AS export_szla_db
,@fromDate AS kezdo_ido
,@toDate AS zaro_ido
,@minInvoiceNo AS kezdo_szla_szam
,@maxInvoiceNo AS zaro_szla_szam
,@transactionXml [*]
FOR XML PATH('szamlak')
);

set @xml = cast(replace(cast(@xml as nvarchar(max)),'xmlns=""','') as xml);

select @xml;

返回:
<szamlak xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.nav.gov.hu/2013/szamlas">
<export_datuma>2018-01-12</export_datuma>
<export_szla_db>12</export_szla_db>
<kezdo_ido>2018-01-01</kezdo_ido>
<zaro_ido>2018-01-12</zaro_ido>
<kezdo_szla_szam>40003753</kezdo_szla_szam>
<zaro_szla_szam>70000219</zaro_szla_szam>
<szamla>
<fejlec>
<szlasorszam>40003753</szlasorszam>
<szlatipus>Rechnung</szlatipus>
<szladatum>2018-01-02</szladatum>
<teljdatum>2017-12-21</teljdatum>
</fejlec>
</szamla>
</szamlak>

关于SQL XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48222993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com