我正在尝试获取以下示例数据以使用sql查询生成xml。
SELECT
ClassId ID,
StudentLevel Code_level,
Code,
A.FullCode FullCode,
LTRIM(RTRIM(RollNo)) Rollno,
a.SubjectID,
(SELECT Description FROM CLS_DescriptionData WHERE DescriptionId = A.DescriptionID) Description,
SUBSTRING(a.FullCode,0,Len(a.Fullcode)-Len(a.Code)+1) ParentCode
FROM (SELECT SubjectID, ClassId, StudentLevel, DescriptionID, Code, FullCode, SUBSTRING(FullCode,1,8) RootNode
FROM Rem_ClassHeader H JOIN Rem_ClassData D on H.ClassHeaderId = D.ClassHeaderId AND Version = 514 AND SubjectID = 'MAT11' ) A
JOIN Symptom_Rem_Code B on A.RootNode = B.FullCode AND A.SubjectID = B.SubjectID
ORDER BY RollNo, A.FullCode
for xml path('sample', Root('DCD')
我希望从上面的SQL查询中显示的数据以下面的XML格式显示。有人可以帮我解决这个问题。
-<DCD Subject ="Mat11" Sampel="">
-<Pt LocatorNo="some code" someNo="some no.">
-<FaultType Description="some text" Value="A1">
-<Ddd Description="some text" Value="1A">
-<CC Description="Some text" Value="2D">
<RC Description="some text" Value="01"/>
</CC>
-<CC Description="some text" Value="02">
<RC Description="some text" Value="01"/>
</CC>
</DC>
</pt>
</DCD>
但我实际得到的是以下列方式
<DCD>
<ID>01</ID>
<StudentLevel>some data</StudentLevel>
<FullCode>Mat11</FullCode>
<RollNo>01</RollNo>
<ClassID>1</ClassID>
<Description>some data</Description>
<ParentCode>000000</ParentCode>
</DCD>
答案 0 :(得分:0)
以下查询根据需要生成XML,
select 'Mat11' as [DCD/@Subject],
'' as [DCD/@Sampel],
'some code' as [DCD/Pt/@LocatorNo],
'some no.' as [DCD/Pt/@someNo],
'some Text' as [DCD/Pt/FaultType/@Description],
'Value' as [DCD/Pt/FaultType/@Value],
'some Text' as [DCD/Pt/FaultType/Ddd/@Description],
'Value' as [DCD/Pt/FaultType/Ddd/@Value],
'CC Text' as [DCD/Pt/FaultType/Ddd/CC/@Description],
'235' as [DCD/Pt/FaultType/Ddd/CC/@Value],
'some Text' as [DCD/Pt/FaultType/Ddd/CC/RC/@Description],
'01' as [DCD/Pt/FaultType/Ddd/CC/RC/@Value],
'CC1 Text' as [DCD/Pt/FaultType/Ddd/CC1/@Description],
'2' as [DCD/Pt/FaultType/Ddd/CC1/@Value],
'some Text' as [DCD/Pt/FaultType/Ddd/CC1/RC/@Description],
'02' as [DCD/Pt/FaultType/Ddd/CC1/RC/@Value]
for XML Path('')
在您显示的XML
中,有一个重复的父节点名称(CC
)我们不能像这样使用它因为同一个属性不能在同一个{{1}上生成多次},所以我使用了不同的名称(XML tag
)并使CC1
像你的一样