null 値を使用して、テーブルから XML を抽出する必要があります。
<RoomNo xsi:nil="true" />
そこで、句要素 xsinilを使用します。
for xml path('root'), elements xsinil
私の問題は、サブクエリで作成された複雑な XML の抽出にあります。句要素 xsinilは、すべてのサブクエリにコードxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"を挿入します。
内部要素 xsinilを削除すると、null フィールドが失われます。
このクエリでは:
-- Test table
CREATE TABLE #TestTable (StudentName VARCHAR(100), Course VARCHAR(100), Instructor VARCHAR(100), RoomNo VARCHAR(100))
GO
INSERT INTO #TestTable (StudentName, Course, Instructor, RoomNo)
SELECT 'Mark', 'Algebra', 'Dr. James', '101'
UNION ALL
SELECT 'Mark', 'Maths', 'Dr. Jones', '201'
UNION ALL
SELECT 'Joe', 'Algebra', 'Dr. James', null
UNION ALL
SELECT 'Joe', 'Science', 'Dr. Ross', '301'
UNION ALL
SELECT 'Joe', 'Geography', 'Dr. Lisa', null
UNION ALL
SELECT 'Jenny', 'Algebra', 'Dr. James', '101'
GO
-- Xpath query
select -- node STUDENT
A.StudentName,
(
select -- node TEST
Instructor as '@Instructor', Course as 'Course', RoomNo as 'RoomNo'
from #TestTable B where B.StudentName = A.StudentName
for xml path('test'), type, elements xsinil
) as 'node()'
from (select distinct StudentName from #TestTable) A
for xml path('student'), root('root'), type, elements xsinil
-- end
drop TABLE #TestTable
私が得た:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<student>
<StudentName>Jenny</StudentName>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo>101</RoomNo>
</test>
</student>
<student>
<StudentName>Joe</StudentName>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo xsi:nil="true" />
</test>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. Ross">
<Course>Science</Course>
<RoomNo>301</RoomNo>
</test>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. Lisa">
<Course>Geography</Course>
<RoomNo xsi:nil="true" />
</test>
</student>
<student>
<StudentName>Mark</StudentName>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo>101</RoomNo>
</test>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Instructor="Dr. Jones">
<Course>Maths</Course>
<RoomNo>201</RoomNo>
</test>
</student>
</root>
しかし、私は必要です:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<student>
<StudentName>Jenny</StudentName>
<test Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo>101</RoomNo>
</test>
</student>
<student>
<StudentName>Joe</StudentName>
<test Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo xsi:nil="true" />
</test>
<test Instructor="Dr. Ross">
<Course>Science</Course>
<RoomNo>301</RoomNo>
</test>
<test Instructor="Dr. Lisa">
<Course>Geography</Course>
<RoomNo xsi:nil="true" />
</test>
</student>
<student>
<StudentName>Mark</StudentName>
<test Instructor="Dr. James">
<Course>Algebra</Course>
<RoomNo>101</RoomNo>
</test>
<test Instructor="Dr. Jones">
<Course>Maths</Course>
<RoomNo>201</RoomNo>
</test>
</student>
</root>