<?xml version="1.0"?>
<!--
 Licensed Materials - Property of IBM

 IBM Cognos Products: fmmd

 (C) Copyright IBM Corp. 2003, 2013

 US Government Users Restricted Rights - Use, duplication or disclosure 
 restricted by GSA ADP Schedule Contract with IBM Corp.
-->
<!--XSLT version 2-->
<!--XSL Transformation from a schema v2 model to a valid BMT schema v3 model-->
<xsl:stylesheet xmlns="http://www.developer.cognos.com/schemas/bmt/3/0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bmtcr1="http://www.developer.cognos.com/schemas/bmt/2/0" exclude-result-prefixes="bmtcr1">
	<xsl:output encoding="utf-8" method="xml" indent="yes"/>
	<!--root match-->
	<xsl:template match="/">
		<!--make sure that this transformation is not performed on a schema v3 model-->
		<xsl:if test="/bmtcr1:project/@xsi:schemaLocation = 'http://www.developer.cognos.com/schemas/bmt/3/0 BMTModelSpecification.xsd'">
			<xsl:message terminate="yes">The input file for this transformer must be a schema v2 model.  You tried to use a schema v3 model.  Exiting...</xsl:message>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>
	<!--generic match, copies over the element unless it is redefined later in the file-->
	<xsl:template match="*">
		<xsl:element name="{name(.)}">
			<xsl:for-each select="@*">
				<xsl:choose>
					<!--Check if the attribute is language, which is now called locale-->
					<xsl:when test="name(.) = 'language'">
						<xsl:attribute name="locale"><xsl:value-of select="."/></xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--change the schema to v3 version-->
	<xsl:template match="bmtcr1:project">
		<xsl:element name="project" namespace="http://www.developer.cognos.com/schemas/bmt/3/0">
			<xsl:for-each select="@*">
				<xsl:choose>
					<!--change schema from v2 to v3-->
					<xsl:when test="name(.) = 'xsi:schemaLocation'">
						<xsl:attribute name="{name(.)}"><xsl:value-of select="'http://www.developer.cognos.com/schemas/bmt/3/0 BMTModelSpecification.xsd'"/></xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--rename languages to locales-->
	<xsl:template match="bmtcr1:languages">
		<xsl:element name="locales">
			<xsl:for-each select="@*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--rename language to locale-->
	<xsl:template match="bmtcr1:language">
		<xsl:element name="locale">
			<xsl:for-each select="@*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--rename defaultLanguage to defaultLocale-->
	<xsl:template match="bmtcr1:defaultLanguage">
		<xsl:element name="defaultLocale">
			<xsl:for-each select="@*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--adding the sources element to a dbQueryand sub-class the source sub-elements-->
	<xsl:template match="bmtcr1:dbQuery">
		<xsl:element name="dbQuery">
			<xsl:for-each select="@*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:element name="sources">
				<xsl:for-each select="*">
					<xsl:if test="name(.) = 'source'">
						<xsl:element name="{name(.)}">
							<xsl:for-each select="@*">
								<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
							</xsl:for-each>
							<xsl:apply-templates/>
						</xsl:element>
					</xsl:if>
				</xsl:for-each>
			</xsl:element>
			<xsl:for-each select="*">
				<xsl:if test="not(name(.) = 'source')">
					<xsl:element name="{name(.)}">
						<xsl:for-each select="@*">
							<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
						</xsl:for-each>
						<xsl:apply-templates/>
					</xsl:element>
				</xsl:if>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<!--add the sources and source elements to a modelQuery-->
	<xsl:template match="bmtcr1:modelQuery">
		<xsl:element name="modelQuery">
			<xsl:for-each select="@*">
				<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:element name="sources">
				<xsl:for-each select="*">
					<xsl:if test="name(.) = 'source'">
						<xsl:element name="{name(.)}">
							<xsl:for-each select="@*">
								<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
							</xsl:for-each>
							<xsl:apply-templates/>
						</xsl:element>
					</xsl:if>
				</xsl:for-each>
			</xsl:element>
			<xsl:for-each select="*">
				<xsl:if test="not(name(.) = 'source')">
					<xsl:element name="{name(.)}">
						<xsl:for-each select="@*">
							<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
						</xsl:for-each>
						<xsl:apply-templates/>
					</xsl:element>
				</xsl:if>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<!--remove packages and subtree completely-->
	<xsl:template match="*[local-name() = 'packages']"/>
</xsl:stylesheet>