<?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 4-->
<!--XSL Transformation from a schema v4 model to a valid BMT schema v5 model-->
<xsl:stylesheet xmlns="http://www.developer.cognos.com/schemas/bmt/5/0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<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 v5 model-->
		<xsl:if test="/*[local-name() = 'project']/@xsi:schemaLocation = 'http://www.developer.cognos.com/schemas/bmt/5/0 BMTModelSpecification.xsd'">
			<xsl:message terminate="yes">The input file for this transformer must be a schema v4 model.  You tried to use a schema v5 model.</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:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
			</xsl:for-each>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--change the schema to v5 version-->
	<xsl:template match="*[local-name() = 'project']">
	<!--Count and store the number of 'parameterMaps' entities and 'securityViews' entities.-->
		<xsl:variable name="parameterMapFlag" select="count(*[name()='parameterMaps'])"/>
		<xsl:variable name="securityViewFlag" select="count(*[name()='securityViews'])"/>
		<xsl:element name="project" namespace="http://www.developer.cognos.com/schemas/bmt/5/0">
			<xsl:for-each select="@*">
				<xsl:choose>
					<!--change schema from v4 to v5-->
					<xsl:when test="name(.) = 'xsi:schemaLocation'">
						<xsl:attribute name="{name(.)}"><xsl:value-of select="'http://www.developer.cognos.com/schemas/bmt/5/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/>
			<!--If these don't exist, create them.--> 
			<xsl:if test="not($parameterMapFlag &gt; 0)">
				<xsl:element name="parameterMaps"/>
			</xsl:if>
			<xsl:if test="not($securityViewFlag &gt; 0)">
				<xsl:element name="securityViews"/>
			</xsl:if>
		</xsl:element>
	</xsl:template>
	
	<!--remove securityView (but not securityViews container) completely-->
	<xsl:template match="*[local-name() = 'securityView']"/>
	<!--remove packages and subtree completely-->
	<xsl:template match="*[local-name() = 'packages']"/>
	<!--remove externalSource from parameterMap-->
	<xsl:template match="*[local-name() = 'externalSource']"/>
	<!--add mandatory elements to queryItem-->
	<xsl:template match="*[local-name() = 'queryItem']">
		<xsl:choose>
			<!--remove entire queryItem if it has a calculation-->
			<xsl:when test="./*[local-name() = 'calculation']">
				<!--do nothing-->
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="queryItem">
					<xsl:for-each select="@*">
						<xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
					</xsl:for-each>
					<xsl:apply-templates select="*[local-name() = 'name']"/>
					<xsl:apply-templates select="*[local-name() = 'description']"/>
					<xsl:apply-templates select="*[local-name() = 'lastChanged']"/>
					<!--copy over columnName-->
					<xsl:apply-templates select="*[local-name() = 'columnName']"/>
					<!--properties are now mandatory; create with defaults if not already existing-->
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'usage']">
							<xsl:apply-templates select="*[local-name() = 'usage']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="usage">kUsageIdentifier</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'format']">
							<xsl:apply-templates select="*[local-name() = 'format']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="format"/>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'datatype']">
							<xsl:apply-templates select="*[local-name() = 'datatype']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="datatype">kDataTypeDecimal</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'scale']">
							<xsl:apply-templates select="*[local-name() = 'scale']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="scale">0</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'size']">
							<xsl:apply-templates select="*[local-name() = 'size']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="size">20</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'nullable']">
							<xsl:apply-templates select="*[local-name() = 'nullable']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="nullable">0</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'regularAggregate']">
							<xsl:apply-templates select="*[local-name() = 'regularAggregate']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="regularAggregate">kAggregateRuleUnsupported</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'semiAggregate']">
							<xsl:apply-templates select="*[local-name() = 'semiAggregate']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="semiAggregate">kAggregateRuleUnsupported</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'precision']">
							<xsl:apply-templates select="*[local-name() = 'precision']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="precision">38</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'displayType']">
							<xsl:apply-templates select="*[local-name() = 'displayType']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="displayType">LargeText</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'isFew']">
							<xsl:apply-templates select="*[local-name() = 'isFew']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="isFew">false</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'isPromptLabel']">
							<xsl:apply-templates select="*[local-name() = 'isPromptLabel']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="isPromptLabel">false</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'collationSequenceName']">
							<xsl:apply-templates select="*[local-name() = 'collationSequenceName']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="collationSequenceName">C</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'collationSequenceLevel']">
							<xsl:apply-templates select="*[local-name() = 'collationSequenceLevel']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="collationSequenceLevel">2</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'originalCollationSequenceName']">
							<xsl:apply-templates select="*[local-name() = 'originalCollationSequenceName']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="originalCollationSequenceName">BINARY</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:choose>
						<xsl:when test="./*[local-name() = 'originalEncodingName']">
							<xsl:apply-templates select="*[local-name() = 'originalEncodingName']"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="originalEncodingName">utf8</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
					<!--non-properties-->
					<xsl:apply-templates select="*[local-name() = 'templates']"/>
					<xsl:apply-templates select="*[local-name() = 'property']"/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>