在SQL中添加XML模式引用

在SQL中添加XML模式引用,sql,sql-server,xml,tsql,Sql,Sql Server,Xml,Tsql,如果可以在XML的根目录中添加对模式的直接引用,我一生都找不到 这是我的密码: Declare @Employee table (EmployeeID int,First_Name varchar(50),Last_Name varchar(50)) Insert into @Employee values (1,'John','Smith'), (2,'Jane','Doe' ) Select (Select CreatedBy='My Organizat

如果可以在XML的根目录中添加对模式的直接引用,我一生都找不到

这是我的密码:

Declare @Employee table (EmployeeID int,First_Name varchar(50),Last_Name varchar(50))
    Insert into @Employee values
    (1,'John','Smith'),
    (2,'Jane','Doe'  )


    Select (Select CreatedBy='My Organization',CreateDate=GetDate() For XML Path('RecordHeader'),Type ) 
          ,(Select * From @Employee For XML Path('Employee'),Type ) 
    For XML Path ('Employees'),Type
这将产生以下结果:

<Employees>
  <RecordHeader>
    <CreatedBy>My Organization</CreatedBy>
    <CreateDate>2016-10-18T16:09:48.110</CreateDate>
  </RecordHeader>
  <Employee>
    <EmployeeID>1</EmployeeID>
    <First_Name>John</First_Name>
    <Last_Name>Smith</Last_Name>
  </Employee>
  <Employee>
    <EmployeeID>2</EmployeeID>
    <First_Name>Jane</First_Name>
    <Last_Name>Doe</Last_Name>
  </Employee>
</Employees>

我的组织
2016-10-18T16:09:48.110
1.
约翰
史密斯
2.
简
雌鹿
是否有一种方法可以在my SQL中添加part,使根标记包含以下内容:

<Employees xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">

谢谢你的帮助

您可以使用关键字,但这会将名称空间添加到所有节点:

Declare @Employee table (EmployeeID int,First_Name varchar(50),Last_Name varchar(50))
Insert into @Employee values
(1,'John','Smith'),
(2,'Jane','Doe'  );


WITH XMLNAMESPACES (DEFAULT 'https://www.blahblah.org/blahblah/BlahSchema.xsd')
Select (Select CreatedBy='My Organization',CreateDate=GetDate() For XML Path('RecordHeader'),Type ) 
      ,(Select * From @Employee For XML Path('Employee'),Type ) 
For XML Path ('Employees'),Type
结果:

<Employees xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">
  <RecordHeader xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">
    <CreatedBy>My Organization</CreatedBy>
    <CreateDate>2017-01-10T16:36:23.450</CreateDate>
  </RecordHeader>
  <Employee xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">
    <EmployeeID>1</EmployeeID>
    <First_Name>John</First_Name>
    <Last_Name>Smith</Last_Name>
  </Employee>
  <Employee xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">
    <EmployeeID>2</EmployeeID>
    <First_Name>Jane</First_Name>
    <Last_Name>Doe</Last_Name>
  </Employee>
</Employees>

我的组织
2017-01-10T16:36:23.450
1.
约翰
史密斯
2.
简
雌鹿

在使用xmlns时总是遇到问题,所以我使用了一些技巧

Declare @Employee table (EmployeeID int,First_Name varchar(50),Last_Name varchar(50))
Insert into @Employee values
(1,'John','Smith'),
(2,'Jane','Doe'  )

Declare @XML xml = (
Select (Select CreatedBy='My Organization',CreateDate=GetDate() For XML Path('RecordHeader'),Type ) 
      ,(Select * From @Employee For XML Path('Employee'),Type ) 
For XML Path ('Employees'),Type
)

set @XML.modify('insert ( attribute MyNameSpace {"https://www.blahblah.org/blahblah/BlahSchema.xsd"}) into (/Employees)[1]')
set @XML = replace(cast(@XML as nvarchar(max)),'[MyNameSpace]','xmlns')

Select @XML
返回

<Employees xmlns="https://www.blahblah.org/blahblah/BlahSchema.xsd">
  <RecordHeader>
    <CreatedBy>My Organization</CreatedBy>
    <CreateDate>2017-01-10T15:19:42.873</CreateDate>
  </RecordHeader>
  <Employee>
    <EmployeeID>1</EmployeeID>
    <First_Name>John</First_Name>
    <Last_Name>Smith</Last_Name>
  </Employee>
  <Employee>
    <EmployeeID>2</EmployeeID>
    <First_Name>Jane</First_Name>
    <Last_Name>Doe</Last_Name>
  </Employee>
</Employees>

这看起来很有希望,但它似乎将其添加到每个父标记而不是根标记中。Tks!谢谢你,约翰!你照常过来@我也不知道直接的方法+从我这边。一个小提示:最好转换为
NVARCHAR(MAX)
,因为XML是16位内部编码的,除了性能之外,VARCHAR可能会弄乱一些特殊字符。。。还有一个想法:使用
东西
在字符串基础上用
替换开头的
可能更容易…@johncapelletti,我不知道,这看起来很傻。。。我的假设是,在内部,XML的处理方式与从OPENXML调用
(或从超重量级函数调用
的结果非常相似。更改默认名称空间可能会产生很高且不可预测的影响…@ssokol91请参阅更新/编辑。默认情况下,空值将被忽略,因此可以通过NullIf()为空值设置空值
Declare @Employee table (EmployeeID int,First_Name varchar(50),Last_Name varchar(50))
Insert into @Employee values
(1,'John','Smith'),
(2,'Jane','Doe'  ),
(3,'Betty',null  ),  -- No element for Last_Name
(4,'Susan',''  )     -- No element for Last_Name

Declare @XML xml = (
Select (Select CreatedBy='My Organization',CreateDate=GetDate() For XML Path('RecordHeader'),Type ) 
      ,(Select EmployeeID = NullIf(EmployeeID,'') 
              ,First_Name = NullIf(First_Name,'')
              ,Last_Name  = NullIf(Last_Name,'')
         From  @Employee For XML Path('Employee'),Type ) 
For XML Path ('Employees'),Type
)

set @XML.modify('insert ( attribute MyNameSpace {"https://www.blahblah.org/blahblah/BlahSchema.xsd"}) into (/Employees)[1]')
set @XML = replace(cast(@XML as nvarchar(max)),'[MyNameSpace]','xmlns')

Select @XML