我正在尝试使用FOR XML从SQL Server 2008为HL7 CDA文档生成有效的XML。我在使用语法来解决节点中的多个子节点而不是为每个子节点重复节点时遇到了麻烦。
/* Expected output:
<!--
********************************************************
Past Medical History section
********************************************************
-->
<component>
<section>
<code code="10153-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
<title>Past Medical History</title>
<text>
<list>
<item>COPD - 1998</item>
<item>Dehydration: 2001</item>
<item>Myocardial infarction: 2003</item>
</list>
</text>
</section>
</component>
*/
SELECT '
********************************************************
Past Medical History section
********************************************************
' As "comment()",
'10153-2' AS [section/code/@code],
'2.16.840.1.113883.6.1' AS [section/code/@codeSystem],
'LOINC' AS [section/code/@codeSystemName],
'Past Medical History' AS [section/title],
(SELECT [Incident] + ' - ' + [IncidentYear] as [item]
FROM [tblSummaryPastMedicalHistory] AS PMH
WHERE ([PMH].[Incident] IS NOT NULL)
AND ([PMH].[PtUnitNum] = [PatientEncounter].[PtUnitNum])
FOR XML PATH('list'), TYPE
) as [section/text]
FROM tblPatientEncounter AS PatientEncounter
WHERE (PatientEncounterNumber = 54)
FOR XML PATH('component'), TYPE
而不是获得
<text>
<list>
<item>blah</item>
<item>blah2</item>
</list>
</text>
从预期输出的结构,我得到:
<text>
<list>
<item>blah</item>
</list>
<list>
<item>blah2</item>
</list>
</text>
任何人都可以解释如何格式化FOR XML以获取节点内的多个子节点吗?
答案 0 :(得分:2)
想出来......
而不是:
FOR XML PATH('list'), TYPE) as [section/text]
更改为:
FOR XML PATH(''), TYPE) as [list/section/text]
希望将来帮助其他人。