﻿<?xml version="1.0" encoding="UTF-8"?>
<!--
Metadata: Owner: ebxml-bp (OASIS ebXML Business Process TC)
Product: ebxmlbp (aka ebBP)
Product Version: 2.0.3
Artifact Type: Example (snippet)
Stage: cs (Committee Specification)
Descriptive Name: TransformFor1.05
Revision: None
Language: en (English)
Form: xsl (snippet)
Date: 200600428 (28 April 2006)
Note: This is a working example for assistance in transformation of previous ebXML BPSS versions to ebBP v2.0.x. This is for information only.
 -->
 <!--OASIS takes no position regarding the validity or scope of any intellectual property or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; neither does it represent that it has made any effort to identify any such rights. Information on OASIS's procedures with respect to rights in OASIS specifications can be found at the OASIS website. Copies of claims of rights made available for publication and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementors or users of this specification, can be obtained from the OASIS Executive Director. 
OASIS invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights which may cover technology that may be required to implement this specification. Please address the information to the OASIS Executive Director. 
Copyright © OASIS Open 2005, 2006. All Rights Reserved.
This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to OASIS, except as needed for the purpose of developing OASIS specifications, in which case the procedures for copyrights defined in the OASIS Intellectual Property Rights document must be followed, or as required to translate it into languages other than English. 
The limited permissions granted above are perpetual and will not be revoked by OASIS or its successors or assigns. 
This document and the information contained herein is provided on an "AS IS" basis and OASIS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bp1="http://www.ebxml.org/BusinessProcess" version="1.0" xmlns="http://docs.oasis-open.org/ebxml-bp/ebbp-2.0" xmlns:bp2="http://docs.oasis-open.org/ebxml-bp/ebbp-2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<xsl:key name="bt" match="//bp1:BusinessTransaction" use="@nameID"/>
	<xsl:key name="bd" match="//bp1:BusinessDocument" use="@name"/>
	<xsl:key name="state" match="../*" use="@name"/>
	<!--  
    1. Add conditionals for optional elements and  attributes
    2. Add iterations for repeatable elements as needed.
    3. Missing required elements and attributes inserted with empty text nodes.
    4. Checks on semantics, warnings of incomplete elements, etc.
    -->
	<xsl:template match="bp1:ProcessSpecification">
		<!-- @nameID is mandatory in 2.0 -->
		<ProcessSpecification specificationVersion="2.0" nameID="{generate-id()}">
			<xsl:apply-templates select="@name|@version|@uuid"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- AttributeSubstitution  -->
			<!-- ExternalRoles, Signal, Variable: there are no counterparts in 1.05 -->
			<Signal name="Unspecified Signal" nameID="unspecified-signal">
				<Specification name="Unspecified Signal Specification" nameID="unspecified-signal-specification"
					location=""/>
			</Signal>
			<xsl:apply-templates select="bp1:Package|bp1:BusinessDocument|bp1:BusinessTransaction|bp1:BinaryCollaboration|bp1:MultiPartyCollaboration"/>
		</ProcessSpecification>
	</xsl:template>
	<!-- a rule for copying attributes as is -->
	<xsl:template match="@*">
		<xsl:copy/>
	</xsl:template>
	<!-- for detecting insufficient templates -->
	<xsl:template match="bp1:*">
		<xsl:comment>WARNING: there is no explicit template for <xsl:value-of select="name(.)"/></xsl:comment>
		<xsl:element name="{local-name()}" namespace="http://docs.oasis-open.org/ebxml-bp/ebbp-2.0">
			<xsl:apply-templates select="node()|@*"/>
		</xsl:element>
	</xsl:template>
	<!-- copy name and nameID attribute, generate nameID if omitted -->
	<xsl:template name="identifiers">
		<xsl:apply-templates select="@name|@nameID"/>
		<xsl:if test="not(@nameID)">
			<xsl:attribute name="nameID"><xsl:value-of select="generate-id()"/></xsl:attribute>
		</xsl:if>
	</xsl:template>
	<xsl:template match="bp1:ProcessSpecification/@version">
		<xsl:attribute name="instanceVersion"><xsl:value-of select="."/></xsl:attribute>
	</xsl:template>
	<xsl:template match="bp1:Documentation">
		<Documentation>
			<!-- expecting @uri and @xml:lang -->
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates select="node()"/>
		</Documentation>
	</xsl:template>
	<xsl:template name="Specification">
		<Specification name="{generate-id(@name)}" nameID="{generate-id(@name)}" location="{@specificationLocation}"/>
	</xsl:template>
	<xsl:template match="bp1:BusinessDocument">
		<BusinessDocument>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:apply-templates select="bp1:ConditionExpression"/>
			<xsl:call-template name="Specification"/>
		</BusinessDocument>
	</xsl:template>
	<xsl:template match="bp1:BusinessTransaction">
		<LegacyBusinessTransaction>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="@pattern|@isGuaranteedDeliveryRequired"/>
			<!-- beginsWhen, endsWhen, preCondition, postCondition have been moved to
				BusinessTransactionActivity in 1.01-1.05 -->
			<xsl:apply-templates select="bp1:Documentation"/>
			<RequestingRole name="RequestingRole" nameID="{generate-id(bp1:RequestingBusinessActivity/@name)}"/>
			<RespondingRole name="RespondingRole" nameID="{generate-id(bp1:RespondingBusinessActivity/@name)}"/>
			<xsl:apply-templates select="bp1:RequestingBusinessActivity" mode="legacy"/>
			<xsl:apply-templates select="bp1:RespondingBusinessActivity" mode="legacy"/>
		</LegacyBusinessTransaction>
	</xsl:template>
	<xsl:template name="BusinessActivity">
		<xsl:call-template name="identifiers"/>
		<xsl:apply-templates select="@isAuthorizationRequired"/>
		<xsl:apply-templates select="@isIntelligibleCheckRequired"/>
		<xsl:apply-templates select="@isNonRepudiationReceiptRequired"/>
		<xsl:apply-templates select="@isNonRepudiationRequired"/>
		<xsl:apply-templates select="@timeToAcknowledgeReceipt"/>
		<xsl:apply-templates select="@timeToAcknowledgeAcceptance"/>
		<!-- not in BPSS 1.01, new in 1.05 -->
		<xsl:apply-templates select="@retryCount"/>
		<xsl:apply-templates select="bp1:Documentation"/>
		<xsl:apply-templates select="bp1:DocumentEnvelope"/>
		<!-- these cannot be automatically generated in full -->
		<ReceiptAcknowledgement name="undefined" nameID="{generate-id()}_RA" signalDefinitionRef="unspecified-signal"/>
		<ReceiptAcknowledgementException name="undefined" nameID="{generate-id()}-RAEX" signalDefinitionRef="unspecified-signal"/>
	</xsl:template>
	<xsl:template match="bp1:RequestingBusinessActivity" mode="legacy">
		<RequestingBusinessActivity>
			<xsl:call-template name="BusinessActivity"/>
		</RequestingBusinessActivity>
	</xsl:template>
	<xsl:template match="bp1:RespondingBusinessActivity" mode="legacy">
		<xsl:if test="bp1:DocumentEnvelope">
			<RespondingBusinessActivity>
				<xsl:call-template name="BusinessActivity"/>
			</RespondingBusinessActivity>
		</xsl:if>
	</xsl:template>
	<xsl:template name="businessDocumentRef">
		<xsl:attribute name="businessDocumentRef"><xsl:value-of select="@businessDocumentIDRef"/><!--
		<xsl:variable name="node" select="key('bd', @businessDocument)"/>
		<xsl:choose>
			<xsl:when test="@businessDocumentIDRef">
				<xsl:value-of select="@businessDocumentIDRef"/>
			</xsl:when>
			<xsl:when test="$node/@nameID">
				<xsl:value-of select="$node/@nameID"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id($node)"/>
			</xsl:otherwise>
		</xsl:choose>
--></xsl:attribute>
	</xsl:template>
	<xsl:template match="bp1:DocumentEnvelope">
		<DocumentEnvelope>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="@isPositiveResponse"/>
			<xsl:call-template name="businessDocumentRef"/>
			<xsl:apply-templates select="@isAuthenticated|@isConfidential|@isTamperProof|@isTamperDetectable"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:apply-templates select="bp1:Attachment"/>
		</DocumentEnvelope>
	</xsl:template>
	<xsl:template match="bp1:Attachment">
		<Attachment>
			<xsl:call-template name="identifiers"/>
			<xsl:call-template name="businessDocumentRef"/>
			<xsl:apply-templates select="@isAuthenticated|@isConfidential|@isTamperProof|@isTamperDetectable"/>
			<xsl:apply-templates select="@mimeType"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:call-template name="Specification"/>
		</Attachment>
	</xsl:template>
	<!-- TODO: convert value of @isAuthenticated, @isConfidential from boolean to enum -->
	<xsl:template match="@isTamperProof">
		<xsl:attribute name="isTamperDetectable"><xsl:choose><xsl:when test=".='true'">persistent</xsl:when><xsl:otherwise>none</xsl:otherwise></xsl:choose><!-- <xsl:value-of select="."/> --></xsl:attribute>
	</xsl:template>
	<xsl:template match="bp1:Package">
		<Package>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:BusinessDocument"/>
			<xsl:apply-templates select="bp1:BusinessTransaction"/>
			<xsl:apply-templates select="bp1:BinaryCollaboration"/>
			<xsl:apply-templates select="bp1:Package"/>
		</Package>
	</xsl:template>
		<xsl:template name="TimeToPerform">
		<TimeToPerform>
			<xsl:apply-templates select="@timeToPerform"/>
		</TimeToPerform>		
	</xsl:template>
	<xsl:template match="@timeToPerform">
		<xsl:attribute name="duration"><xsl:value-of select="."/></xsl:attribute>
	</xsl:template>
	<xsl:template match="bp1:BinaryCollaboration">
		<BinaryCollaboration>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="@pattern"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- 1.01 -->
			<xsl:apply-templates select="bp1:InitiatingRole|bp1:RespondingRole"/>
			<!-- 1.05 -->
			<xsl:apply-templates select="bp1:Role"/>
			<xsl:call-template name="TimeToPerform"/>
			<xsl:apply-templates select="bp1:Start"/>
			<xsl:apply-templates select="@beginsWhen"/>
			<xsl:apply-templates select="@preCondition"/>
			<xsl:apply-templates select="@postCondition"/>
			<xsl:apply-templates select="@endsWhen"/>
			<xsl:apply-templates select="bp1:BusinessTransactionActivity"/>
			<xsl:apply-templates select="bp1:CollaborationActivity"/>
			<xsl:apply-templates select="bp1:Success"/>
			<xsl:apply-templates select="bp1:Failure"/>
			<xsl:apply-templates select="bp1:Transition"/>
			<xsl:apply-templates select="bp1:Fork"/>
			<xsl:apply-templates select="bp1:Join"/>
		</BinaryCollaboration>
	</xsl:template>
	<xsl:template match="bp1:InitiatingRole|bp1:RespondingRole|bp1:Role">
		<Role>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
		</Role>
	</xsl:template>
	<xsl:template match="bp1:Start">
		<Start>
			<xsl:apply-templates select="@name|@nameID"/>
			<xsl:apply-templates select="Documentation"/>
			<xsl:call-template name="ToLink"/>
		</Start>
	</xsl:template>
	<xsl:template match="bp1:CollaborationActivity">
		<CollaborationActivity>
			<xsl:call-template name="identifiers"/>
		</CollaborationActivity>
	</xsl:template>
	<xsl:template match="@isLegallyBinding">
		<xsl:attribute name="hasLegalIntent"><xsl:value-of select="."/></xsl:attribute>
	</xsl:template>
	<xsl:template match="@beginsWhen">
		<BeginsWhen expression="{.}" expressionLanguage="DocumentEnvelope"/>
	</xsl:template>
	<xsl:template match="@endsWhen">
		<EndsWhen expression="{.}" expressionLanguage="DocumentEnvelope"/>
	</xsl:template>
	<xsl:template match="@preCondition">
		<PreCondition expression="{.}" expressionLanguage="DocumentEnvelope"/>
	</xsl:template>
	<xsl:template match="@postCondition">
		<PostCondition expression="{.}" expressionLanguage="DocumentEnvelope"/>
	</xsl:template>
	<xsl:template match="bp1:BusinessTransactionActivity">
		<BusinessTransactionActivity>
			<xsl:call-template name="identifiers"/>
			<xsl:call-template name="businessTransactionRef"/>
			<xsl:apply-templates select="@isLegallyBinding"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- TimeToPerform, Performs, BeginsWhen, PreCondition, PostCondition, EndsWhen -->
			<xsl:call-template name="TimeToPerform"/>
			<!-- TODO: @businessTransactionIDRef may not exist -->
			<xsl:variable name="bt" select="key('bt', @businessTransactionIDRef)"/>
			<xsl:comment>BT=<xsl:value-of select="$bt/@name"/>
			</xsl:comment>
			<!-- TODO: in BPSS 1.01, fromAuthorizedRoleIDRef and toAuthorizedRoleIDRef are
				optional. generate IDs if omitted -->
			<Performs currentRoleRef="{concat(@fromAuthorizedRoleIDRef,@fromRoleIDRef)}" performsRoleRef="{generate-id($bt/bp1:RequestingBusinessActivity/@name)}"/>
			<Performs currentRoleRef="{concat(@toAuthorizedRoleIDRef,@toRoleIDRef)}" performsRoleRef="{generate-id($bt/bp1:RespondingBusinessActivity/@name)}"/>
			<!-- TODO: is it really valid to use BinaryCollaboration's values? -->
			<xsl:apply-templates select="$bt/@beginsWhen"/>
			<xsl:apply-templates select="$bt/@preCondition"/>
			<xsl:apply-templates select="$bt/@postCondition"/>
			<xsl:apply-templates select="$bt/@endsWhen"/>
		</BusinessTransactionActivity>
	</xsl:template>
	<xsl:template name="businessTransactionRef">
		<xsl:attribute name="businessTransactionRef">
			<!-- TODO: @businessTransactionIDRef is optional in 1.05 -->
			<xsl:value-of select="@businessTransactionIDRef"/>
		</xsl:attribute>
	</xsl:template>
	<xsl:template name="FromLink">
		<FromLink>
			<xsl:attribute name="fromBusinessStateRef">
				<xsl:variable name="node" select="key('state', @fromBusinessState)"/>
				<xsl:choose>
					<xsl:when test="@fromBusinessStateIDRef"><xsl:value-of select="@fromBusinessStateIDRef"/></xsl:when>
					<xsl:when test="$node/@nameID"><xsl:value-of select="$node/@nameID"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="generate-id($node)"/></xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates select="@conditionGuard"/>
			<!-- copy bp1:Transition/bp1:Documentation here because bp2:Transition has no
				Documentation child -->
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:apply-templates select="bp1:ConditionExpression"/>
			<!-- ConditionExpression -->
		</FromLink>
	</xsl:template>
	<xsl:template match="@fromBusinessStateIDRef">
		<xsl:attribute name="fromBusinessStateRef"><xsl:value-of select="."/></xsl:attribute>
	</xsl:template>
	<xsl:template name="ToLink">
		<ToLink>
			<xsl:attribute name="toBusinessStateRef">
				<xsl:variable name="node" select="key('state', @toBusinessState)"/>
				<xsl:choose>
					<xsl:when test="@toBusinessStateIDRef"><xsl:value-of select="@toBusinessStateIDRef"/></xsl:when>
					<xsl:when test="$node/@nameID"><xsl:value-of select="$node/@nameID"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="generate-id($node)"/></xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:apply-templates select="bp1:ConditionExpression"/>
		</ToLink>
	</xsl:template>
	<xsl:template match="bp1:Transition">
		<!-- TODO: Transitions that links to/from Fork or Join shall be transformed into 
			children of Fork/Join -->
		<Transition>
			<xsl:call-template name="identifiers"/>
			<xsl:call-template name="FromLink"/>
			<xsl:call-template name="ToLink"/>
		</Transition>
	</xsl:template>
	<!-- Conversion from 1.x versions will likely need editing and corrections -->
	<xsl:template match="bp1:Fork">
		<Fork>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- FromLink -->
			<!-- ToLink -->
		</Fork>
	</xsl:template>
	<!-- Conversion from 1.x versions will likely need editing and corrections -->
	<xsl:template match="bp1:Join">
		<Join>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- FromLinks  added in 2.0-->
			<!-- ToLink added in 2.0 -->
		</Join>
	</xsl:template>
	<xsl:template match="bp1:Success">
		<Success>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<xsl:call-template name="FromLink"/>
		</Success>
	</xsl:template>
	<xsl:template match="bp1:Failure">
		<Failure>
			<xsl:call-template name="identifiers"/>
			<xsl:apply-templates select="bp1:Documentation"/>
			<!-- FromLink  added in 2.0 -->
			<xsl:call-template name="FromLink"/>
		</Failure>
	</xsl:template>
	<xsl:template match="bp1:ConditionExpression">
		<ConditionExpression expression="{@expression}">
			<xsl:attribute name="expressionLanguage">
				<!-- save commonly used, but not 2.0 compliant values -->
				<xsl:choose>
					<xsl:when test="@expressionLanguage='XPath'">XPath1</xsl:when>
					<xsl:otherwise><xsl:value-of select="@expressionLanguage"/></xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<!-- BPSS 1.01 doesn't have default attribute -->
			<xsl:apply-templates select="bp1:Documentation"/>
		</ConditionExpression>
	</xsl:template>
</xsl:stylesheet>
