Необходима декларация пространства имен, поскольку все теги XML имеют префикс пространства имен.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE
(
SchoolInfoID INT PRIMARY KEY
, MainPhoneTypeCode CHAR(4)
, MainPhoneNumber VARCHAR(20)
, AlternateNumberTypeCode CHAR(4)
, AlternatePhoneNumber VARCHAR(20)
);
INSERT INTO @tbl (
SchoolInfoID,
MainPhoneTypeCode,
MainPhoneNumber,
AlternateNumberTypeCode,
AlternatePhoneNumber
)
VALUES
(4, '0096', '(08) 8232 5550', '0026', '(08) 8336 1050');
-- DDL and sample data population, end
;WITH XMLNAMESPACES ('http://www.sifassociation.org/datamodel/au/3.4' AS ns0)
SELECT MainPhoneTypeCode AS [ns0:PhoneNumber/@Type]
, MainPhoneNumber AS [ns0:PhoneNumber/ns0:Number]
, ''
, AlternateNumberTypeCode AS [ns0:PhoneNumber/@Type]
, AlternatePhoneNumber AS [ns0:PhoneNumber/ns0:Number]
FROM @tbl
WHERE SchoolInfoID = 4
FOR XML PATH(''), TYPE, ROOT('ns0:PhoneNumberList');
Вывод
<ns0:PhoneNumberList xmlns:ns0="http://www.sifassociation.org/datamodel/au/3.4">
<ns0:PhoneNumber Type="0096">
<ns0:Number>(08) 8232 5550</ns0:Number>
</ns0:PhoneNumber>
<ns0:PhoneNumber Type="0026">
<ns0:Number>(08) 8336 1050</ns0:Number>
</ns0:PhoneNumber>
</ns0:PhoneNumberList>