Index: ccr2ccd-xslt/trunk/ccr_ccd.xsl
===================================================================
--- ccr2ccd-xslt/trunk/ccr_ccd.xsl	(revision 757)
+++ ccr2ccd-xslt/trunk/ccr_ccd.xsl	(revision 766)
@@ -1,3 +1,3 @@
-<?xml version="1.0" encoding="utf-8"?>
+﻿<?xml version="1.0" encoding="utf-8"?>
 <!--
 Conversion of CCR to Level 3 CCD
@@ -9,5 +9,5 @@
 Contributors:
 Richard Braman, EHR Doctors, Inc rbraman@ehrdoctors.com
-George Gilly (WorldVistA glilly@glilly.net)
+George Lilly (WorldVistA glilly@glilly.net)
 xxxx - Oroville Hospital
 
@@ -31,20 +31,16 @@
 
 -->
-<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:stylesheet version="1.0" xmlns="urn:hl7-org:v3" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="urn:astm-org:CCR" xmlns:date="http://exslt.org/dates-and-times"
+xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="a date">
+    <xsl:import href="./templates/cdavocablookup.xsl"/>
+    <xsl:import href="./templates/hl7oidlookup.xsl"/>
     <xsl:import href="./templates/code.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/datetime.xsl"/>
     <xsl:import href="./templates/problemDescription.xsl"/>
-    <xsl:import href="./templates/HL7OIDLookup.xsl"/>
 
     <xsl:output method="xml" encoding="utf-8" version="1.0" indent="yes"/>
     <xsl:template match="/">
-        <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">
+        <ClinicalDocument xmlns="urn:hl7-org:v3" 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"/>
@@ -104,13 +100,15 @@
             </author>
 
-            <custodian>
-                <assignedCustodian>
-                    <representedCustodianOrganization>
-                        <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>
+            <xsl:if test="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$fromID]/a:InternalCCRLink[a:LinkRelationship='Organization']">
+                <custodian>
+                    <assignedCustodian>
+                        <representedCustodianOrganization>
+                            <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>
+            </xsl:if>
 
             <documentationOf>
@@ -533,125 +531,70 @@
                                     </table>
                                 </text>
+
+                                <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: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> -->
+                                            <xsl:call-template name="ccdCodedValue">
+                                                <xsl:with-param name="ccrCodedDescription" select="a:Description"/>
+                                                <xsl:with-param name="nodeName" select="'code'"/>
+                                            </xsl:call-template>
+
+                                            <text>
+                                                <reference>
+                                                    <xsl:attribute name="value">
+                                                        <xsl:text>#</xsl:text>
+                                                        <xsl:value-of select="a:CCRDataObjectID"/>
+                                                    </xsl:attribute>
+                                                </reference>
+                                            </text>
+
+                                            <xsl:call-template name="ccdStatusProcedure">
+                                                <xsl:with-param name="status" select="a:Status"></xsl:with-param>
+                                            </xsl:call-template>
+
+                                            <xsl:call-template name="ccdDateTime">
+                                                <xsl:with-param name="dt" select="a:DateTime"/>
+                                            </xsl:call-template>
+
+                                            <xsl:if test="a:Method">
+                                                <xsl:call-template name="ccdCodedValue">
+                                                    <xsl:with-param name="ccrCodedDescription" select="a:Method"/>
+                                                    <xsl:with-param name="nodeName" select="'approachSiteCode'"/>
+                                                </xsl:call-template>
+                                            </xsl:if>
+
+                                            <xsl:if test="a:Site">
+                                                <xsl:call-template name="ccdCodedValue">
+                                                    <xsl:with-param name="ccrCodedDescription" select="a:Site"/>
+                                                    <xsl:with-param name="nodeName" select="'targetSiteCode'"/>
+                                                </xsl:call-template>
+                                            </xsl:if>
+
+                                            <xsl:if test="a:Practitioners/a:Practitioner">
+                                                <performer typeCode="PRF">
+                                                    <assignedEntity>
+                                                        <xsl:call-template name="ccdEntity">
+                                                            <xsl:with-param name="ccrActorObjectID" select="a:Practitioners/a:Practitioner[1]/a:ActorID"/>
+                                                        </xsl:call-template>
+                                                    </assignedEntity>
+                                                </performer>
+                                            </xsl:if>
+                                        </procedure>
+                                    </entry>
+                                </xsl:for-each>
                             </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">
@@ -780,37 +723,31 @@
                                             <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">
+
+                                            <!-- <id> -->
+                                            <xsl:call-template name="ccdID">
+                                                <xsl:with-param name="ccrObjectID">
                                                     <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:with-param>
+                                            </xsl:call-template>
+
+                                            <statusCode code='completed'/>
+
                                             <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:call-template name="ccdCodedValue">
+                                                <xsl:with-param name="ccrCodedDescription" select="a:Directions/a:Direction/a:Route"/>
+                                                <xsl:with-param name="nodeName" select="'routeCode'"/>
+                                                <xsl:with-param name="domain" select="'RouteOfAdministration'"/>
+                                            </xsl:call-template>
+
                                             <xsl:if test="a:Directions/a:Direction/a:Dose">
                                                 <doseQuantity>
@@ -838,19 +775,13 @@
                                                     <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>
+                                                        <xsl:call-template name="ccdCodedValue">
+                                                            <xsl:with-param name="ccrCodedDescription" select="a:Product/a:ProductName"/>
+                                                            <xsl:with-param name="nodeName" select="'code'"/>
+                                                            <xsl:with-param name="domain" select="'RouteOfAdministration'"/>
+                                                        </xsl:call-template>
                                                         <name>
                                                             <xsl:value-of select="a:Product/a:BrandName/a:Text"></xsl:value-of>
@@ -1171,6 +1102,9 @@
                                                 <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">
                                                 <tr>
+                                                    <xsl:attribute name="id">
+                                                        <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                    </xsl:attribute>
                                                     <td>
                                                         <xsl:value-of select="a:Description/a:Text"/>
@@ -1185,20 +1119,32 @@
                                                     <td>
                                                         <xsl:for-each select="a:Test[a:TestResult/a:Value!='']">
-                                                            <xsl:value-of select="a:Description/a:Text"/>
-                                                            <xsl:text xml:space="preserve"> </xsl:text>
-                                                            <xsl:value-of select="a:TestResult/a:Value"/>
-                                                            <xsl:text xml:space="preserve"> </xsl:text>
-                                                            <xsl:value-of select="a:TestResult/a:Units/a:Unit"/>
-                                                            <xsl:text xml:space="preserve"> </xsl:text>
-                                                            <xsl:value-of select="a:Flag/a:Text"/>
-                                                            <br/>
+                                                            <div>
+                                                                <xsl:attribute name="id">
+                                                                    <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                                </xsl:attribute>
+
+                                                                <xsl:value-of select="a:Description/a:Text"/>
+                                                                <xsl:text xml:space="preserve"> </xsl:text>
+                                                                <xsl:value-of select="a:TestResult/a:Value"/>
+                                                                <xsl:text xml:space="preserve"> </xsl:text>
+                                                                <xsl:value-of select="a:TestResult/a:Units/a:Unit"/>
+                                                                <xsl:text xml:space="preserve"> </xsl:text>
+                                                                <xsl:value-of select="a:Flag/a:Text"/>
+                                                                <br/>
+                                                            </div>
                                                         </xsl:for-each>
                                                         <xsl:for-each select="a:Test[a:TestResult/a:Description/a:Text!='']">
-                                                            <xsl:value-of select="a:Description/a:Text"/>
-                                                            <xsl:text xml:space="preserve"> </xsl:text>
-                                                            <xsl:value-of select="a:TestResult/a:Description/a:Text" disable-output-escaping="yes"/>
-                                                            <xsl:text xml:space="preserve"> </xsl:text>
-                                                            <xsl:value-of select="a:Flag/a:Text"/>
-                                                            <br/>
+                                                            <div>
+                                                                <xsl:attribute name="id">
+                                                                    <xsl:value-of select="a:CCRDataObjectID"></xsl:value-of>
+                                                                </xsl:attribute>
+
+                                                                <xsl:value-of select="a:Description/a:Text"/>
+                                                                <xsl:text xml:space="preserve"> </xsl:text>
+                                                                <xsl:value-of select="a:TestResult/a:Description/a:Text" disable-output-escaping="yes"/>
+                                                                <xsl:text xml:space="preserve"> </xsl:text>
+                                                                <xsl:value-of select="a:Flag/a:Text"/>
+                                                                <br/>
+                                                            </div>
                                                         </xsl:for-each>
                                                     </td>
@@ -1213,4 +1159,64 @@
                                     </table>
                                 </text>
+                                <xsl:for-each select="/a:ContinuityOfCareRecord/a:Body/a:Results/a:Result">
+                                    <entry typeCode="DRIV">
+                                        <xsl:variable name="testDate" select="a:DateTime"/>
+
+                                        <xsl:choose>
+                                            <xsl:when test="count(a:Test)>1">
+                                                <organizer classCode="BATTERY" moodCode="EVN">
+                                                    <templateId root="2.16.840.1.113883.10.20.1.32"/>
+                                                    <!--Result organizer template -->
+
+                                                    <!-- <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> -->
+                                                    <xsl:call-template name="ccdCodedValue">
+                                                        <xsl:with-param name="ccrCodedDescription" select="a:Description"/>
+                                                        <xsl:with-param name="nodeName" select="'code'"/>
+                                                    </xsl:call-template>
+
+                                                    <statusCode code="completed"/>
+
+                                                    <!-- <effectiveTime> -->
+                                                    <xsl:call-template name="ccdDateTime">
+                                                        <xsl:with-param name="dt" select="$testDate"/>
+                                                    </xsl:call-template>
+
+                                                    <performer>
+                                                        <assignedEntity>
+                                                            <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>
+
+                                                    <xsl:for-each select="a:Test">
+                                                        <xsl:call-template name="labObservation">
+                                                            <xsl:with-param name="ccrTestNode" select="."/>
+                                                            <xsl:with-param name="testDate" select="$testDate"/>
+                                                        </xsl:call-template>
+                                                    </xsl:for-each>
+
+                                                </organizer>
+
+                                            </xsl:when>
+                                            <xsl:otherwise>
+                                                <xsl:call-template name="labObservation">
+                                                    <xsl:with-param name="ccrTestNode" select="a:Test[1]"/>
+                                                    <xsl:with-param name="testDate" select="$testDate"/>
+                                                </xsl:call-template>
+
+                                            </xsl:otherwise>
+                                        </xsl:choose>
+                                    </entry>
+                                </xsl:for-each>
                             </section>
                         </component>
@@ -1736,3 +1742,549 @@
         </ClinicalDocument>
     </xsl:template>
+
+    <xsl:template name="ccdEntity">
+        <xsl:param name="ccrActorObjectID"/>
+        <xsl:param name="personNodeName">assignedPerson</xsl:param>
+
+        <xsl:variable name="CCRActor" select="//a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$ccrActorObjectID]"/>
+
+        <id>
+            <xsl:choose>
+                <xsl:when test="$CCRActor/a:IDs">
+                    <xsl:attribute name="extension">
+                        <xsl:value-of select="$CCRActor/a:IDs[1]/a:ID"/>
+                    </xsl:attribute>
+                    <xsl:attribute name="root">
+                        <xsl:value-of select="$CCRActor/a:IDs[1]/a:Type/a:Text"/>
+                    </xsl:attribute>
+                </xsl:when>
+                <xsl:otherwise>
+                    <xsl:attribute name="extension">
+                        <xsl:value-of select="$CCRActor/a:ActorObjectID"/>
+                    </xsl:attribute>
+                    <xsl:attribute name="root">
+                        <xsl:text>CCR Actor ID</xsl:text>
+                    </xsl:attribute>
+                </xsl:otherwise>
+            </xsl:choose>
+        </id>
+
+        <xsl:if test="$CCRActor/a:Organization">
+            <xsl:value-of select="$CCRActor/a:Organization/a:Name"></xsl:value-of>
+        </xsl:if>
+
+        <addr>
+            <xsl:choose>
+                <xsl:when test="$CCRActor/a:Address">
+                    <xsl:attribute name="use">
+                        <xsl:call-template name="CDAVocabularyLookup">
+                            <xsl:with-param name="domain" select="'telecommunicationsAddressUse'"/>
+                            <xsl:with-param name="ccrDescription" select="$CCRActor/a:Address/a:Type/a:Text"/>
+                        </xsl:call-template>
+                    </xsl:attribute>
+                    <streetAddressLine>
+                        <xsl:value-of select="$CCRActor/a:Address[1]/a:Line1"/>
+                    </streetAddressLine>
+                    <city>
+                        <xsl:value-of select="$CCRActor/a:Address[1]/a:City"/>
+                    </city>
+                    <state>
+                        <xsl:value-of select="$CCRActor/a:Address[1]/a:State"/>
+                    </state>
+                    <postalcode>
+                        <xsl:value-of select="$CCRActor/a:Address[1]/a:PostalCode"/>
+                    </postalcode>
+                </xsl:when>
+                <xsl:otherwise>
+                    <streetAddressLine/>
+                </xsl:otherwise>
+            </xsl:choose>
+        </addr>
+
+        <telecom>
+            <xsl:if test="$CCRActor/a:Telephone">
+                <xsl:attribute name="use">
+                    <xsl:call-template name="CDAVocabularyLookup">
+                        <xsl:with-param name="domain" select="'telecommunicationsAddressUse'"/>
+                        <xsl:with-param name="ccrDescription" select="$CCRActor/a:Telephone/a:Type/a:Text"/>
+                    </xsl:call-template>
+                </xsl:attribute>
+                <xsl:attribute name="value">
+                    <xsl:text>tel:+1-</xsl:text>
+                    <xsl:value-of select="$CCRActor/a:Telephone[1]/a:Value"/>
+                </xsl:attribute>
+            </xsl:if>
+
+        </telecom>
+
+        <xsl:if test="$CCRActor/a:Email">
+            <telecom>
+                <xsl:choose>
+                    <xsl:when test="$CCRActor/a:Telephone">
+                        <xsl:attribute name="use">
+                            <xsl:call-template name="CDAVocabularyLookup">
+                                <xsl:with-param name="domain" select="'telecommunicationsAddressUse'"/>
+                                <xsl:with-param name="ccrDescription" select="$CCRActor/a:Telephone/a:Type/a:Text"/>
+                            </xsl:call-template>
+                        </xsl:attribute>
+                        <xsl:attribute name="value">
+                            <xsl:text>mailto:</xsl:text>
+                            <xsl:value-of select="$CCRActor/a:Email[1]/a:Value"/>
+                        </xsl:attribute>
+                    </xsl:when>
+                    <xsl:otherwise>
+                        <xsl:attribute name="value">Unknown</xsl:attribute>
+                    </xsl:otherwise>
+                </xsl:choose>
+            </telecom>
+        </xsl:if>
+
+        <xsl:if test="a:Person">
+            <xsl:element name="{$personNodeName}">
+                <name>
+                    <xsl:if test="a:Person/a:Name/a:CurrentName/a:Title">
+                        <prefix>
+                            <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Title"></xsl:value-of>
+                        </prefix>
+                    </xsl:if>
+                    <xsl:if test="a:Person/a:Name/a:CurrentName/a:Given">
+                        <given>
+                            <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Given"></xsl:value-of>
+                        </given>
+                    </xsl:if>
+                    <xsl:if test="a:Person/a:Name/a:CurrentName/a:Middle">
+                        <given>
+                            <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Middle"></xsl:value-of>
+                        </given>
+                    </xsl:if>
+                    <xsl:if test="a:Person/a:Name/a:CurrentName/a:Family">
+                        <family>
+                            <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Family"></xsl:value-of>
+                        </family>
+                    </xsl:if>
+                    <xsl:if test="a:Person/a:Name/a:CurrentName/a:Suffix">
+                        <suffix>
+                            <xsl:value-of select="a:Person/a:Name/a:CurrentName/a:Suffix"></xsl:value-of>
+                        </suffix>
+                    </xsl:if>
+                </name>
+
+                <xsl:if test="a:Person/a:Gender">
+
+                    <xsl:call-template name="ccdCodedValue">
+                        <xsl:with-param name="ccrCodedDescription" select="a:Person/a:Gender"/>
+                        <xsl:with-param name="domain" select="'AdministrativeGender'"/>
+                        <xsl:with-param name="nodeName" select="'administrativeGenderCode'"/>
+                    </xsl:call-template>
+                </xsl:if>
+
+                <xsl:if test="a:Person/a:DateOfBirth">
+                    <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>
+                    </birthTime>
+                </xsl:if>
+            </xsl:element>
+        </xsl:if>
+
+        <xsl:if test="$ccrActorObjectID=/a:ContinuityOfCareRecord/a:Patient[1]/a:ActorID">
+            <xsl:if test="/a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement[a:Type/a:Text='Marital Status']">
+
+                <xsl:call-template name="ccdCodedValue">
+                    <xsl:with-param name="ccrCodedDescription" select="/a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement[a:Type/a:Text='Marital Status']/a:Description"></xsl:with-param>
+                    <xsl:with-param name="domain" select="'MaritalStatus'"/>
+                    <xsl:with-param name="nodeName" select="'maritalStatusCode'"/>
+                </xsl:call-template>
+
+            </xsl:if>
+
+            <xsl:if test="/a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement[a:Type/a:Text='Language']">
+                <languageCommunication>
+                    <templateId root="2.16.840.1.113883.3.88.11.83.2" assigningAuthorityName="HITSP/C83"/>
+                    <templateId root="1.3.6.1.4.1.19376.1.5.3.1.2.1" assigningAuthorityName="IHE/PCC"/>
+
+                    <xsl:call-template name="ccdCodedValue">
+                        <xsl:with-param name="ccrCodedDescription" select="/a:ContinuityOfCareRecord/a:Body/a:SocialHistory/a:SocialHistoryElement[a:Type/a:Text='Language']/a:Description"></xsl:with-param>
+                        <xsl:with-param name="domain" select="'HumanLanguage'"/>
+                        <xsl:with-param name="nodeName" select="'languageCode'"/>
+                    </xsl:call-template>
+
+                </languageCommunication>
+            </xsl:if>
+        </xsl:if>
+
+        <xsl:if test="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$ccrActorObjectID]/a:InternalCCRLink[a:LinkRelationship='Organization']">
+            <representedOrganization>
+                <xsl:call-template name="ccdEntity">
+                    <xsl:with-param name="ccrActorObjectID" select="/a:ContinuityOfCareRecord/a:Actors/a:Actor[a:ActorObjectID=$ccrActorObjectID]/a:InternalCCRLink[a:LinkRelationship='Organization']/a:LinkID"></xsl:with-param>
+                </xsl:call-template>
+            </representedOrganization>
+        </xsl:if>
+
+    </xsl:template>
+
+    <xsl:template name="ccdID">
+        <xsl:param name="ccrObjectID"/>
+        <xsl:param name="suffix"></xsl:param>
+
+        <id>
+            <xsl:attribute name="root">
+                <xsl:value-of select="$ccrObjectID"></xsl:value-of>
+                <xsl:value-of select="$suffix"></xsl:value-of>
+            </xsl:attribute>
+            <xsl:attribute name="extension">CCR Object ID</xsl:attribute>
+        </id>
+    </xsl:template>
+
+    <xsl:template name="ccdCodedValue">
+        <xsl:param name="ccrCodedDescription"/>
+        <xsl:param name="type"/>
+        <xsl:param name="domain"/>
+        <xsl:param name="nodeName">value</xsl:param>
+
+        <xsl:element name="{$nodeName}">
+            <xsl:if test="$type">
+                <xsl:attribute name="xsi:type">
+                    <xsl:value-of select="$type"/>
+                </xsl:attribute>
+            </xsl:if>
+            <xsl:choose>
+                <xsl:when test="$ccrCodedDescription/a:Code">
+                    <xsl:attribute name="displayName">
+                        <xsl:value-of select="$ccrCodedDescription/a:Text"/>
+                    </xsl:attribute>
+                    <xsl:attribute name="code">
+                        <xsl:value-of select="$ccrCodedDescription/a:Code/a:Value"/>
+                    </xsl:attribute>
+                    <xsl:attribute name="codeSystemName">
+                        <xsl:value-of select="$ccrCodedDescription/a:Code/a:CodingSystem"/>
+                    </xsl:attribute>
+                    <xsl:attribute name="codeSystem">
+                        <xsl:call-template name="HL7OIDLookup">
+                            <xsl:with-param name="name" select="$ccrCodedDescription/a:Code/a:CodingSystem"/>
+                        </xsl:call-template>
+                    </xsl:attribute>
+                </xsl:when>
+                <xsl:when test="$domain">
+                    <xsl:variable name="cdaCode">
+                        <xsl:call-template name="CDAVocabularyLookup">
+                            <xsl:with-param name="domain" select="$domain"/>
+                            <xsl:with-param name="ccrtext" select="$ccrCodedDescription/a:Text"/>
+                        </xsl:call-template>
+                    </xsl:variable>
+                    <xsl:attribute name="displayName">
+                        <xsl:call-template name="CDADisplayNameLookup">
+                            <xsl:with-param name="domain" select="$domain"/>
+                            <xsl:with-param name="cdacode" select="$cdaCode"/>
+                        </xsl:call-template>
+                    </xsl:attribute>
+                    <xsl:attribute name="code">
+                        <xsl:value-of select="$cdaCode"></xsl:value-of>
+                    </xsl:attribute>
+                    <xsl:attribute name="codeSystemName">
+                        <xsl:call-template name="CDAVocabularyCodeSystemNameLookup">
+                            <xsl:with-param name="domain" select="$domain"/>
+                        </xsl:call-template>
+                    </xsl:attribute>
+                    <xsl:attribute name="codeSystemName">
+                        <xsl:call-template name="CDAVocabularyCodeSystemLookup">
+                            <xsl:with-param name="domain" select="$domain"/>
+                        </xsl:call-template>
+                    </xsl:attribute>
+                </xsl:when>
+                <xsl:otherwise>
+                    <xsl:attribute name="displayName">
+                        <xsl:value-of select="$ccrCodedDescription/a:Text"/>
+                    </xsl:attribute>
+                </xsl:otherwise>
+            </xsl:choose>
+        </xsl:element>
+
+    </xsl:template>
+
+    <xsl:template name="ccdDateTime">
+        <xsl:param name="dt"/>
+        <xsl:param name="type"/>
+        <xsl:if test="$dt">
+            <xsl:if test="$dt[1]/a:ExactDateTime">
+                <effectiveTime>
+                    <xsl:if test="$type">
+                        <xsl:attribute name="xsi:type">
+                            <xsl:value-of select="$type"></xsl:value-of>
+                        </xsl:attribute>
+                    </xsl:if>
+                    <low>
+                        <xsl:attribute name="value">
+                            <xsl:call-template name="date:format-date">
+                                <xsl:with-param name="date-time">
+                                    <xsl:value-of select="$dt[1]/a:ExactDateTime"/>
+                                </xsl:with-param>
+                                <xsl:with-param name="pattern">yyyyMMdd</xsl:with-param>
+                            </xsl:call-template>
+                        </xsl:attribute>
+                    </low>
+                    <high nullFlavor="UNK"/>
+                </effectiveTime>
+            </xsl:if>
+            <xsl:if test="$dt[1]/a:DateTimeRange">
+                <effectiveTime>
+                    <low>
+                        <xsl:attribute name="value">
+                            <xsl:call-template name="date:format-date">
+                                <xsl:with-param name="date-time">
+                                    <xsl:value-of select="$dt[1]/a:DateTimeRange/a:BeginRange/a:ExactDateTime"/>
+                                </xsl:with-param>
+                                <xsl:with-param name="pattern">yyyyMMdd</xsl:with-param>
+                            </xsl:call-template>
+                        </xsl:attribute>
+                    </low>
+                    <high>
+                        <xsl:attribute name="value">
+                            <xsl:call-template name="date:format-date">
+                                <xsl:with-param name="date-time">
+                                    <xsl:value-of select="$dt[1]/a:DateTimeRange/a:EndRange/a:ExactDateTime"/>
+                                </xsl:with-param>
+                                <xsl:with-param name="pattern">yyyyMMdd</xsl:with-param>
+                            </xsl:call-template>
+                        </xsl:attribute>
+                    </high>
+                </effectiveTime>
+            </xsl:if>
+        </xsl:if>
+    </xsl:template>
+
+    <xsl:template name="ccdMedicationFrequency">
+        <xsl:param name="frequency"/>
+        <xsl:if test="$frequency">
+            <xsl:choose>
+                <xsl:when test="$frequency/a:Value='qd'">
+                    <effectiveTime xsi:type="PIVL_TS" operator="A">
+                        <xsl:attribute name="institutionSpecified">true</xsl:attribute>
+                        <period>
+                            <xsl:attribute name="value">24</xsl:attribute>
+                            <xsl:attribute name="unit">h</xsl:attribute>
+                        </period>
+                    </effectiveTime>
+                </xsl:when>
+                <xsl:when test="$frequency/a:Value='bid'">
+                    <effectiveTime xsi:type="PIVL_TS" operator="A">
+                        <xsl:attribute name="institutionSpecified">true</xsl:attribute>
+                        <period>
+                            <xsl:attribute name="value">12</xsl:attribute>
+                            <xsl:attribute name="unit">h</xsl:attribute>
+                        </period>
+                    </effectiveTime>
+                </xsl:when>
+                <xsl:when test="$frequency/a:Value='tid'">
+                    <effectiveTime xsi:type="PIVL_TS" operator="A">
+                        <xsl:attribute name="institutionSpecified">true</xsl:attribute>
+                        <period>
+                            <xsl:attribute name="value">8</xsl:attribute>
+                            <xsl:attribute name="unit">h</xsl:attribute>
+                        </period>
+                    </effectiveTime>
+                </xsl:when>
+                <xsl:when test="$frequency/a:Value='qid'">
+                    <effectiveTime xsi:type="PIVL_TS" operator="A">
+                        <xsl:attribute name="institutionSpecified">true</xsl:attribute>
+                        <period>
+                            <xsl:attribute name="value">6</xsl:attribute>
+                            <xsl:attribute name="unit">h</xsl:attribute>
+                        </period>
+                    </effectiveTime>
+                </xsl:when>
+                <xsl:when test="$frequency/a:Value='qam'">
+                    <effectiveTime xsi:type='EIVL' operator='A'>
+                        <event code='ACM'/>
+                    </effectiveTime>
+                </xsl:when>
+                <xsl:when test="$frequency/a:Value='qpm'">
+                    <effectiveTime xsi:type='EIVL' operator='A'>
+                        <event code='PCV'/>
+                    </effectiveTime>
+                </xsl:when>
+            </xsl:choose>
+        </xsl:if>
+    </xsl:template>
+
+    <xsl:template name="ccdStatus">
+        <xsl:param name="ccrStatus"/>
+
+        <entryRelationship typeCode="REFR">
+            <observation classCode="OBS" moodCode="EVN">
+                <templateId root="2.16.840.1.113883.10.20.1.50"/>
+                <!-- Problem status observation template -->
+                <code code="33999-4" codeSystem="2.16.840.1.113883.6.1" displayName="Status"/>
+                <statusCode code="completed"/>
+                <xsl:call-template name="ccdCodedValue">
+                    <xsl:with-param name="ccrCodedDescription" select="$ccrStatus"/>
+                </xsl:call-template>
+            </observation>
+        </entryRelationship>
+    </xsl:template>
+
+    <xsl:template name="ccdStatusObservation">
+        <xsl:param name="status"/>
+        <xsl:variable name="statusText" select="$status/a:Text"></xsl:variable>
+        <xsl:choose>
+            <xsl:when test="$statusText='Active'">
+                <xsl:call-template name="ccdStatusElement">
+                    <xsl:with-param name="statusCode" select="'55561003'"></xsl:with-param>
+                    <xsl:with-param name="statusDisplayName" select="$statusText"></xsl:with-param>
+                </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="$statusText='Inactive'">
+                <xsl:call-template name="ccdStatusElement">
+                    <xsl:with-param name="statusCode" select="'73425007'"></xsl:with-param>
+                    <xsl:with-param name="statusDisplayName" select="$statusText"></xsl:with-param>
+                </xsl:call-template>
+            </xsl:when>
+            <xsl:when test="$statusText='Chronic'">
+                <xsl:call-template name="ccdStatusElement">
+                    <xsl:with-param name="statusCode" select="'90734009'"></xsl:with-param>
+                    <xsl:with-param name="statusDisplayName" select="$statusText"></xsl:with-param>
+                </xsl:call-template>
+            </xsl:when>
+        </xsl:choose>
+    </xsl:template>
+
+    <xsl:template name="ccdStatusProcedure">
+        <xsl:param name="status"/>
+        <xsl:variable name="statusText" select="$status/a:Text"></xsl:variable>
+        <xsl:choose>
+            <xsl:when test="$statusText='Completed'">
+                <statusCode code="completed"/>
+            </xsl:when>
+            <xsl:when test="$statusText='Active'">
+                <statusCode code="active"/>
+            </xsl:when>
+            <xsl:when test="$statusText='Aborted'">
+                <statusCode code="aborted"/>
+            </xsl:when>
+            <xsl:when test="$statusText='Cancelled' or $statusText='Canceled'">
+                <statusCode code="cancelled"/>
+            </xsl:when>
+            <xsl:otherwise>
+                <statusCode>
+                    <xsl:attribute name="code">
+                        <xsl:value-of select="$statusText"></xsl:value-of>
+                    </xsl:attribute>
+                </statusCode>
+            </xsl:otherwise>
+        </xsl:choose>
+    </xsl:template>
+
+    <xsl:template name="ccdStatusElement">
+        <xsl:param name="statusCode"/>
+        <xsl:param name="statusDisplayName"/>
+        <value>
+            <xsl:attribute name="xsi:type">CE</xsl:attribute>
+            <xsl:attribute name="code">
+                <xsl:value-of select="$statusCode"></xsl:value-of>
+            </xsl:attribute>
+            <xsl:attribute name="codeSystem">2.16.840.1.113883.6.96</xsl:attribute>
+            <xsl:attribute name="displayName">
+                <xsl:value-of select="$statusDisplayName"></xsl:value-of>
+            </xsl:attribute>
+        </value>
+    </xsl:template>
+
+    <xsl:template name="labObservation">
+        <xsl:param name="ccrTestNode"/>
+        <xsl:param name="testDate"/>
+
+        <component>
+            <observation classCode="OBS" moodCode="EVN">
+                <templateId root="2.16.840.1.113883.3.88.11.83.15" assigningAuthorityName="HITSP C83"/>
+                <templateId root="2.16.840.1.113883.10.20.1.31" assigningAuthorityName="CCD"/>
+                <templateId root="1.3.6.1.4.1.19376.1.5.3.1.4.13" assigningAuthorityName="IHE PCC"/>
+                <!-- Result observation template -->
+
+                <xsl:call-template name="ccdID">
+                    <xsl:with-param name="ccrObjectID">
+                        <xsl:value-of select="$ccrTestNode/a:CCRDataObjectID"></xsl:value-of>
+                    </xsl:with-param>
+                </xsl:call-template>
+
+                <!-- <code> -->
+                <xsl:call-template name="ccdCodedValue">
+                    <xsl:with-param name="ccrCodedDescription" select="$ccrTestNode/a:Description"/>
+                    <xsl:with-param name="nodeName" select="'code'"/>
+                </xsl:call-template>
+
+                <text>
+                    <reference>
+                        <xsl:attribute name="value">
+                            <xsl:text>#</xsl:text>
+                            <xsl:value-of select="$ccrTestNode/a:CCRDataObjectID"/>
+                        </xsl:attribute>
+                    </reference>
+                </text>
+
+                <statusCode code="completed"/>
+
+                <!-- <effectiveTime> -->
+                <xsl:choose>
+                    <xsl:when test="$ccrTestNode/a:DateTime">
+                        <xsl:call-template name="ccdDateTime">
+                            <xsl:with-param name="dt" select="$ccrTestNode/a:DateTime"/>
+                        </xsl:call-template>
+                    </xsl:when>
+                    <xsl:otherwise>
+                        <xsl:call-template name="ccdDateTime">
+                            <xsl:with-param name="dt" select="$testDate"/>
+                        </xsl:call-template>
+                    </xsl:otherwise>
+                </xsl:choose>
+
+                <xsl:choose>
+                    <xsl:when test="$ccrTestNode/a:TestResult[a:Value!='']">
+                        <value xsi:type="PQ">
+                            <xsl:attribute name="value">
+                                <xsl:value-of select="$ccrTestNode/a:TestResult/a:Value"/>
+                            </xsl:attribute>
+                            <xsl:attribute name="unit">
+                                <xsl:value-of select="$ccrTestNode/a:TestResult/a:Units/a:Unit"/>
+                            </xsl:attribute>
+                        </value>
+                    </xsl:when>
+                    <xsl:when test="$ccrTestNode/a:TestResult[a:Description/a:Text!='']">
+                        <value xsi:type="TX">
+                            <xsl:attribute name="value">
+                                <xsl:value-of select="$ccrTestNode/a:TestResult/a:Description/a:Text"/>
+                            </xsl:attribute>
+                        </value>
+                    </xsl:when>
+                </xsl:choose>
+
+                <xsl:if test="$ccrTestNode/a:Flag">
+                    <xsl:call-template name="ccdCodedValue">
+                        <xsl:with-param name="ccrCodedDescription" select="$ccrTestNode/a:Flag"/>
+                        <xsl:with-param name="nodeName" select="'interpretationCode'"/>
+                        <xsl:with-param name="domain" select="'ObservationInterpretation'"/>
+                    </xsl:call-template>
+                </xsl:if>
+
+                <xsl:if test="$ccrTestNode/a:NormalResult">
+                    <referenceRange>
+                        <xsl:if test="$ccrTestNode/a:NormalResult/a:Normal/a:Description/a:Text">
+                            <observationRange>
+                                <text>
+                                    <xsl:value-of select="$ccrTestNode/a:NormalResult/a:Normal/a:Description/a:Text"/>
+                                </text>
+                            </observationRange>
+                        </xsl:if>
+                    </referenceRange>
+                </xsl:if>
+
+            </observation>
+        </component>
+
+    </xsl:template>
 </xsl:stylesheet>
