2009-07-20 39 views
3

背景:我使用SQL FOR XML查詢生成更大的XML文檔(HL7 CDA文檔)。遵循約定,我們需要在此XML節點之前包含節註釋,以便當節點重組爲更大的文檔時,它們更易於閱讀。使用SQL FOR XML語句生成XML註釋

這裏是預期輸出的一個示例:

<!-- 
******************************************************** 
    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> 

這裏是我構建呈現上述XML FOR XML語句的SQL:

SELECT  '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].[PatientUnitNumber] = [PatientEncounter].[PatientUnitNumber]) 
      FOR XML PATH('list'), TYPE 
      ) as "section/text" 
FROM   tblPatientEncounter AS PatientEncounter 
WHERE  (PatientEncounterNumber = 6) 
FOR XML PATH('component'), TYPE 

雖然我可以插入評論從重新組裝這些XML片段到主文檔的控制功能,我們的目標是通過輸出生成註釋以避免文檔構建錯誤。

我已經嘗試了一些東西,但是在用SELECT語句生成註釋時遇到了麻煩。我嘗試了一個簡單的字符串,但一直沒有能夠獲得換行符的語法。有什麼建議麼?

回答

10

例子:

SELECT [EmployeeKey] 
     ,[ParentEmployeeKey] 
     ,[FirstName] 
     ,[LastName] 
     ,[MiddleName] 
     ,[DepartmentName] AS "comment()" 
    FROM [AdventureWorksDW2008].[dbo].[DimEmployee] 
    FOR XML PATH('Employee'),ROOT('Employees') 

生產:

<Employees> 
    <Employee> 
    <EmployeeKey>1</EmployeeKey> 
    <ParentEmployeeKey>18</ParentEmployeeKey> 
    <FirstName>Guy</FirstName> 
    <LastName>Gilbert</LastName> 
    <MiddleName>R</MiddleName> 
    <!--Production--> 
    </Employee> 
    <Employee> 
    <EmployeeKey>2</EmployeeKey> 
    <ParentEmployeeKey>7</ParentEmployeeKey> 
    <FirstName>Kevin</FirstName> 
    <LastName>Brown</LastName> 
    <MiddleName>F</MiddleName> 
    <!--Marketing--> 
    </Employee> 
</Employees> 
+0

感謝您的幫助!必須真正放入任何我想要保存的空白區域,例如退貨,標籤等;因爲評論的性質不會識別任何 標籤等。 – 2009-07-20 18:13:20