<?xml version="1.0" encoding="UTF-8"?>
<!--
	Licensed Materials - Property of IBM
	IBM Cognos Products: ans
	(C) Copyright IBM Corp. 2005, 2010
	US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-->


<!--
*  Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
*  Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
*  
* 
*
* This stylesheet generates a most update Analysis High Level Specification (HLS) 
* from a beta version.
*
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:exsl="http://exslt.org/common" xmlns:xalan="http://xml.apache.org/xalan" xmlns:java="http://xml.apache.org/xalan/java" extension-element-prefixes="xalan date exsl" exclude-result-prefixes="xalan java date exsl">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes"/>
	<!--global variables-->
	<xsl:variable name="schema0.1" select="number(0.1)"/>
	<xsl:variable name="schema0.101" select="number(0.101)"/>
	<xsl:variable name="schema0.102" select="number(0.102)"/>
	<xsl:variable name="schema0.103" select="number(0.103)"/>
	<xsl:variable name="schema0.104" select="number(0.104)"/>
	<xsl:variable name="schema0.105" select="number(0.105)"/>
	<xsl:variable name="schema0.106" select="number(0.106)"/>
	<xsl:variable name="schema0.107" select="number(0.107)"/>
	<xsl:variable name="schema0.108" select="number(0.108)"/>
	<xsl:variable name="schema0.109" select="number(0.109)"/>
	<xsl:variable name="schema1.0" select="number(1.0)"/>
	<xsl:variable name="schema1.1" select="number(1.1)"/>
	<xsl:variable name="currentSchema" select="number(1.1)"/>
	<xsl:variable name="reportSchema">
		<xsl:value-of select="number(//analysisReport/attribute::schemaVersion)"/>
	</xsl:variable>
	<xsl:template match="/PersistHLS">
		<!-- Once a point of time we had PersistHLS as the fake root. 
                              New report should have analysisReport as the root. 
                              Here we make sure the fake root is removed during upgrade-->
		<xsl:apply-templates select="analysisReport"/>
	</xsl:template>
	<xsl:template match="analysisReport">
		<xsl:choose>
			<xsl:when test="$reportSchema &lt; $currentSchema">
				<xsl:element name="analysisReport">
					<xsl:copy-of select="@*"/>
					<xsl:attribute name="schemaVersion"><xsl:value-of select="$currentSchema"/></xsl:attribute>
					<xsl:comment>upgrade from version 
          <xsl:value-of select="$reportSchema"/>
					</xsl:comment>
					<xsl:if test="not(subtotalEnablementOptions)">
						<!-- 
							Prior to 0.106 we did not have subtotalEnablementOptions. The default behavior for remainderMode was
							subtract as opposed to null. The default behavior for filterExcludedEnabled was true as opposed to false.
						-->
						<xsl:element name="subtotalEnablementOptions">
							<xsl:attribute name="filterExcludedEnabled">true</xsl:attribute>
							<xsl:attribute name="filterIncludedEnabled">true</xsl:attribute>
							<xsl:attribute name="remainderEnabled">true</xsl:attribute>
							<xsl:attribute name="subtotalVisibleEnabled">false</xsl:attribute>
							<xsl:attribute name="summaryEnabled">true</xsl:attribute>
							<xsl:attribute name="visibleMembersEnabled">true</xsl:attribute>
							<xsl:attribute name="remainderMode">subtract</xsl:attribute>
						</xsl:element>
					</xsl:if>
					<!--
						Convert the 8.1 suppression to work order 2567 suppression.  Test for each variant separately
					-->
					<xsl:if test="filterSet/zeroSuppress">
						<xsl:if test="not(otherOptions)">
							<xsl:element name="otherOptions">
								<xsl:attribute name="treatZerosAsNullsForSuppression">true</xsl:attribute>
								<xsl:attribute name="suppressionState"><xsl:value-of select="/analysisReport/filterSet/zeroSuppress/@state"/></xsl:attribute>
							</xsl:element>
						</xsl:if>
						<xsl:apply-templates>
							<xsl:with-param name="suppressionStateName">suppressionState</xsl:with-param>
							<xsl:with-param name="suppressionStateValue" select="/analysisReport/filterSet/zeroSuppress/@state"/>
							<xsl:with-param name="treatmentName">treatZerosAsNullsForSuppression</xsl:with-param>
							<xsl:with-param name="treatmentValue">true</xsl:with-param>
						</xsl:apply-templates>
					</xsl:if>
					<xsl:if test="(filterSet/nullSuppress) and not (filterSet/zeroSuppress)">
						<xsl:if test="not(otherOptions)">
							<xsl:element name="otherOptions">
								<xsl:attribute name="treatZerosAsNullsForSuppression">false</xsl:attribute>
								<xsl:attribute name="suppressionState"><xsl:value-of select="/analysisReport/filterSet/nullSuppress/@state"/></xsl:attribute>
							</xsl:element>
						</xsl:if>
						<xsl:apply-templates>
							<xsl:with-param name="suppressionStateName">suppressionState</xsl:with-param>
							<xsl:with-param name="suppressionStateValue" select="/analysisReport/filterSet/nullSuppress/@state"/>
							<xsl:with-param name="treatmentName">treatZerosAsNullsForSuppression</xsl:with-param>
							<xsl:with-param name="treatmentValue">true</xsl:with-param>
						</xsl:apply-templates>
					</xsl:if>
					<xsl:if test="not(filterSet/nullSuppress) and not (filterSet/zeroSuppress)">
						<xsl:choose>
							<xsl:when test="not(otherOptions)">
								<xsl:apply-templates/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:apply-templates>
									<xsl:with-param name="treatmentName">treatZerosAsNullsForSuppression</xsl:with-param>
									<xsl:with-param name="treatmentValue" select="/analysisReport/otherOptions/@treatZerosAsNullForSparsityRemoval"/>
									<xsl:with-param name="suppressionStateName">suppressionState</xsl:with-param>
									<xsl:with-param name="suppressionStateValue">none</xsl:with-param>
								</xsl:apply-templates>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:if>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="otherOptions">
		<xsl:param name="suppressionStateName"/>
		<xsl:param name="suppressionStateValue"/>
		<xsl:param name="treatmentName"/>
		<xsl:param name="treatmentValue"/>
		<xsl:element name="otherOptions">
			<xsl:choose>
				<xsl:when test="@treatZerosAsNullsForSparsityRemoval">
					<xsl:attribute name="{$suppressionStateName}"><xsl:value-of select="$suppressionStateValue"/></xsl:attribute>
					<xsl:attribute name="treatZerosAsNullsForSuppression"><xsl:value-of select="@treatZerosAsNullsForSparsityRemoval"/></xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="{$suppressionStateName}"><xsl:value-of select="$suppressionStateValue"/></xsl:attribute>
					<xsl:attribute name="{$treatmentName}"><xsl:value-of select="$treatmentValue"/></xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<!--remove old zero and null suppress.  should be upgraded by this point.-->
	<xsl:template match="filterSet/zeroSuppress">
	</xsl:template>
	<xsl:template match="filterSet/nullSuppress">
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions">
		<xsl:element name="subtotalEnablementOptions">
			<xsl:if test="not(@remainderMode) or not(@remaindermode)">
				<xsl:attribute name="remainderMode">subtract</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="@*"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="measureSortSpec|topBottomFilter">
		<xsl:element name="{name()}">
			<xsl:copy-of select="@*"/>
			<xsl:choose>
				<xsl:when test="$reportSchema &lt; $schema0.101">
					<xsl:variable name="v_locked">
						<xsl:value-of select="ancestor::*/properties/@locked"/>
					</xsl:variable>
					<xsl:element name="scope">
						<xsl:choose>
							<xsl:when test="context">
								<xsl:element name="memberExprs">
									<xsl:copy-of select="context/*"/>
								</xsl:element>
							</xsl:when>
							<xsl:otherwise>
								<!-- add <defaultMemberContext> for not locked block -->
								<xsl:if test="$v_locked != 'true'">
									<xsl:element name="memberExprs">
										<xsl:element name="defaultMemberContext"/>
									</xsl:element>
								</xsl:if>
							</xsl:otherwise>
						</xsl:choose>
						<xsl:choose>
							<xsl:when test="measure">
								<xsl:element name="measureExpr">
									<xsl:copy-of select="measure"/>
								</xsl:element>
							</xsl:when>
							<xsl:when test="calcRef">
								<xsl:element name="measureExpr">
									<xsl:copy-of select="calcRef"/>
								</xsl:element>
							</xsl:when>
							<xsl:otherwise>
								<xsl:element name="measureExpr">
									<xsl:element name="measure">default</xsl:element>
								</xsl:element>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:element>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<xsl:template match="scope">
		<xsl:variable name="v_locked">
			<xsl:value-of select="ancestor::*/properties/@locked"/>
		</xsl:variable>
		<!-- For analysis before 0.104, we didn't distinguish empty and default member context. 
                      So we need to upgrade those analysis to use default context for any non-locked block. From 0.104, we
                      will write out <defaultMemberContext> for default context and empty for empty context. -->
		<xsl:choose>
			<xsl:when test="$v_locked != 'true' and $reportSchema &lt; $schema0.104">
				<xsl:element name="{name()}">
					<xsl:copy-of select="@*"/>
					<xsl:if test="not(memberExprs)">
						<xsl:element name="memberExprs">
							<xsl:element name="defaultMemberContext"/>
						</xsl:element>
					</xsl:if>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="scatterchartoptions">
		<!-- Scatter charts are no longer supported. -->
	</xsl:template>
	<xsl:template match="childrenOfMeasureBlock/hiddenItems/members">
		<!-- change <members> to <measures>, and <member> to <measure> -->
		<xsl:element name="measures">
			<xsl:for-each select="./member">
				<xsl:element name="measure">
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<xsl:template match="attributes/members">
		<!-- Change <attributes><members><member> to <attributes><attribute> -->
		<xsl:for-each select="./member">
			<xsl:element name="attribute">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="ci[@type='context']">
		<!-- we should remove any default context for locked block -->
		<xsl:variable name="v_locked">
			<xsl:value-of select="ancestor::*/properties/@locked"/>
		</xsl:variable>
		<xsl:variable name="v_value" select="."/>
		<xsl:if test="$v_locked != 'true' or $v_value != 'default'">
			<xsl:copy-of select="."/>
		</xsl:if>
	</xsl:template>
	<xsl:template match="childrenOfBlock|childrenOfMeasureBlock|depthBlock|nestedBlock|levelBlock|namedSetBlock|v5Block|memberBlock">
		<xsl:element name="{name()}">
			<xsl:copy-of select="@*"/>
			<xsl:if test="name() != 'memberBlock'">
				<xsl:if test="not(properties)">
					<xsl:element name="properties">
						<xsl:attribute name="remainderMode">subtract</xsl:attribute>
					</xsl:element>
				</xsl:if>
			</xsl:if>
			<xsl:if test="name() != 'childrenOfMeasureBlock'">
				<xsl:if test="not(subtotals)">
					<xsl:element name="subtotals">
						<xsl:attribute name="excluded">true</xsl:attribute>
					</xsl:element>
				</xsl:if>
			</xsl:if>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="childrenOfBlock/properties|childrenOfMeasureBlock/properties|depthBlock/properties|nestedBlock/properties|levelBlock/properties|namedSetBlock/properties|v5Block/properties">
		<xsl:element name="{name()}">
			<xsl:if test="not(@remainderMode) or not(@remaindermode)">
				<xsl:attribute name="remainderMode">subtract</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="@*"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="childrenOfBlock/subtotals|memberBlock/subtotals|depthBlock/subtotals|nestedBlock/subtotals|levelBlock/subtotals|namedSetBlock/subtotals|v5Block/subtotals">
		<xsl:element name="{name()}">
			<xsl:if test="not(@excluded)">
				<xsl:attribute name="excluded">true</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="@*"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="reportOptions/pageBreaks">
		<xsl:variable name="section" select="./@*[name() = 'section']"/>
		<xsl:element name="{name()}">
			<xsl:for-each select="@*">
				<xsl:if test="name() != 'section'">
					<xsl:copy-of select="."/>
				</xsl:if>
			</xsl:for-each>
			<xsl:if test="$section">
				<xsl:element name="sectionFilterId">
					<xsl:attribute name="value"><xsl:choose><xsl:when test="starts-with($section, 'uuid:')"><xsl:value-of select="concat('x', substring($section, 6))"/></xsl:when><xsl:otherwise><xsl:value-of select="$section"/></xsl:otherwise></xsl:choose></xsl:attribute>
				</xsl:element>
			</xsl:if>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>
	<!--
		Old reports (< 0.109) did not have an axisExpr element between the rows|columns 
        element and the expression elements. In that case we need to add an 
        axisExpr element.

        The original upgrade (0.109 - 1.0) rule did NOT check to see if we already had an 
        axisExpr element so subsequent upgrades added superflous axisExpr
        elements. If see those we have to skip/delete them.

	  NOTE: THE ORDER OF THESE TEMPLATES IS IMPORTANT.
-->
	<xsl:template match="dataMatrix/rows/axisExpr|dataMatrix/columns/axisExpr">
		<xsl:choose>
			<xsl:when test="axisExpr">
				<!-- Yikes we have nested axisExprs. Ignore the nested one and process it's children. -->
				<xsl:apply-templates/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="axisExpr">
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="dataMatrix/rows|dataMatrix/columns">
		<!-- We need to add an axisExpr -->
		<xsl:element name="{name()}">
			<xsl:choose>
				<xsl:when test="not(axisExpr)">
					<xsl:element name="axisExpr">
						<xsl:apply-templates/>
					</xsl:element>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<!-- 
Interim upgrade  from 0.103-0.106
We want attributes to be consistently named, capitalized, etc
-->
	<xsl:template match="subtotalEnablementOptions/@filterexcludedenabled">
		<xsl:attribute name="filterExcludedEnabled"><xsl:value-of select="../@filterexcludedenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@filterincludedenabled">
		<xsl:attribute name="filterIncludedEnabled"><xsl:value-of select="../@filterincludedenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@remainderenabled">
		<xsl:attribute name="remainderEnabled"><xsl:value-of select="../@remainderenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@subtotalvisibleenabled">
		<xsl:attribute name="subtotalVisibleEnabled"><xsl:value-of select="../@subtotalvisibleenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@summaryenabled">
		<xsl:attribute name="summaryEnabled"><xsl:value-of select="../@summaryenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@visiblemembersenabled">
		<xsl:attribute name="visibleMembersEnabled"><xsl:value-of select="../@visiblemembersenabled"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="subtotalEnablementOptions/@remaindermode">
		<xsl:attribute name="remainderMode"><xsl:value-of select="../@remaindermode"/></xsl:attribute>
	</xsl:template>
	<xsl:template match="properties/@remaindermode">
		<xsl:attribute name="remainderMode"><xsl:value-of select="../@remaindermode"/></xsl:attribute>
	</xsl:template>
	<!-- Copy anything that is unmatched over to output. -->
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>