<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


	<!-- This stylesheet tranforms ODM files generated under the ODMDraftv1-1(nx) DTD
	     to files according to the odm1-1-0 DTD (the "final" version).
	     Using this stylesheet about 95% of the data are transformed. No more could be established
	     as in the "final" version new elements and attributes were created for which there was 
	     no counterpart in the draft version.	-->
	<!-- Therefore it is the users responsibility to check the output of the transformation, 
	     and to make changes by hand, if needed -->
	<!-- Computer Chemistry Consultancy GmbH is not responsible for any damage that may occur due to the 
	     use of this stylesheet, be it direct or indirect -->


	<!-- Remarks:
		* if an attribute is IMPLIED in the "final" DTD, a test is performed whether there 
		is a counterpart in the file which conforms to the draft version. If not, the new attribute 
		is not created.
		* if an attribute is REQUIRED in the "final" DTD, a new attribute is always created, 
		whether there is a counterpart in the file that conforms to the draft version or not.
		This may lead to an empty attribute, like for example 'CreationDateTime="" '. -->

	<!-- start at the root "/" -->
	<!-- Still to do: encoding="utf-8" is generated, should be "ISO-8859-1" -->
	<xsl:template match="/">
		<xsl:copy><xsl:apply-templates select="@*"/></xsl:copy>
		<xsl:apply-templates/>
	</xsl:template>

	<!-- ODM element: change TransferType attribute into FileType attribute -->
	<!-- Archival attribute is new, as it is IMPLIED, we do not generate it -->
	<!-- AsOfDateTime is new, as it is IMPLIED, we do not generate it -->
	<!-- Order has been changed, CreationDate comes before PriorFileOID (was PriorTransferID) -->
	<xsl:template match="ODM">
		<xsl:copy>
		<xsl:attribute name="FileType"><xsl:value-of select="@TransferType"/></xsl:attribute>
		<!-- Here we test whether there is an attribute Granularity is present, if yes, it is copied -->
		<xsl:if test="@Granularity">
			<xsl:attribute name="Granularity"><xsl:value-of select="@Granularity"/></xsl:attribute>
		</xsl:if>
		<!-- FileOID is a REQUIRED attribute -->
		<xsl:attribute name="FileOID"><xsl:value-of select="@TransferID"/></xsl:attribute>
		<!-- CreationDateTime attribute is now REQUIRED (was IMPLIED). Therefore we generate it, this may lead to an empty attribute -->
		<xsl:attribute name="CreationDateTime"><xsl:value-of select="@CreationDateTime"/></xsl:attribute>
		<xsl:if test="@PriorTransferID">
			<xsl:attribute name="PriorFileOID"><xsl:value-of select="@PriorTransferID"/></xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
		</xsl:copy>
		
	</xsl:template>

	<!-- ****************** PART 1 : Study ******************* -->

	<!-- Study element -->
	<xsl:template match="Study">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>		
	</xsl:template>

	<!-- GlobalVariables element (child of Study), there are no changes with respect to the draft DTD -->
	<!-- Copy the whole tree -->
	<xsl:template match="GlobalVariables">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- BasicDefinitions element (child of Study), do a simple copy as there are no attributes -->
	<xsl:template match="BasicDefinitions">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>
	
	<!-- MeasurementUnit element -->
	<!-- Both OID as Name are REQUIRED attributes -->
	<xsl:template match="MeasurementUnit">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- Symbol element, do a simple copy as there are no attributes -->
	<xsl:template match="Symbol">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- TranslatedText element, there are no changes with respect to the draft DTD -->
	<!-- Copy the whole tree -->
	<!-- Note that the xml:lang attribute was already REQUIRED in the draft version -->
	<xsl:template match="TranslatedText">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- MetaDataVersion element, child of Study -->
	<!-- Change ID attribute into OID attribute -->
	<!-- Both OID and Name attributes are REQUIRED -->
	<xsl:template match="MetaDataVersion">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- Include element, child of MetaDataVersion -->
	<!-- Change all StudyID attributes into StudyOID -->
	<!-- Change MetaDataVersionID into MetaDataVersionOID -->
	<!-- Both these attributes are REQUIRED -->
	<!-- The Include element is EMPTY, so no apply-templates -->
	<xsl:template match="Include">
		<xsl:copy>
			<xsl:attribute name="StudyOID"><xsl:value-of select="@StudyID"/></xsl:attribute>
			<xsl:attribute name="MetaDataVersionOID"><xsl:value-of select="@MetaDataVersionID"/></xsl:attribute>	
		</xsl:copy>
	</xsl:template>

	<!-- Protocol element, child of MetaDataVersion, do a simple copy as there are no attributes -->
	<xsl:template match="Protocol">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- StudyEventRef, child of Protocol -->
	<!-- Change StudyEventID into StudyEventOID -->
	<!-- The StudyEventRef element is EMPTY, so no apply-templates -->
	<!-- Remark that OrderNumber is IMPLIED, so test on its presence -->
	<xsl:template match="StudyEventRef">
		<xsl:copy>
			<xsl:attribute name="StudyEventOID"><xsl:value-of select="@StudyEventID"/></xsl:attribute>
			<xsl:if test="@OrderNumber">
				<xsl:attribute name="OrderNumber"><xsl:value-of select="@OrderNumber"/></xsl:attribute>
			</xsl:if>
			<xsl:attribute name="Mandatory"><xsl:value-of select="@Mandatory"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- StudyEventDef, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- Remark that Category attribute is and was IMPLIED, so test on its presence -->
	<xsl:template match="StudyEventDef">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="Repeating"><xsl:value-of select="@Repeating"/></xsl:attribute>
			<xsl:attribute name="Type"><xsl:value-of select="@Type"/></xsl:attribute>
			<xsl:if test="@Category">
				<xsl:attribute name="Category"><xsl:value-of select="@Category"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- FormRef, child of StudyEventDef -->
	<!-- Change FormRefID into FormRefOID -->
	<!-- The FormRef element is EMPTY, so no apply-templates -->
	<!-- Remark that OrderNumber is and was IMPLIED, so test on its presence -->
	<xsl:template match="FormRef">
		<xsl:copy>
			<xsl:attribute name="FormOID"><xsl:value-of select="@FormID"/></xsl:attribute>
			<xsl:if test="@OrderNumber">
				<xsl:attribute name="OrderNumber"><xsl:value-of select="@OrderNumber"/></xsl:attribute>
			</xsl:if>
			<xsl:attribute name="Mandatory"><xsl:value-of select="@Mandatory"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- FormDef, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- All three attributes are REQUIRED -->
	<xsl:template match="FormDef">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="Repeating"><xsl:value-of select="@Repeating"/></xsl:attribute>
			<xsl:apply-templates/>
			</xsl:copy>
	</xsl:template>

	<!-- ItemGroupRef, child of FormDef -->
	<!-- Change ItemGroupID into ItemGroupOID -->
	<!-- OrderNumber attribute is and was IMPLIED, so test on its presence -->
	<!-- The ItemGroupRef element is EMPTY, so no apply-templates -->
	<xsl:template match="ItemGroupRef">
		<xsl:copy>
			<xsl:attribute name="ItemGroupOID"><xsl:value-of select="@ItemGroupID"/></xsl:attribute>
			<xsl:if test="@OrderNumber">
				<xsl:attribute name="OrderNumber"><xsl:value-of select="@OrderNumber"/></xsl:attribute>
			</xsl:if>
			<xsl:attribute name="Mandatory"><xsl:value-of select="@Mandatory"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- ArchiveLayout, child of FormDef -->
	<!-- Change ID attribute into OID, and PresentationID in PresentationOID -->
	<!-- PresentationID was IMPLIED in draft version, so check on its presence -->
	<!-- ArchiveLayout element is EMPTY, so no apply-templates -->
	<xsl:template match="ArchiveLayout">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="PdfFileName"><xsl:value-of select="@PdfFileName"/></xsl:attribute>
			<xsl:if test="@PresentationID">
				<xsl:attribute name="PresentationOID"><xsl:value-of select="@PresentationID"/></xsl:attribute>
			</xsl:if>
		</xsl:copy>
	</xsl:template>

	<!-- ItemGroupDef, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- Only OID, Name and Repeating are REQUIRED attributes, the others are implied 
		and their presence has to be tested -->
	<xsl:template match="ItemGroupDef">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="Repeating"><xsl:value-of select="@Repeating"/></xsl:attribute>
			<xsl:if test="@IsReferenceData"><xsl:attribute name="IsReferenceData"><xsl:value-of select="@IsReferenceData"/></xsl:attribute></xsl:if>
			<xsl:if test="@SASDatasetName"><xsl:attribute name="SASDatasetName"><xsl:value-of select="@SASDatasetName"/></xsl:attribute></xsl:if>
			<xsl:if test="@Domain"><xsl:attribute name="Domain"><xsl:value-of select="@Domain"/></xsl:attribute></xsl:if>
			<xsl:if test="@Origin"><xsl:attribute name="Origin"><xsl:value-of select="@Origin"/></xsl:attribute></xsl:if>
			<xsl:if test="@Role"><xsl:attribute name="Role"><xsl:value-of select="@Role"/></xsl:attribute></xsl:if>
			<xsl:if test="@Comment"><xsl:attribute name="Comment"><xsl:value-of select="@Comment"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- ItemRef, child of ItemGroupDef -->
	<!-- Change ItemID attribute into ItemOID -->
	<!-- OrderNumber is and was IMPLIED, test its presence -->
	<!-- ItemRef is EMPTY, so no apply-templates -->
	<xsl:template match="ItemRef">
		<xsl:copy>
			<xsl:attribute name="ItemOID"><xsl:value-of select="@ItemID"/></xsl:attribute>
			<xsl:if test="@OrderNumber"><xsl:attribute name="OrderNumber"><xsl:value-of select="@OrderNumber"/></xsl:attribute></xsl:if>
			<xsl:attribute name="Mandatory"><xsl:value-of select="@Mandatory"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- ItemDef, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- Length, SignificantDigits, SASFieldName, Origin and Comment are IMPLIED attributes: check for presence -->
	<!-- Role has become a subelement instead of an attribute -->
	<!-- SDSVarName is a new attribute -->
	<xsl:template match="ItemDef">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="DataType"><xsl:value-of select="@DataType"/></xsl:attribute>
			<xsl:if test="@Length"><xsl:attribute name="Length"><xsl:value-of select="@Length"/></xsl:attribute></xsl:if>
			<xsl:if test="@SignificantDigits"><xsl:attribute name="SignificantDigits"><xsl:value-of select="@SignificantDigits"/></xsl:attribute></xsl:if>
			<xsl:if test="@SASFieldName"><xsl:attribute name="SASFieldName"><xsl:value-of select="@SASFieldName"/></xsl:attribute></xsl:if>
			<xsl:if test="@Origin"><xsl:attribute name="Origin"><xsl:value-of select="@Origin"/></xsl:attribute></xsl:if>
			<xsl:if test="@Comment"><xsl:attribute name="Comment"><xsl:value-of select="@Comment"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>	
			<!-- If there was a role attribute, make a new Role element -->
			<xsl:if test="@Role">
				<xsl:element name="Role"><xsl:value-of select="@Role"/></xsl:element>
			</xsl:if>		
		</xsl:copy>
	</xsl:template>

	<!-- Question, child of ItemDef, perform a simple copy, as there are no attributes -->
	<xsl:template match="Question">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- ExternalQuestion, child of ItemDef -->
	<!-- No changes, copy the whole tree -->
	<xsl:template match="ExternalQuestion">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- MeasurementUnitRef, child of ItemDef -->
	<!-- Change MeasurementUnitID attribute into MeasurementUnitOID (REQUIRED attribute) -->
	<!-- MeasurementUnitRef is EMPTY, so no apply-templates -->
	<xsl:template match="MeasurementUnitRef">
		<xsl:copy>
			<xsl:attribute name="MeasurementUnitOID"><xsl:value-of select="@MeasurementUnitID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- RangeCheck, child of ItemDef -->
	<!-- We cannot do a simple copy, as some subelements have changed attributes -->
	<!-- Comparator and SoftHard are both REQUIRED attributes -->
	<xsl:template match="RangeCheck">
		<xsl:copy>
		<xsl:attribute name="Comparator"><xsl:value-of select="@Comparator"/></xsl:attribute>
		<xsl:attribute name="SoftHard"><xsl:value-of select="@SoftHard"/></xsl:attribute>			
		<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- CheckValue, child of RangeCheck -->
	<!-- No changes, use a simple copy -->
	<xsl:template match="CheckValue">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- ErrorMessage, child of RangeCheck -->
	<!-- No attributes, simply copy -->
	<xsl:template match="ErrorMessage">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- CodeListRef, child of ItemDef -->
	<!-- Change CodeListID in CodeListOID (REQUIRED attribute) -->
	<!-- CodeListRef is EMPTY, so no apply-templates -->
	<xsl:template match="CodeListRef">
		<xsl:copy>
			<xsl:attribute name="CodeListOID"><xsl:value-of select="@CodeListID"/></xsl:attribute>
		</xsl:copy>	
	</xsl:template>

	<!-- CodeList, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- SASFormatName is IMPLIED, so check for its presence -->
	<xsl:template match="CodeList">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="DataType"><xsl:value-of select="@DataType"/></xsl:attribute>
			<xsl:if test="@SASFormatName"><xsl:attribute name="SASFormatName"><xsl:value-of select="@SASFormatName"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- CodeListItem, child of CodeList -->
	<!-- No changes, copy full tree -->
	<xsl:template match="CodeListItem">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- ExternalCodeList, child of CodeList -->
	<!-- No changes, copy full tree -->
	<xsl:template match="ExternalCodeList">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Presentation, child of MetaDataVersion -->
	<!-- Change ID attribute into OID -->
	<!-- xml:lang attribute is implied, check for presence -->
	<!-- Copy text (#PCDATA) -->
	<xsl:template match="Presentation">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:if test="@xml:lang"><xsl:attribute name="xml:lang"><xsl:value-of select="@xml:lang"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- ****************** PART 2 : Study ******************* -->
	
	<!-- AdminData, child of ODM -->
	<!-- Change StudyID attribute into StudyOID (IMPLIED, so test for presence) -->
	<xsl:template match="AdminData">
		<xsl:copy>
			<xsl:if test="@StudyID"><xsl:attribute name="StudyOID"><xsl:value-of select="@StudyID"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- User, child of AdminData -->
	<!-- Change ID attribute into OID (REQUIRED attribute) -->
	<!-- UserType is a new attribute, but as it is IMPLIED and there is no corresponding value in the draft version, we do not create it -->
	<xsl:template match="User">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- LoginName, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="LoginName">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- DisplayName, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="DisplayName">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- FullName, child of USer -->
	<!-- No change, simple copy -->
	<xsl:template match="FullName">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- FirstName, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="FirstName">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- LastName, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="LastName">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Organization, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Organization">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Address, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Address">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Email, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Email">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Picture, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Picture">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Pager, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Pager">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Fax, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Fax">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Phone, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Phone">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- LocationRef, child of User, and others -->
	<!-- Change LocationID attribute in LocationOID (REQUIRED attribute) -->
	<!-- LocationRef is EMPTY, so no apply-templates -->
	<xsl:template match="LocationRef">
		<xsl:copy>
			<xsl:attribute name="LocationOID"><xsl:value-of select="@LocationID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- Certificate, child of User -->
	<!-- No change, simple copy -->
	<xsl:template match="Certificate">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Location, child of AdminData -->
	<!-- Change ID attribute into OID -->
	<!-- LocationType is IMPLIED attribute, so check for its presence -->
	<xsl:template match="Location">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:if test="@LocationType"><xsl:attribute name="LocationType"><xsl:value-of select="@LocationType"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- MetaDataVersionRef, child of Location -->
	<!-- Change StudyID attribute into StudyOID -->
	<!-- Change MetaDataVersionID into MetaDataVersionOID -->
	<!-- All attributes are REQUIRED -->
	<!-- MetaDataVersionRef is EMPTY, so no apply-templates -->
	<xsl:template match="MetaDataVersionRef">
		<xsl:copy>
			<xsl:attribute name="StudyOID"><xsl:value-of select="@StudyID"/></xsl:attribute>
			<xsl:attribute name="MetaDataVersionOID"><xsl:value-of select="@MetaDataVersionID"/></xsl:attribute>
			<xsl:attribute name="EffectiveDate"><xsl:value-of select="@EffectiveDate"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- SignatureDef, child of AdminData -->
	<!-- Change ID attribute into OID -->
	<!-- Methodology attribute is IMPLIED, so check for its presence -->
	<xsl:template match="SignatureDef">
		<xsl:copy>
			<xsl:attribute name="OID"><xsl:value-of select="@ID"/></xsl:attribute>
			<xsl:if test="@Methodology"><xsl:attribute name="Methodology"><xsl:value-of select="@Methodology"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- Meaning, child of SignatureDef -->
	<!-- No changes, simply copy -->
	<xsl:template match="Meaning">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- LegalReason, child of SignatureDef -->
	<!-- No changes, simply copy -->
	<xsl:template match="LegalReason">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- ****************** PART 3 : ReferenceData ******************* -->

	<!-- ReferenceData, child of ODM -->
	<!-- Change StudyID attribute into StudyOID -->
	<!-- Change MetaDataVersionID into MetaDataVersionOID -->
	<!-- Both these attributes are REQUIRED -->
	<xsl:template match="ReferenceData">
		<xsl:copy>
			<xsl:attribute name="StudyOID"><xsl:value-of select="@StudyID"/></xsl:attribute>
			<xsl:attribute name="MetaDataVersionOID"><xsl:value-of select="@MetaDataVersionID"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- ItemGroupData, child of ReferenceData, and of others --> 
	<!-- Change ItemGroupID attribute into ItemGroupOID -->
	<!-- ItemGroupRepeatKey and TransactionType are IMPLIED attributes, so check for presence -->
	<!-- Change the order of the subelements: new order : AuditRecord, Signature, Annotation, ItemData -->
	<xsl:template match="ItemGroupData">
		<xsl:copy>
			<xsl:attribute name="ItemGroupOID"><xsl:value-of select="@ItemGroupID"/></xsl:attribute>
			<xsl:if test="@ItemGroupRepeatKey"><xsl:attribute name="ItemGroupRepeatKey"><xsl:value-of select="@ItemGroupRepeatKey"/></xsl:attribute></xsl:if>
			<xsl:if test="@TransactionType"><xsl:attribute name="TransactionType"><xsl:value-of select="@TransactionType"/></xsl:attribute></xsl:if>
			<xsl:apply-templates select="AuditRecord"/>
			<xsl:apply-templates select="Signature"/>
			<xsl:apply-templates select="Annotation"/>
			<xsl:apply-templates select="ItemData"/>
			<!--xsl:apply-templates/-->
		</xsl:copy>
	</xsl:template>

	<!-- AuditRecord, child of ItemGroupData, and others -->
	<!-- AuditRecord has no attributes, use a simple copy without copying the subelements -->
	<!-- Reason: there has been a change in the UserRef and LocationRef subelements -->
	<xsl:template match="AuditRecord">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- UserRef, child of AuditRecord, and others -->
	<!-- Change UserID attribute into UserOID (REQUIRED attribute) -->
	<!-- UserRef has no subelements, so no apply-templates -->
	<xsl:template match="UserRef">
		<xsl:copy>
			<xsl:attribute name="UserOID"><xsl:value-of select="@UserID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- TOT HIER -->
	
	<!-- DateTimeStamp, child of AuditRecord, and of Signature -->
	<!-- No changes, simply copy -->
	<xsl:template match="DateTimeStamp">
	<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Signature, child of ItemGroupData, and of many others -->
	<!-- There has been changes in UserRef, LocationRef, SignatureRef -->
	<!-- Signature has no attributes -->
	<xsl:template match="Signature">
		<xsl:copy><xsl:apply-templates/></xsl:copy>
	</xsl:template>

	<!-- UserRef and LocationRef have been treated earlier -->
	<!-- SignatureRef, child of Signature -->
	<!-- Change SignatureID attribute into SignatureOID -->
	<!-- SignatureRef is EMPTY, so no apply-templates -->
	<xsl:template match="SignatureRef">
		<xsl:copy>
			<xsl:attribute name="SignatureOID"><xsl:value-of select="@SignatureID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- CryptoBindingManifest, child of Signature -->
	<!-- No change, simply copy -->
	<xsl:template match="CryptoBindingManifest">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- Annotation, child of ItemGroupData, and of others -->
	<!-- There is a new attribute value for attribute TransactionType, however we do not have to change anything -->
	<xsl:template match="Annotation">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- ItemData, child of ItemGroupData -->
	<!-- Change ItemID attribute into ItemOID -->
	<!-- Subelement Value has become an attribute -->
	<!-- The is a new IsNull attribute (IMPLIED) -->
	<!-- TransactionType is an IMPLIED attribute, so check for its presence -->
	<!-- Also the order of the subelements has been changed: new order: AuditRecord, Signature, MeasurementUnitRef, Annotation -->
	<xsl:template match="ItemData">
		<xsl:copy>
			<xsl:attribute name="ItemOID"><xsl:value-of select="@ItemID"/></xsl:attribute>
			<xsl:if test="@TransactionType"><xsl:attribute name="TransactionType"><xsl:value-of select="@TransactionType"/></xsl:attribute></xsl:if>
			<xsl:if test="./Value">
				<xsl:attribute name="Value"><xsl:value-of select="./Value"/></xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="AuditRecord"/>
			<xsl:apply-templates select="Signature"/>
			<xsl:apply-templates select="MeasurementUnitRef"/>
			<xsl:apply-templates select="Annotation"/>
		</xsl:copy>	
	</xsl:template>

	<!-- ReasonForChange, child of AuditRecord -->
	<!-- No changes, simply copy -->
	<xsl:template match="ReasonForChange">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- SourceID, child of AuditRecord -->
	<!-- No changes, simply copy -->
	<xsl:template match="SourceID">
		<xsl:copy-of select="."/>
	</xsl:template>

	<!-- ****************** PART 4 : ClinicalData ******************* -->

	<!-- ClinicalData, child of ODM -->
	<!-- Change StudyID attribute into StudyOID -->
	<!-- Change MetaDataVersionID attribute into MetaDataVersionOID -->
	<!-- Both attributes are REQUIRED -->
	<xsl:template match="ClinicalData">
		<xsl:copy>
			<xsl:attribute name="StudyOID"><xsl:value-of select="@StudyID"/></xsl:attribute>
			<xsl:attribute name="MetaDataVersionOID"><xsl:value-of select="@MetaDataVersionID"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

	<!-- SubjectData, child of ClinicalData -->
	<!-- Attributes remain unchanged -->
	<!-- TransactionType is IMPLIED, check for presence, is taken into account when using copy-of -->
	<!-- Order of elements is changed: new order: AuditRecord, Signature, InvestigatorRef, SiteRef, Annotation, StudyEventData -->
	<xsl:template match="SubjectData">
	<xsl:copy>
	<xsl:copy-of select="@*"/>
	<xsl:apply-templates select="AuditRecord"/>
	<xsl:apply-templates select="Signature"/>
	<xsl:apply-templates select="InvestigatorRef"/>
	<xsl:apply-templates select="SiteRef"/>
	<xsl:apply-templates select="Annotation"/>
	<xsl:apply-templates select="StudyEventData"/>
	</xsl:copy>
	</xsl:template>

	<!-- InvestigatorRef, child of SubjectData -->
	<!-- Change UserID attribute into UserOID (REQUIRED) -->
	<!-- InvestigatorRef is EMPTY, so no apply-templates -->
	<xsl:template match="InvestigatorRef">
		<xsl:copy>
			<xsl:attribute name="UserOID"><xsl:value-of select="@UserID"/></xsl:attribute>
		</xsl:copy>	
	</xsl:template>

	<!-- SiteRef, child of SubjectData -->
	<!-- Change LocationID attribute into LocationOID (REQUIRED attribute) -->
	<!-- SiteRef is EMPTY, so no apply-templates -->
	<xsl:template match="SiteRef">
		<xsl:copy>
			<xsl:attribute name="LocationOID"><xsl:value-of select="@LocationID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>

	<!-- StudyEventData -->
	<!-- Change StudyEventID attribute into StudyEventOID -->
	<!-- StudyEventRepeatKey and TransactionType are IMPLIED attributes, so check for presence -->
	<!-- New order of subelements: new order: AuditRecord, Signature, Annotation, FormData -->
	<!-- ExpectedTime and ActualTimne have been removed --> 

	<xsl:template match="StudyEventData">
		<xsl:copy>
			<xsl:attribute name="StudyEventOID"><xsl:value-of select="@StudyEventID"/></xsl:attribute>
			<xsl:if test="@StudyEventRepeatKey"><xsl:attribute name="StudyEventRepeatKey"><xsl:value-of select="StudyEventRepeatKey"/></xsl:attribute></xsl:if>
			<xsl:if test="@TransactionType"><xsl:attribute name="TransactionType"><xsl:value-of select="TransactionType"/></xsl:attribute></xsl:if>
			<xsl:apply-templates select="AuditRecord"/>
			<xsl:apply-templates select="Signature"/>
			<!--xsl:apply-templates select="ExpectedTime"/-->
			<!--xsl:apply-templates select="ActualTime"/-->
			<xsl:apply-templates select="Annotation"/>
			<xsl:apply-templates select="FormData"/>
		</xsl:copy>
	</xsl:template>

	<!-- ExpectedTime, child of StudyEventData has been removed --> 

	<!-- ActualTime, child of StudyEventData has been removed --> 

	<!-- FormData, child of StudyEventData -->
	<!-- Change FormID attribute into FormOID -->
	<!-- FormRepeatKey and TranactionType are IMPLIED attributes, so check for presence -->
	<!-- Change the order of the subelements: new order: AuditRecord, Signature, ArchiveLayoutRef, Annotation,ItemGroupData -->
	<xsl:template match="FormData">
		<xsl:copy>	
			<xsl:attribute name="FormOID"><xsl:value-of select="@FormID"/></xsl:attribute>
			<xsl:if test="@FormRepeatKey"><xsl:attribute name="FormRepeatKey"><xsl:value-of select="@FormRepeatKey"/></xsl:attribute></xsl:if>
			<xsl:if test="@TransactionType"><xsl:attribute name="TransactionType"><xsl:value-of select="@TransactionType"/></xsl:attribute></xsl:if>
			<xsl:apply-templates select="AuditRecord"/>
			<xsl:apply-templates select="Signature"/>
			<xsl:apply-templates select="ArchiveLayoutRef"/>
			<xsl:apply-templates select="Annotation"/>
			<xsl:apply-templates select="ItemGroupData"/>
		</xsl:copy>
	</xsl:template>

	<!-- ArchiveLayoutRef, child of FormData -->
	<!-- Change ArchiveLayoutID attribute into ArchiveLayoutOID (REQUIRED attribute) -->
	<!-- ArchiveLayoutRef is EMPTY, so no apply-templates -->
	<xsl:template match="ArchiveLayoutRef">
		<xsl:copy>
			<xsl:attribute name="ArchiveLayoutOID"><xsl:value-of select="@ArchiveLayoutID"/></xsl:attribute>
		</xsl:copy>
	</xsl:template>


	<!-- end of stylesheet -->
</xsl:stylesheet>
