修改XML输出

时间:2018-02-19 13:10:46

标签: sql-server xml tsql sqlxml

我以下面的方式生成了XML,除了我用红色边框的标记。

enter image description here

  

如何将标记为红色的以下行添加到Accounts标记中:    ba.cbbh.crr.retail 以便它看起来如上图所示?   (现在看起来像this

     

考虑到我必须避免XML语法错误,例如空格,但在Accounts之后保留空格。

     

<Accounts xmlns="ba.cbbh.crr.retailba.cbbh.crr.retail">

I tried this but nothing changes...

DECLARED ALL...

SET @ACCOUNTTYPE = 'T';

SET @kodBanke = ( SELECT vrednost FROM dbini WHERE IDENT = 'KOD' AND SECTION = 'PP' );
SET @XML_info = '<?xml version="1.0" encoding="UTF-8"?>';
SET @xmlnsDodatak =  'ba.cbbh.crr.retail';



IF (@tabelaTipaRacuna = 'istdev')
BEGIN
SELECT @SQL =  'WITH Ent_Posta
            AS
        (
           SELECT e.naziv,p.posta,e.sifra
            FROM entitet AS e
          INNER JOIN poste AS p ON e.sifra = p.entitet
        )

           SELECT [dbo].[brojracuna](' + @kodBanke + ',i.partija) AS ''AccountNo/BBAN'',
           [dbo].[GENERATEIBAN](i.partija) AS ''AccountNo/IBAN'', 
           ' + '''D''' + ' AS ''AccountType'',
           (a.ime + ''('' + a.roditel + '')''  + a.prezime) AS ''Name'',
           a.embg AS ''UID'',
       CASE status 
       WHEN 2 THEN ''A'' 
       WHEN 4 THEN ''B''
       WHEN 8 THEN ''U''
  END AS ''Status'',
  c.sifra AS ''Territory'',
  ' + @kodBanke + ' as ''ID_Bank'',
  CONVERT(DATETIME,' + 'i.dotvoranje' + ',120) AS ''OpeningDate'',
  ISNULL(CONVERT(DATETIME,' + '1' + ',120),'''') AS ''ClosingDate''
 FROM adresar AS a
  INNER JOIN' + QUOTENAME(@tabelaTipaRacuna) + ' AS i 
      ON a.embg = i.embg
        INNER JOIN Ent_Posta as c
        ON a.postbroj = c.posta 
        FOR XML PATH(''Account''), ROOT(''Accounts'')'

         SELECT @SQL = REPLACE(@SQL, '''<Accounts>''', '''<Accounts xmlns="' + @xmlnsDodatak + '">''')
         SELECT @SQL
         EXEC(@SQL)

解决方案

这对我有用......

 ...'WITH XMLNAMESPACES(DEFAULT ''ba.cbbh.crr.retail''), Ent_Posta
            AS
        (
           SELECT e.naziv,p.posta,e.sifra
            FROM entitet AS e
          INNER JOIN poste AS p ON e.sifra = p.entitet
        )...



... INNER JOIN Ent_Posta as c
    ON a.postbroj = c.posta 
    FOR XML PATH(''Account''), ROOT(''Accounts'')'...

编辑:

现在我想要这个:

enter image description here

经过研究,我发现它看起来应该是这样的:

...SELECT @SQL = 'SELECT ''<?xml version=""1.0"" encoding=""UTF-8""?>''' + ''

         'WITH XMLNAMESPACES(DEFAULT ''ba.cbbh.crr.retail''), Ent_Posta
            AS
        (
           SELECT e.naziv,p.posta,e.sifra
            FROM entitet AS e
          INNER JOIN poste AS p ON e.sifra = p.entitet
        ) ...

显然,它不会起作用。 这有什么问题?

1 个答案:

答案 0 :(得分:1)

这是一个默认命名空间。所以请遵循以下示例:

  

指定默认命名空间您可以使用DEFAULT声明默认命名空间,而不是声明命名空间前缀   关键词。在FOR XML查询中,它将默认命名空间绑定到   生成的XML中的XML节点。在以下示例中,WITH   XMLNAMESPACES定义了两个一起定义的名称空间前缀   使用默认命名空间。

WITH XMLNAMESPACES ('uri1' as ns1,   
                    'uri2' as ns2,  
                    DEFAULT 'uri2')  
SELECT ProductID,   
      Name,  
      Color  
FROM Production.Product   
WHERE ProductID=316 or ProductID=317  
FOR XML RAW ('ns1:Product'), ROOT('ns2:root'), ELEMENTS  

Add Namespaces to Queries with WITH XMLNAMESPACES

您有CTE,并将其与XMLNAMESPACES结合使用,如下所示:

WITH XMLNAMESPACES ('uri1' as ns1,   
                    'uri2' as ns2,  
                    DEFAULT 'uri2'),
q as
(
SELECT ProductID,   
      Name,  
      Color  
FROM Production.Product   
WHERE ProductID=316 or ProductID=317  
)
select *
from q
FOR XML RAW ('ns1:Product'), ROOT('ns2:root'), ELEMENTS