使用FOR XML Raw或Auto时如何在子查询中删除冗余名称空间

时间:2018-12-13 23:11:35

标签: sql for-xml

我有一个包含用户信息的用户表。

Create table #Users (
userid uniqueidentifier, 
NationalID int,
Firstname varchar (25), 
LastName varchar(25), 
suffix varchar(25), 
DOB datetime, 
Gender nvarchar(2),
position nvarchar(200)

 ) 

  INSERT INTO #USERS
  Values 
  ('8F6390AF-8C7C-4036-AEEC-7152161A23A7',
  1234567, 
  'Bob',
 'gates',
  'II',
  '01-01-1987', 
  'M',
  'Manager')

我想使用For XML Raw返回XML格式的结果,并且我想使用下面的子查询在根节点上使用默认名称空间(https://Somthing

  WITH xmlnamespaces(Default 'https://Somthing')
  SELECT
  (
    SELECT GetDate() as AsOfDate, NationalID,
        (
        SELECT FirstName, 
                LastName, 
                Suffix
        FROM #Users AS [Name]
        WHERE [Name].UserID= u.UserID
        FOR XML RAW('Name'), TYPE,ELEMENTS
    ),
DOB, 
Gender
FROM #Users AS EmployeeInfo
 WHERE EmployeeInfo.UserID = u.UserID
        FOR XML Raw ('EmployeeInfo'), TYPE, ELEMENTS)
       From #Users u
       WHERE u.userid = '8F6390AF-8C7C-4036-AEEC-7152161A23A7'
      FOR XML Raw ('Transcript'),Type, ELEMENTS XSINIL

我得到的结果是所有节点中的冗余名称空间。我应该如何删除其他节点中的这些冗余并将其仅保留在顶部

            <Transcript xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
             instance" xmlns="https://Somthing">
          <EmployeeInfo xmlns="https://Somthing">
            <AsOfDate>2018-12-13T17:54:11.183</AsOfDate>
            <NationalID>1234567</NationalID>
            <Name xmlns="https://Somthing">
              <FirstName>Bob</FirstName>
              <LastName>gates</LastName>
              <Suffix>II</Suffix>
            </Name>
            <DOB>1987-01-01T00:00:00</DOB>
            <Gender>M</Gender>
          </EmployeeInfo>
        </Transcript>

我希望我的结果像下面这样

   <Transcript xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="https://Somthing">
          <EmployeeInfo>
            <AsOfDate>2018-12-13T17:54:11.183</AsOfDate>
            <NationalID>1234567</NationalID>
            <Name>
              <FirstName>Bob</FirstName>
              <LastName>gates</LastName>
              <Suffix>II</Suffix>
            </Name>
            <DOB>1987-01-01T00:00:00</DOB>
            <Gender>M</Gender>
          </EmployeeInfo>
        </Transcript>

    Thanks

0 个答案:

没有答案