<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/LocationCost/@fromlocationid">
    <xsl:attribute name="fromlocation">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/Admin/LocationCost/@tolocationid">
    <xsl:attribute name="tolocation">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>


  <xsl:template match="/Admin">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <!-- To ensure the "right" order -->

      <xsl:apply-templates select="Agent[@operation='create']"/>

	    <xsl:apply-templates select="Team[@operation='create']"/>
		<xsl:apply-templates select="AgentTeam[@operation='create']"/>


    	<xsl:apply-templates select="Role[@operation='create']"/>
      <xsl:apply-templates select="RoleMember[@operation='create']"/>

      <xsl:apply-templates select="CallbackRuleset[@operation='create']"/>
      <xsl:apply-templates select="CallbackRule[@operation='create']"/>

      <xsl:apply-templates select="AddressBook[@operation='create']"/>
      <xsl:apply-templates select="AddressBookEntry[@operation='create']"/>


      <xsl:apply-templates select="Carrier[@operation='create']"/>
      <xsl:apply-templates select="Resource[@operation='create']"/>
      <xsl:apply-templates select="ResourceCarrier[@operation='create']"/>


      <xsl:apply-templates select="Location[@operation='create']"/>
      <xsl:apply-templates select="LocationCost[@operation='create']"/>
      <xsl:apply-templates select="LocationNumberingRule[@operation='create']"/>

      <xsl:apply-templates select="PromptLink[@operation='delete']"/>
      <xsl:apply-templates select="PromptLink[@operation!='delete']"/>
      <xsl:apply-templates select="Prompt"/>


      <xsl:apply-templates select="Campaign"/>

      <xsl:apply-templates select="Queue[@operation='create']"/>


      <xsl:apply-templates select="UserField[@operation='create']"/>

      <xsl:apply-templates select="Qualification[@operation='create' and not(ParentId)]"/>
      <xsl:apply-templates select="Qualification[@operation!='create' or ParentId]"/>

      <xsl:apply-templates select="InboundActivity[@operation='create']"/>
      <xsl:apply-templates select="OutboundActivity[@operation='create']"/>

      
      <xsl:apply-templates select="AppointmentsContext[@operation='create']"/>
      <xsl:apply-templates select="AppointmentsArea[@operation='create']"/>
      <xsl:apply-templates select="AppointmentsMember[@operation='create']"/>
      
      <xsl:apply-templates select="node()[not(self::PromptLink) and not(self::Prompt) and not(self::Qualification) and not(self::Campaign) and not(self::InboundActivity[@operation='create']) and not(self::OutboundActivity[@operation='create']) 
                           and not(self::AppointmentsArea[@operation='create']) and not(self::AppointmentsMember[@operation='create']) and not(self::AppointmentsContext[@operation='create']) 
                           and not(self::Location[@operation='create']) and not(self::LocationCost[@operation='create']) and not(self::LocationNumberingRule[@operation='create']) 
                           and not(self::Carrier[@operation='create']) and not(self::Resource[@operation='create']) and not(self::ResourceCarrier[@operation='create']) 
                           and not(self::CallbackRuleset[@operation='create']) and not(self::CallbackRule[@operation='create'])
                           and not(self::AddressBook[@operation='create']) and not(self::AddressBookEntry[@operation='create'])
                           and not(self::Role[@operation='create']) and not(self::RoleMember[@operation='create'])
                           and not(self::Agent[@operation='create'])
                           and not(self::Queue[@operation='create'])
                           and not(self::Team[@operation='create']) and not(self::AgentTeam[@operation='create'])
                           and not(self::UserField[@operation='create'])
                           ]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/*" >
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/*[@operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/*/*" >
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/*/*[@type]" >
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/*/*/*" >
    <xsl:copy-of select="."/>
  </xsl:template>

  <!--Convert xml boolean values-->

  <xsl:template match="/Admin/*/*[@type='Boolean'][. = 'false']" >
    <xsl:copy>0</xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/*/*[@type='Boolean'][. = 'true']" >
    <xsl:copy>1</xsl:copy>
  </xsl:template>

  <!--Ensure that there is a property campaignid on activities, convert child OwnerId in attribute owner-->

  <xsl:template match="/Admin/OutboundActivity[not(@campaignid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="campaignid">
        <xsl:value-of select="Campaign"/>
      </xsl:attribute>
      <xsl:if test="OwnerId != ''">
        <xsl:attribute name="owner">
          <xsl:value-of select="OwnerId"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/InboundActivity[not(@campaignid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="campaignid">
        <xsl:value-of select="Campaign"/>
      </xsl:attribute>
      <xsl:if test="OwnerId != ''">
        <xsl:attribute name="owner">
          <xsl:value-of select="OwnerId"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/Team[@operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:if test="OwnerId != ''">
        <xsl:attribute name="owner">
          <xsl:value-of select="OwnerId"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/Queue[@operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:if test="OwnerId != ''">
        <xsl:attribute name="owner">
          <xsl:value-of select="OwnerId"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/PredefinedText[not(@campaignid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="campaignid">
        <xsl:value-of select="Campaign"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/Attachment[not(@campaignid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="campaignid">
        <xsl:value-of select="Campaign"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!--Ensure attribute planningid is present on planning related elements -->

  <xsl:template match="/Admin/PlanningTimeSpan[not(@planningid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="planningid">
        <xsl:value-of select="PlanningId"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/SpecialDay[not(@planningid) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="planningid">
        <xsl:value-of select="PlanningId"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/AppointmentsArea[not(@context) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="context">
        <xsl:value-of select="Context"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/AppointmentsMember[not(@context) and @operation='create']" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="context">
        <xsl:value-of select="Context"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/Qualification[ParentId and not(@parentid)]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>
      <xsl:attribute name="parentid">
        <xsl:value-of select="ParentId"/>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterTemplateDefinition[List and not(@listid) and ParentParam and not(@parentid)]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="listid">
        <xsl:value-of select="List"/>
      </xsl:attribute>

      <xsl:attribute name="parentid">
        <xsl:value-of select="ParentParam"/>
      </xsl:attribute>
      
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterTemplateDefinition[List and not(@listid) and not(ParentParam) or List and not(@listid) and @parentid]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="listid">
        <xsl:value-of select="List"/>
      </xsl:attribute>
      
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterTemplateDefinition[not(List) and ParentParam and not(@parentid) or @listid and ParentParam and not(@parentid)]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="parentid">
        <xsl:value-of select="ParentParam"/>
      </xsl:attribute>

      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterDefinition[List and not(@listid) and ParentParam and not(@parentid)]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="listid">
        <xsl:value-of select="List"/>
      </xsl:attribute>

      <xsl:attribute name="parentid">
        <xsl:value-of select="ParentParam"/>
      </xsl:attribute>

      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterDefinition[List and not(@listid) and not(ParentParam) or List and not(@listid) and @parentid]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="listid">
        <xsl:value-of select="List"/>
      </xsl:attribute>
      
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterDefinition[not(List) and ParentParam and not(@parentid) or @listid and ParentParam and not(@parentid)]" >
    <xsl:copy>
      <xsl:attribute name="state">10</xsl:attribute>

      <xsl:attribute name="parentid">
        <xsl:value-of select="ParentParam"/>
      </xsl:attribute>

      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/DynamicParameterListItem[@operation='update']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:if test="Sequence != ''">      
        <xsl:element name="Sequence">
          <xsl:value-of select="-Sequence"/>
        </xsl:element>     
      </xsl:if>      
 
      <xsl:apply-templates/>
    
    </xsl:copy>
    
  </xsl:template>


  <xsl:template match="/Admin/DynamicParameterListItem[@operation='create']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:attribute name="sequenceid">
        <xsl:value-of select="-Sequence"/>
      </xsl:attribute>

      <xsl:element name="Sequence">
        <xsl:value-of select="-Sequence"/>
      </xsl:element>

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>

  <xsl:template match="/Admin/DynamicParameterListItem[@operation='update' or @operation='create']/Sequence" >
  </xsl:template>


  <xsl:template match="/Admin/CallbackRule[@operation='update']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:if test="Sequence != ''">      
        <xsl:element name="Sequence">
          <xsl:value-of select="-Sequence"/>
        </xsl:element>     
      </xsl:if>      

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>


  <xsl:template match="/Admin/CallbackRule[@operation='create']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:attribute name="sequenceid">
        <xsl:value-of select="-Sequence"/>
      </xsl:attribute>

      <xsl:element name="Sequence">
        <xsl:value-of select="-Sequence"/>
      </xsl:element>

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>

  <xsl:template match="/Admin/CallbackRule[@operation='update' or @operation='create']/Sequence" >
  </xsl:template>


  <xsl:template match="/Admin/AddressBookEntry[@operation='update']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:if test="Sequence != ''">      
        <xsl:element name="Sequence">
          <xsl:value-of select="-Sequence"/>
        </xsl:element>     
      </xsl:if>      

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>


  <xsl:template match="/Admin/AddressBookEntry[@operation='create']" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>

      <xsl:attribute name="sequenceid">
        <xsl:value-of select="-Sequence"/>
      </xsl:attribute>

      <xsl:element name="Sequence">
        <xsl:value-of select="-Sequence"/>
      </xsl:element>

      <xsl:apply-templates/>

    </xsl:copy>

  </xsl:template>

  <xsl:template match="/Admin/AddressBookEntry[@operation='update' or @operation='create']/Sequence" >
  </xsl:template>


  <xsl:template match="/Admin/ObjectSecurity[not(SecuredAdminObjectId) and not(RoleId) and not(RightId) and @securedadminobjectid and @roleid and @rightid]" >
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:element name="SecuredAdminObjectId">
        <xsl:value-of select="@securedadminobjectid"/>
      </xsl:element>
      <xsl:element name="RoleId">
        <xsl:value-of select="@roleid"/>
      </xsl:element>
      <xsl:element name="RightId">
        <xsl:value-of select="@rightid"/>
      </xsl:element>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="/Admin/OutboundActivity/OwnerId" >
  </xsl:template>

  <xsl:template match="/Admin/InboundActivity/OwnerId" >
  </xsl:template>

  <xsl:template match="/Admin/Queue/OwnerId" >
  </xsl:template>

  <xsl:template match="/Admin/Team/OwnerId" >
  </xsl:template>

  <!--Ensure no DataSortOrder, SystemSortOrder, DataFilter, SystemFilter and CurrentActivityFilter on outbound activities -->

  <xsl:template match="/Admin/OutboundActivity/DataSortOrder" >
  </xsl:template>

  <xsl:template match="/Admin/OutboundActivity/SystemSortOrder" >
  </xsl:template>

  <xsl:template match="/Admin/OutboundActivity/DataFilter" >
  </xsl:template>

  <xsl:template match="/Admin/OutboundActivity/SystemFilter" >
  </xsl:template>

  <xsl:template match="/Admin/OutboundActivity/CurrentActivityFilter" >
  </xsl:template>

  <!--Ensure no GuiLanguages and no UserFields-->

  <xsl:template match="/Admin/GuiLanguages" >
  </xsl:template>

  <xsl:template match="/Admin/UserField" >
  </xsl:template>

</xsl:stylesheet>