Index: ccr2ccd-xslt/trunk/ccr_ccd.xsl
===================================================================
--- ccr2ccd-xslt/trunk/ccr_ccd.xsl	(revision 746)
+++ ccr2ccd-xslt/trunk/ccr_ccd.xsl	(revision 757)
@@ -1,84 +1,92 @@
 <?xml version="1.0" encoding="utf-8"?>
-
 <!--
-Conversion of CCR to Level 2 CCD
-
-Author:   	Ken Miller
+Conversion of CCR to Level 3 CCD
+
+Orginal Author:   	Ken Miller
 Solventus LLC
 ken.miller@solventus.coms
 
-Date: 	2008-04-25
+Contributors:
+Richard Braman, EHR Doctors, Inc rbraman@ehrdoctors.com
+George Gilly (WorldVistA glilly@glilly.net)
+xxxx - Oroville Hospital
+
+Date: 	2010-05-5
 Version: 	0.1
 
+License :
+
+        This program is free software: you can redistribute it and/or modify
+        it under the terms of the GNU General Public License as published by
+        the Free Software Foundation, either version 3 of the License, or
+        (at your option) any later version.
+
+        This program is distributed in the hope that it will be useful,
+        but WITHOUT ANY WARRANTY; without even the implied warranty of
+        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+        GNU General Public License for more details.
+
+        You should have received a copy of the GNU General Public License
+        along with this program.  If not, see http://www.gnu.org/licenses.
+
 -->
-
-<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="urn:astm-org:CCR" xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="a date">
-
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="urn:astm-org:CCR" xmlns:date="http://exslt.org/dates-and-times" exclude-result-prefixes="a date" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <xsl:import href="./templates/code.xsl"/>
-    <xsl:import href="./templates/datetime.xsl"/>
+    <xsl:import href="./templates/ccdcodedvalue.xsl"/>
+    <xsl:import href="./templates/ccdentity.xsl"/>
+    <xsl:import href="./templates/ccdid.xsl"/>
+    <xsl:import href="./templates/ccddatetime.xsl"/>
+    <xsl:import href="./templates/ccdstatus.xsl"/>
+    <xsl:import href="./templates/ccdmedicationfrequency.xsl"/>
     <xsl:import href="./templates/actor.xsl"/>
     <xsl:import href="./templates/problemDescription.xsl"/>
-
-    <xsl:output method="xml" encoding="utf-8" version="1.0"/>
+    <xsl:import href="./templates/HL7OIDLookup.xsl"/>
+
+    <xsl:output method="xml" encoding="utf-8" version="1.0" indent="yes"/>
     <xsl:template match="/">
-        <ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-            <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040" />
+        <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="CCD.xsl"</xsl:processing-instruction>
+        <ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 http://xreg2.nist.gov:8080/hitspValidation/schema/cdar2c32/infrastructure/cda/C32_CDA.xsd" classCode="DOCCLIN" moodCode="EVN">
+            <realmCode code="US"/>
+            <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>
             <templateId root="2.16.840.1.113883.10.20.1"/>
-            <id root="db4"/>
-            <code code="34133-9" codeSystem="2.16.840.1.113883.6.1" displayName="Summarization of episode note"/>
+            <templateId root="1.3.6.1.4.1.19376.1.5.3.1.1.1"/>
+            <templateId root="2.16.840.1.113883.10.20.3"/>
+            <templateId root="2.16.840.1.113883.3.88.11.32.1"/>
+
+            <id>
+                <xsl:attribute name="root">
+                    <xsl:value-of select="/a:ContinuityOfCareRecord/a:CCRDocumentObjectID"></xsl:value-of>
+                </xsl:attribute>
+            </id>
+            <code code="34133-9" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Summarization of episode note"/>
             <title>Continuity of Care Document</title>
-            <xsl:element name="effectiveTime">
+            <effectiveTime>
                 <xsl:attribute name="value">
                     <xsl:call-template name="date:format-date">
                         <xsl:with-param name="date-time">
-                            <xsl:value-of select="a:ContinuityOfCareRecord/a:DateTime/a:ExactDateTime"/>
+                            <xsl:value-of select="/a:ContinuityOfCareRecord/a:DateTime/a:ExactDateTime"/>
                         </xsl:with-param>
                         <xsl:with-param name="pattern">yyyyMMddhhmmss+0000</xsl:with-param>
                     </xsl:call-template>
                 </xsl:attribute>
-            </xsl:element>
+            </effectiveTime>
             <confidentialityCode code="N" codeSystem="2.16.840.1.113883.5.25"/>
             <languageCode code="en-US"/>
-            <xsl:variable name="patientID" select="a:ContinuityOfCareRecord/a:Patient[1]/a:ActorID"/>
+            <xsl:variable name="patientID" select="/a:ContinuityOfCareRecord/a:Patient[1]/a:ActorID"/>
             <recordTarget>
                 <xsl:for-each select="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$patientID]">
                     <patientRole>
-                        <xsl:element name="id">
-                            <xsl:attribute name="extension">
-                                <xsl:value-of select="a:IDs/a:ID[1]"/>
-                            </xsl:attribute>
-                        </xsl:element>
-                        <patient>
-                            <name>
-                                <given>
-                                    <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Given"></xsl:value-of>
-                                </given>
-                                <family>
-                                    <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Family"></xsl:value-of>
-                                </family>
-                            </name>
-                            <xsl:element name="administrativeGenderCode">
-                                <xsl:attribute name="code">
-                                    <xsl:value-of select="a:Person/a:Gender/a:Text"></xsl:value-of>
-                                </xsl:attribute>
-                            </xsl:element>
-                            <xsl:element name="birthTime">
-                                <xsl:attribute name="value">
-                                    <xsl:call-template name="date:format-date">
-                                        <xsl:with-param name="date-time">
-                                            <xsl:value-of select="a:Person/a:DateOfBirth/a:ExactDateTime"/>
-                                        </xsl:with-param>
-                                        <xsl:with-param name="pattern">yyyyMMddhhmmss</xsl:with-param>
-                                    </xsl:call-template>
-                                </xsl:attribute>
-                            </xsl:element>
-                        </patient>
+                        <xsl:call-template name="ccdEntity">
+                            <xsl:with-param name="ccrActorObjectID" select="/a:ContinuityOfCareRecord/a:Patient[1]/a:ActorID"></xsl:with-param>
+                            <xsl:with-param name="personNodeName">patient</xsl:with-param>
+                        </xsl:call-template>
                     </patientRole>
                 </xsl:for-each>
             </recordTarget>
+
+            <xsl:variable name="fromID" select="/a:ContinuityOfCareRecord/a:From/a:ActorLink[1]/a:ActorID"/>
             <author>
-                <xsl:variable name="fromID" select="a:ContinuityOfCareRecord/a:From/a:ActorLink[1]/a:ActorID"/>
                 <xsl:for-each select="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$fromID]">
-                    <xsl:element name="time">
+                    <time>
                         <xsl:attribute name="value">
                             <xsl:call-template name="date:format-date">
@@ -89,72 +97,67 @@
                             </xsl:call-template>
                         </xsl:attribute>
-                    </xsl:element>
-                    <assignedAuthor>
-                        <xsl:element name="id">
-                            <xsl:attribute name="extension">
-                                <xsl:choose>
-                                    <xsl:when test="a:IDs/a:ID[1]">
-                                        <xsl:value-of select="a:IDs/a:ID[1]"/>
-                                    </xsl:when>
-                                    <xsl:otherwise>
-                                        <xsl:value-of select="$fromID"/>
-                                    </xsl:otherwise>
-                                </xsl:choose>
-                            </xsl:attribute>
-                        </xsl:element>
-                        <assignedPerson>
-                            <name>
-                                <prefix>
-                                    <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Title"></xsl:value-of>
-                                </prefix>
-                                <given>
-                                    <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Given"></xsl:value-of>
-                                </given>
-                                <family>
-                                    <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Family"></xsl:value-of>
-                                </family>
-                            </name>
-                        </assignedPerson>
-                    </assignedAuthor>
+                    </time>
+                    <xsl:call-template name="ccdEntity">
+                        <xsl:with-param name="ccrActorObjectID" select="/a:ContinuityOfCareRecord/a:From/a:ActorLink[1]/a:ActorID"></xsl:with-param>
+                    </xsl:call-template>
                 </xsl:for-each>
             </author>
+
             <custodian>
                 <assignedCustodian>
                     <representedCustodianOrganization>
-                        <id root="2.16.840.1.113883.3.96.3.42"/>
-                        <xsl:element name="name">
-                            <xsl:call-template name="actorName">
-                                <xsl:with-param name="objID" select="a:ContinuityOfCareRecord/a:From/a:ActorLink[1]/a:ActorID"/>
-                            </xsl:call-template>
-                        </xsl:element>
+                        <xsl:call-template name="ccdEntity">
+                            <xsl:with-param name="ccrActorObjectID" select="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$fromID]/a:InternalCCRLink[a:LinkRelationship='Organization']/a:LinkID"></xsl:with-param>
+                        </xsl:call-template>
                     </representedCustodianOrganization>
                 </assignedCustodian>
             </custodian>
+
             <documentationOf>
                 <serviceEvent classCode="PCPR">
                     <effectiveTime>
-                        <xsl:element name="low">
+                        <low>
                             <xsl:attribute name="value">
                                 <xsl:call-template name="date:format-date">
                                     <xsl:with-param name="date-time">
-                                        <xsl:value-of select="a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$patientID]/a:Person/a:DateOfBirth/a:ExactDateTime"/>
+                                        <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body//a:DateTime//a:ExactDateTime">
+                                            <xsl:sort order="ascending"/>
+                                            <xsl:if test="position()=1">
+                                                <xsl:value-of select="."/>
+                                            </xsl:if>
+                                        </xsl:for-each>
                                     </xsl:with-param>
                                     <xsl:with-param name="pattern">yyyyMMdd</xsl:with-param>
                                 </xsl:call-template>
                             </xsl:attribute>
-                        </xsl:element>
-                        <xsl:element name="high">
+                        </low>
+                        <high>
                             <xsl:attribute name="value">
                                 <xsl:call-template name="date:format-date">
                                     <xsl:with-param name="date-time">
-                                        <xsl:value-of select="a:ContinuityOfCareRecord/a:DateTime/a:ExactDateTime"/>
+                                        <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body//a:DateTime//a:ExactDateTime">
+                                            <xsl:sort order="descending"/>
+                                            <xsl:if test="position()=1">
+                                                <xsl:value-of select="."/>
+                                            </xsl:if>
+                                        </xsl:for-each>
                                     </xsl:with-param>
                                     <xsl:with-param name="pattern">yyyyMMdd</xsl:with-param>
                                 </xsl:call-template>
                             </xsl:attribute>
-                        </xsl:element>
+                        </high>
                     </effectiveTime>
                 </serviceEvent>
+
+                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:HealthCareProviders/a:Provider">
+                    <assignedEntity>
+                        <xsl:call-template name="ccdEntity">
+                            <xsl:with-param name="a:ccrActorObjectID" select="a:ActorID"/>
+                        </xsl:call-template>
+                    </assignedEntity>
+                </xsl:for-each>
+
             </documentationOf>
+
             <component>
                 <structuredBody>
@@ -165,5 +168,5 @@
                             <title>Purpose</title>
                             <text>
-                                <xsl:value-of select="a:ContinuityOfCareRecord/a:Purpose/a:Description/a:Text"></xsl:value-of>
+                                <xsl:value-of select="/a:ContinuityOfCareRecord/a:Purpose/a:Description/a:Text"></xsl:value-of>
                             </text>
                         </section>
@@ -186,5 +189,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Alerts/a:Alert">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Alerts/a:Alert">
                                                 <tr>
                                                     <td>
@@ -239,5 +242,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:AdvanceDirectives/a:AdvanceDirective">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:AdvanceDirectives/a:AdvanceDirective">
                                                 <tr>
                                                     <td>
@@ -323,5 +326,7 @@
                         <component>
                             <section>
-                                <templateId root="2.16.840.1.113883.10.20.1.11"/>
+                                <templateId root="2.16.840.1.113883.3.88.11.83.103" assigningAuthorityName="HITSP/C83"/>
+                                <templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.6" assigningAuthorityName="IHE PCC"/>
+                                <templateId root="2.16.840.1.113883.10.20.1.11" assigningAuthorityName="HL7 CCD"/>
                                 <code code="11450-4" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Problem list"/>
                                 <title>Problems</title>
@@ -337,15 +342,19 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Problems/a:Problem">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Problems/a:Problem">
                                                 <tr>
+                                                    <xsl:attribute name="id">
+                                                        <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                    </xsl:attribute>
+
                                                     <td>
                                                         <xsl:value-of select="a:Type/a:Text"/>
                                                     </td>
                                                     <td>
-                                                        <xsl:call-template name="date:format-date">
-                                                            <xsl:with-param name="date-time">
-                                                                <xsl:value-of select="a:DateTime/a:ExactDateTime"/>
-                                                            </xsl:with-param>
-                                                        </xsl:call-template>
+                                                        <table>
+                                                            <tbody>
+                                                                <xsl:apply-templates select="a:DateTime"/>
+                                                            </tbody>
+                                                        </table>
                                                     </td>
                                                     <td>
@@ -368,88 +377,69 @@
                                     </table>
                                 </text>
-                                <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Problems/a:Problem">
+                                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Problems/a:Problem">
                                     <entry typeCode="DRIV">
                                         <act classCode="ACT" moodCode="EVN">
                                             <templateId root="2.16.840.1.113883.10.20.1.27"/>
                                             <!-- Problem act template -->
-                                            <id root="6a2fa88d-4174-4909-aece-db44b60a3abb"/>
+
+                                            <!-- <id> -->
+                                            <xsl:call-template name="ccdID">
+                                                <xsl:with-param name="ccrObjectID">
+                                                    <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                </xsl:with-param>
+                                            </xsl:call-template>
+
                                             <code nullFlavor="NA"/>
-                                            <xsl:variable name="SourceActorID" select="a:Source/a:Actor/a:ActorID"/>
+
                                             <performer typeCode="PRF">
                                                 <assignedEntity>
-                                                    <xsl:variable name="CCRActor" select="//a:ContinuityOfCareRecord/a:Actors[a:ActorObjectID=$SourceActorID]/a:Actor"/>
-                                                    <xsl:if test="$CCRActor/a:Address">
-                                                        <addr>
-                                                            <streetAddressLine>
-                                                                <xsl:value-of select="$CCRActor/a:Address[0]/a:Line1"/>
-                                                            </streetAddressLine>
-                                                            <city>
-                                                                <xsl:value-of select="$CCRActor/a:Address[0]/a:City"/>
-                                                            </city>
-                                                            <state>
-                                                                <xsl:value-of select="$CCRActor/a:Address[0]/a:State"/>
-                                                            </state>
-                                                            <postalcode>
-                                                                <xsl:value-of select="$CCRActor/a:Address[0]/a:PostalCode"/>
-                                                            </postalcode>
-                                                        </addr>
-                                                    </xsl:if>
-                                                    <xsl:if test="$CCRActor/a:Telephone">
-                                                        <xsl:element name="telecom">
-                                                            <xsl:attribute name="extension">
-                                                                <xsl:value-of select="$CCRActor/a:Telephone[0]/a:Value"/>
-                                                            </xsl:attribute>
-                                                        </xsl:element>
-                                                    </xsl:if>
-                                                    <assignedPerson>
-                                                        <name>
-                                                            <xsl:call-template name="actorName">
-                                                                <xsl:with-param name="objID" select="$SourceActorID"/>
-                                                            </xsl:call-template>
-                                                        </name>
-                                                    </assignedPerson>
+                                                    <xsl:call-template name="ccdEntity">
+                                                        <xsl:with-param name="ccrActorObjectID">
+                                                            <xsl:value-of select="a:Source/a:Actor/a:ActorID"></xsl:value-of>
+                                                        </xsl:with-param>
+                                                    </xsl:call-template>
                                                 </assignedEntity>
                                             </performer>
+
                                             <entryRelationship typeCode="SUBJ">
                                                 <observation classCode="OBS" moodCode="EVN">
                                                     <templateId root="2.16.840.1.113883.10.20.1.28" assigningAuthorityName="CCD"/>
                                                     <!--Problem observation template-->
-                                                    <id root="d11275e7-67ae-11db-bd13-0800200c9a66"/>
+
+                                                    <!-- <id> -->
+                                                    <xsl:call-template name="ccdID">
+                                                        <xsl:with-param name="ccrObjectID">
+                                                            <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                        </xsl:with-param>
+                                                        <xsl:with-param name="suffix">.1</xsl:with-param>
+                                                    </xsl:call-template>
+
                                                     <code code="64572001" displayName="Condition" codeSystem="2.16.840.1.113883.6.96" codeSystemName="SNOMED-CT"/>
+
                                                     <text>
-                                                        <xsl:element name="reference">
+                                                        <reference>
                                                             <xsl:attribute name="value">
-                                                                <xsl:value-of select="a:CCRObjectID"/>
+                                                                <xsl:text>#</xsl:text>
+                                                                <xsl:value-of select="a:CCRDataObjectID"/>
                                                             </xsl:attribute>
-                                                        </xsl:element>
+                                                        </reference>
                                                     </text>
-                                                    <xsl:element name="statusCode">
-                                                        <xsl:attribute name="value">
-                                                            <xsl:value-of select="a:Status/a:Code/a:Value"/>
-                                                        </xsl:attribute>
-                                                    </xsl:element>
-                                                    <xsl:element name="effectiveTime">
-                                                        <xsl:attribute name="value">
-                                                            <xsl:call-template name="date:format-date">
-                                                                <xsl:with-param name="date-time">
-                                                                    <xsl:value-of select="a:DateTime[0]/a:ExactDateTime"/>
-                                                                </xsl:with-param>
-                                                                <xsl:with-param name="pattern">yyyyMMddhhmmss+0000</xsl:with-param>
-                                                            </xsl:call-template>
-                                                        </xsl:attribute>
-                                                    </xsl:element>
-                                                    <xsl:element name="value">
-                                                        <xsl:attribute name="displayName">
-                                                            <xsl:value-of select="a:Description/a:Text"/>
-                                                        </xsl:attribute>
-                                                        <xsl:attribute name="code">
-                                                            <xsl:value-of select="a:Description/a:Code/a:Value"/>
-                                                        </xsl:attribute>
-                                                        <xsl:attribute name="codeSystemName">
-                                                            <xsl:value-of select="a:Description/a:Code/a:CodingSystem"/>
-                                                        </xsl:attribute>
-                                                    </xsl:element>
+
+                                                    <statusCode code="completed"/>
+
+                                                    <xsl:call-template name="ccdDateTime">
+                                                        <xsl:with-param name="dt" select="a:DateTime"/>
+                                                    </xsl:call-template>
+
+                                                    <xsl:call-template name="ccdCodedValue">
+                                                        <xsl:with-param name="ccrCodedDescription" select="a:Description"/>
+                                                    </xsl:call-template>
+
+                                                    <xsl:call-template name="ccdStatus">
+                                                        <xsl:with-param name="ccrStatus" select="a:Status"/>
+                                                    </xsl:call-template>
                                                 </observation>
                                             </entryRelationship>
+
                                         </act>
                                     </entry>
@@ -480,16 +470,18 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Procedures/a:Procedure">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Procedures/a:Procedure">
                                                 <tr>
+                                                    <xsl:attribute name="id">
+                                                        <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                    </xsl:attribute>
+
                                                     <td>
                                                         <xsl:value-of select="a:Type/a:Text"/>
                                                     </td>
-                                                    <td>
-                                                        <xsl:call-template name="date:format-date">
-                                                            <xsl:with-param name="date-time">
-                                                                <xsl:value-of select="a:DateTime/a:ExactDateTime"/>
-                                                            </xsl:with-param>
-                                                        </xsl:call-template>
-                                                    </td>
+                                                    <table>
+                                                        <tbody>
+                                                            <xsl:apply-templates select="a:DateTime"/>
+                                                        </tbody>
+                                                    </table>
                                                     <td>
                                                         <xsl:apply-templates select="a:Description/a:Code"/>
@@ -543,10 +535,132 @@
                             </section>
                         </component>
+                        <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Procedures/a:Procedure">
+                            <entry typeCode="DRIV">
+                                <procedure classCode="PROC" moodCode="EVN">
+                                    <templateId root="2.16.840.1.113883.3.88.11.83.17" assigningAuthorityName="HITSP C83"/>
+                                    <templateId root="2.16.840.1.113883.10.20.1.29" assigningAuthorityName="CCD"/>
+                                    <templateId root="1.3.6.1.4.1.19376.1.5.3.1.4.19" assigningAuthorityName="IHE PCC"/>
+                                    <id>
+                                        <xsl:attribute name="root">
+                                            <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                        </xsl:attribute>
+                                    </id>
+                                    <code>
+                                        <xsl:attribute name="code">
+                                            <xsl:value-of select="a:Description/a:Code/a:Value"/>
+                                        </xsl:attribute>
+                                        <xsl:attribute name="codeSystemName">
+                                            <xsl:value-of select="a:Description/a:Code/a:CodingSystem"/>
+                                        </xsl:attribute>
+                                        <xsl:attribute name="codeSystem">
+                                            <xsl:call-template name="HL7OIDLookup">
+                                                <xsl:with-param name="name" select="a:Description/a:Code/a:CodingSystem"/>
+                                            </xsl:call-template>
+                                        </xsl:attribute>
+                                        <xsl:attribute name="displayName">
+                                            <xsl:value-of select="a:Description/a:Text"/>
+                                        </xsl:attribute>
+                                    </code>
+                                    <text>
+                                        <reference>
+                                            <xsl:attribute name="value">
+                                                <xsl:value-of select="a:CCRDataObjectID"/>
+                                            </xsl:attribute>
+                                        </reference>
+                                    </text>
+                                    <statusCode>
+                                        <xsl:attribute name="value">
+                                            <xsl:value-of select="a:Status/a:Text"/>
+                                        </xsl:attribute>
+                                    </statusCode>
+                                    <xsl:call-template name="ccdDateTime">
+                                        <xsl:with-param name="dt" select="a:DateTime"/>
+                                    </xsl:call-template>
+                                    <xsl:if test="a:Method">
+                                        <approachSiteCode>
+                                            <xsl:attribute name="code">
+                                                <xsl:value-of select="a:Method/a:Code/a:Value"/>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="codeSystemName">
+                                                <xsl:value-of select="a:Method/a:Code/a:CodingSystem"/>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="codeSystem">
+                                                <xsl:call-template name="HL7OIDLookup">
+                                                    <xsl:with-param name="name" select="a:Method/a:Code/a:CodingSystem"/>
+                                                </xsl:call-template>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="displayName">
+                                                <xsl:value-of select="a:Method/a:Text"/>
+                                            </xsl:attribute>
+                                        </approachSiteCode>
+                                    </xsl:if>
+                                    <xsl:if test="a:Site">
+                                        <targetSiteCode>
+                                            <xsl:attribute name="code">
+                                                <xsl:value-of select="a:Site/a:Code/a:Value"/>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="codeSystemName">
+                                                <xsl:value-of select="a:Site/a:Code/a:CodingSystem"/>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="codeSystem">
+                                                <xsl:call-template name="HL7OIDLookup">
+                                                    <xsl:with-param name="name" select="a:Site/a:Code/a:CodingSystem"/>
+                                                </xsl:call-template>
+                                            </xsl:attribute>
+                                            <xsl:attribute name="displayName">
+                                                <xsl:value-of select="a:Site/a:Text"/>
+                                            </xsl:attribute>
+                                        </targetSiteCode>
+                                    </xsl:if>
+                                    <xsl:if test="a:Practitioners/a:Practitioner">
+                                        <xsl:variable name="PractitionerActorID" select="a:Practitioners/a:Practitioner[0]/a:ActorID"/>
+                                        <performer typeCode="PRF">
+                                            <assignedEntity>
+                                                <xsl:variable name="CCRActor" select="/a:ContinuityOfCareRecord/a:Actors[a:ActorObjectID=$PractitionerActorID]/a:Actor"/>
+                                                <xsl:if test="$CCRActor/a:Address">
+                                                    <addr>
+                                                        <streetAddressLine>
+                                                            <xsl:value-of select="$CCRActor/a:Address[0]/a:Line1"/>
+                                                        </streetAddressLine>
+                                                        <city>
+                                                            <xsl:value-of select="$CCRActor/a:Address[0]/a:City"/>
+                                                        </city>
+                                                        <state>
+                                                            <xsl:value-of select="$CCRActor/a:Address[0]/a:State"/>
+                                                        </state>
+                                                        <postalcode>
+                                                            <xsl:value-of select="$CCRActor/a:Address[0]/a:PostalCode"/>
+                                                        </postalcode>
+                                                    </addr>
+                                                </xsl:if>
+                                                <xsl:if test="$CCRActor/a:Telephone">
+                                                    <telecom>
+                                                        <xsl:attribute name="extension">
+                                                            <xsl:value-of select="$CCRActor/a:Telephone[0]/a:Value"/>
+                                                        </xsl:attribute>
+                                                    </telecom>
+                                                </xsl:if>
+                                                <assignedPerson>
+                                                    <name>
+                                                        <xsl:call-template name="actorName">
+                                                            <xsl:with-param name="objID" select="$PractitionerActorID"/>
+                                                        </xsl:call-template>
+                                                    </name>
+                                                </assignedPerson>
+                                            </assignedEntity>
+                                        </performer>
+                                    </xsl:if>
+                                </procedure>
+                            </entry>
+                        </xsl:for-each>
                     </xsl:if>
                     <xsl:if test="a:ContinuityOfCareRecord/a:Body/a:Medications">
                         <component>
                             <section>
-                                <templateId root="2.16.840.1.113883.10.20.1.8"/>
-                                <code code="10160-0" codeSystem="2.16.840.1.113883.6.1"/>
+                                <templateId root="2.16.840.1.113883.3.88.11.83.112" assigningAuthorityName="HITSP/C83"/>
+                                <templateId root="1.3.6.1.4.1.19376.1.5.3.1.3.19" assigningAuthorityName="IHE PCC"/>
+                                <templateId root="2.16.840.1.113883.10.20.1.8" assigningAuthorityName="HL7 CCD"/>
+                                <!--Medications section template-->
+                                <code code="10160-0" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="History of medication use"/>
                                 <title>Medications</title>
                                 <text>
@@ -566,8 +680,7 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Medications/a:Medication">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Medications/a:Medication">
                                                 <tr>
                                                     <td>
-
                                                         <xsl:value-of select="a:Product/a:ProductName/a:Text"/>
                                                         <xsl:if test="a:Product/a:BrandName">
@@ -662,4 +775,91 @@
                                     </table>
                                 </text>
+                                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Medications/a:Medication">
+                                    <entry typeCode="DRIV">
+                                        <substanceAdministration classCode="SBADM" moodCode="EVN">
+                                            <templateId root="2.16.840.1.113883.10.20.1.24" assigningAuthorityName="CCD"/>
+                                            <templateId root="1.3.6.1.4.1.19376.1.5.3.1.4.7.1" assigningAuthorityName="IHE PCC"/>
+                                            <!--Medication activity template -->
+                                            <id>
+                                                <xsl:attribute name="root">
+                                                    <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                </xsl:attribute>
+                                            </id>
+                                            <statusCode>
+                                                <xsl:attribute name="value">
+                                                    <xsl:value-of select="a:Status/a:Text"/>
+                                                </xsl:attribute>
+                                            </statusCode>
+                                            <xsl:call-template name="ccdDateTime">
+                                                <xsl:with-param name="dt" select="a:DateTime"/>
+                                                <xsl:with-param name="type" select="'IVL_TS'"/>
+                                            </xsl:call-template>
+                                            <xsl:call-template name="ccdMedicationFrequency">
+                                                <xsl:with-param name="frequency" select="a:Directions/a:Direction/a:Frequency"/>
+                                            </xsl:call-template>
+                                            <xsl:if test="a:Directions/a:Direction/a:Route">
+                                                <routeCode>
+                                                    <xsl:attribute name="code">
+                                                        <xsl:value-of select="a:Directions/a:Direction/a:Route/a:Code/a:Value"></xsl:value-of>
+                                                    </xsl:attribute>
+                                                    <xsl:attribute name="codeSystem">
+                                                        <xsl:call-template name="HL7OIDLookup">
+                                                            <xsl:with-param name="name" select="a:Directions/a:Direction/a:Route/a:Code/a:CodingSystem"/>
+                                                        </xsl:call-template>
+                                                    </xsl:attribute>
+                                                    <xsl:attribute name="displayName">
+                                                        <xsl:value-of select="a:Directions/a:Direction/a:Route/a:Text"></xsl:value-of>
+                                                    </xsl:attribute>
+                                                </routeCode>
+                                            </xsl:if>
+                                            <xsl:if test="a:Directions/a:Direction/a:Dose">
+                                                <doseQuantity>
+                                                    <low>
+                                                        <xsl:attribute name="value">
+                                                            <xsl:value-of select="a:Directions/a:Direction/a:Dose/a:Value"></xsl:value-of>
+                                                        </xsl:attribute>
+                                                        <xsl:attribute name="unit">
+                                                            <xsl:value-of select="a:Directions/a:Direction/a:Dose/a:Unit"></xsl:value-of>
+                                                        </xsl:attribute>
+                                                    </low>
+                                                    <high>
+                                                        <xsl:attribute name="value">
+                                                            <xsl:value-of select="a:Directions/a:Direction/a:Dose/a:Value"></xsl:value-of>
+                                                        </xsl:attribute>
+                                                        <xsl:attribute name="unit">
+                                                            <xsl:value-of select="a:Directions/a:Direction/a:Dose/a:Unit"></xsl:value-of>
+                                                        </xsl:attribute>
+                                                    </high>
+                                                </doseQuantity>
+                                            </xsl:if>
+                                            <consumable>
+                                                <manufacturedProduct>
+                                                    <templateId root="2.16.840.1.113883.3.88.11.83.8.2" assigningAuthorityName="HITSP C83"/>
+                                                    <templateId root="2.16.840.1.113883.10.20.1.53" assigningAuthorityName="CCD"/>
+                                                    <templateId root="1.3.6.1.4.1.19376.1.5.3.1.4.7.2" assigningAuthorityName="IHE PCC"/>
+                                                    <!-- Product template -->
+                                                    <manufacturedMaterial>
+                                                        <code>
+                                                            <xsl:attribute name="code">
+                                                                <xsl:value-of select="a:Product/a:ProductName/a:Code/a:Value"></xsl:value-of>
+                                                            </xsl:attribute>
+                                                            <xsl:attribute name="codeSystem">
+                                                                <xsl:call-template name="HL7OIDLookup">
+                                                                    <xsl:with-param name="name" select="a:Product/a:ProductName/a:Code/a:CodingSystem"/>
+                                                                </xsl:call-template>
+                                                            </xsl:attribute>
+                                                            <xsl:attribute name="displayName">
+                                                                <xsl:value-of select="a:Product/a:ProductName/a:Text"></xsl:value-of>
+                                                            </xsl:attribute>
+                                                        </code>
+                                                        <name>
+                                                            <xsl:value-of select="a:Product/a:BrandName/a:Text"></xsl:value-of>
+                                                        </name>
+                                                    </manufacturedMaterial>
+                                                </manufacturedProduct>
+                                            </consumable>
+                                        </substanceAdministration>
+                                    </entry>
+                                </xsl:for-each>
                             </section>
                         </component>
@@ -682,5 +882,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Immunizations/a:Immunization">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Immunizations/a:Immunization">
                                                 <tr>
                                                     <td>
@@ -735,5 +935,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:VitalSigns/a:Result">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:VitalSigns/a:Result">
                                                 <tr>
                                                     <td>
@@ -791,5 +991,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Encounters/a:Encounter">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Encounters/a:Encounter">
                                                 <tr>
                                                     <td>
@@ -864,5 +1064,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement">
                                                 <tr>
                                                     <td>
@@ -916,5 +1116,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:FamilyHistory/a:FamilyProblemHistory">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:FamilyHistory/a:FamilyProblemHistory">
                                                 <tr>
                                                     <td>
@@ -971,5 +1171,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Results/a:Result[a:Test/a:TestResult/a:Value!='']">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Results/a:Result[a:Test/a:TestResult/a:Value!='']">
                                                 <tr>
                                                     <td>
@@ -1033,5 +1233,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Payers/a:Payer">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Payers/a:Payer">
                                                 <tr>
                                                     <td>
@@ -1092,5 +1292,5 @@
                                                     <th>Source</th>
                                                 </tr>
-                                                <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:PlanOfCare/a:Plan[a:Type/a:Text='Treatment Recommendation']">
+                                                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:PlanOfCare/a:Plan[a:Type/a:Text='Treatment Recommendation']">
                                                     <tr>
                                                         <td>
@@ -1136,5 +1336,5 @@
                                                     <th>Source</th>
                                                 </tr>
-                                                <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:PlanOfCare/a:Plan[a:Type/a:Text='Order']">
+                                                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:PlanOfCare/a:Plan[a:Type/a:Text='Order']">
                                                     <tr>
                                                         <td>
@@ -1213,5 +1413,5 @@
                                                 <th>Name</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:HealthCareProviders/a:Provider">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:HealthCareProviders/a:Provider">
                                                 <tr>
                                                     <td>
@@ -1242,5 +1442,5 @@
                                                 <th>Name</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:Support/a:SupportProvider">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Support/a:SupportProvider">
                                                 <tr>
                                                     <td>
@@ -1274,5 +1474,5 @@
                                                 <th>Source</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:References/a:Reference">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:References/a:Reference">
                                                 <tr>
                                                     <td>
@@ -1322,5 +1522,5 @@
                                                 <th>Address/ E-mail</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Actors/a:Actor">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Actors/a:Actor">
                                                 <xsl:sort select="a:Person/a:Name/a:DisplayName|a:Person/a:Name/a:CurrentName/a:Family" data-type="text" order="ascending"/>
                                                 <xsl:if test="a:Person">
@@ -1367,5 +1567,6 @@
                                                                 </xsl:if>
                                                                 <xsl:if test="a:City">
-                                                                    <xsl:value-of select="a:City"/>,
+                                                                    <xsl:value-of select="a:City"/>
+                                                                    <xsl:text>, </xsl:text>
                                                                 </xsl:if>
                                                                 <xsl:value-of select="a:State"/>
@@ -1395,5 +1596,5 @@
                                                 <th>Address/ E-mail</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Actors/a:Actor">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Actors/a:Actor">
                                                 <xsl:sort select="a:Organization/a:Name" data-type="text" order="ascending"/>
                                                 <xsl:if test="a:Organization">
@@ -1438,5 +1639,6 @@
                                                                 </xsl:if>
                                                                 <xsl:if test="a:City">
-                                                                    <xsl:value-of select="a:City"/>,
+                                                                    <xsl:value-of select="a:City"/>
+                                                                    <xsl:text>, </xsl:text>
                                                                 </xsl:if>
                                                                 <xsl:value-of select="a:State"/>
@@ -1466,5 +1668,5 @@
                                                 <th>Address/ E-mail</th>
                                             </tr>
-                                            <xsl:for-each select="a:ContinuityOfCareRecord/a:Actors/a:Actor">
+                                            <xsl:for-each select="/a:ContinuityOfCareRecord/a:Actors/a:Actor">
                                                 <xsl:sort select="a:InformationSystem/a:Name" data-type="text" order="ascending"/>
                                                 <xsl:if test="a:InformationSystem">
@@ -1509,5 +1711,6 @@
                                                                 </xsl:if>
                                                                 <xsl:if test="a:City">
-                                                                    <xsl:value-of select="a:City"/>,
+                                                                    <xsl:value-of select="a:City"/>
+                                                                    <xsl:text>, </xsl:text>
                                                                 </xsl:if>
                                                                 <xsl:value-of select="a:State"/>
