简体   繁体   English

SQL XML命名空间

[英]SQL XML Namespaces

I've been using a sql query to generate an xml output. 我一直在使用sql查询来生成xml输出。 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)设置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');

This works fine so far, but in this query above, the variable @transactionXml is already an xml datatype. 到目前为止,它仍然可以正常工作,但是在上面的查询中,变量@transactionXml已经是xml数据类型。 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=""

<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. 通过将完整的xml字符串转换为nvarchar(max) ,然后用名称空间替换了<szamlak>标记,我以不推荐的方式解决了该问题。 In the output, the xml text is being casted to back to xml datatype. 在输出中,将xml文本强制转换回xml数据类型。 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 一种方法是简单地将其加载到xml变量中,然后从中删除该不需要的属性

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM