I've been using a sql
query to generate an xml
output. I have set WITH XMLNAMESPACES(DEFAULT 'http://schemas.nav.gov.hu/2013/szamla', 'http://www.w3.org/2001/XMLSchema' as xs)
to set the namespaces.
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');
This works fine so far, but in this query above, the variable @transactionXml
is already an xml
datatype. So the output of this query above looks like this:
<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>
...
My question is now, how can I avoid, that each szamla
entry gets the property xmlns=""
<szamla xmlns="">
It should look like this:
<szamla>
Thanks in advance for your help.
I have solved it with the not recommended way by casting the complete xml string to nvarchar(max)
and then replaced the <szamlak>
tag with the namespaces. In the output, the xml text is being casted to back to xml
datatype. I have found no better solution yet.
DECLARE @xml nvarchar(max)
SET @xml = (
CAST(
(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')
) as nvarchar(max))
)
SET @xml = REPLACE(@xml,'<szamlak>', '<szamlak xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.nav.gov.hu/2013/szamla">')
SELECT cast(@xml as xml)
One way is to simply load it in an xml variable, then remove that unwanted property from it
For example:
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;
Returns:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.