<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
	xmlns:xa="urn:schemas-microsoft-com:xml-analysis:mddataset"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	exclude-result-prefixes="msxsl xa user"
	xmlns:user="http://www.intellimaxsolutions.com">

<!--
<msxsl:script implements-prefix="user" language="JScript"><![CDATA[
		var nfx = /(\$*)(\d*)(\,*)(\d*)(\.*)(\d*)(\%*)/

		function fn2nf(str){
			var m = str.match(nfx);
			var nf = m[1] + "#" + m[3];
			if (m[3]) 
				nf += "#";
			nf += m[5];
			for (var i=0; i<m[6].length; i++)
				nf += "0";
			nf += m[7];
			return (nf);
		} 

]]></msxsl:script>
-->

	<xsl:output method="xml"/>

	<xsl:param name="title"/>

	<xsl:key name="Cell" match="xa:root/xa:CellData/xa:Cell" use="@CellOrdinal"/>
	<xsl:key name="Axis" match="xa:root/xa:Axes/xa:Axis" use="@name"/>

	<!-- Root -->
	<xsl:template match="xa:root">
		<results>
		<title><xsl:value-of select="$title" /></title>
		<xsl:choose>
			<xsl:when test="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis2']">
				<alert>Only two axes are supported!</alert>
			</xsl:when>
			<xsl:when test="count(//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis0']/xa:HierarchyInfo) &gt; 1">
				<alert>Only one column hierarcy supported</alert>
			</xsl:when>
			<xsl:when test="count(//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis1']/xa:HierarchyInfo) &gt; 1">
				<alert>Only one row hierarcy supported</alert>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis0']">
					<xsl:apply-templates select="//xa:Axes/xa:Axis[@name='Axis0']" />
					<xsl:if test="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis1']">
						<xsl:apply-templates select="//xa:Axes/xa:Axis[@name='Axis1']" />
					</xsl:if>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
		</results>
	</xsl:template>

	<!-- Columns -->
	<xsl:template match="xa:Axis[@name='Axis0']">
		<xsl:variable name="varAxisTuples" select="xa:Tuples/xa:Tuple" />
		<xsl:variable name="varHierarchy" select="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis0']/xa:HierarchyInfo" />
<!-- 
Clean this up in terms of reuse with varHierarcy for rows
Perhaps defile varColumnHierarcy and varRowHierarcy up front
-->
		<xsl:variable name="varAxis1Hierarchy" select="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis1']/xa:HierarchyInfo" />
			<columns>

			<headingheading><xsl:value-of select="$varAxis1Hierarchy/@name" /></headingheading>
			<!-- Get the axis members for the column axis -->
			<xsl:for-each select="//xa:Axes/xa:Axis[@name='Axis0']/xa:Tuples/*">
				<xsl:variable name="varAxisMember" select="." />
				<xsl:variable name="varPos" select="position()-1" />
				<xsl:variable name="varMemDimCols" select="$varAxisMember/xa:Member[@Hierarchy = $varHierarchy/@name]" />
				<!-- get rid of any $ added to WITH statement measure -->
				<xsl:variable name="varMemCapValue" select="translate($varMemDimCols/xa:Caption,'$','')" />
				<heading format="{key('Cell', $varPos)/xa:FmtValue}"><xsl:value-of select="$varMemCapValue" /></heading>

				<!-- create a format string from the formatted value of the first cell in this column -->
				<!--<heading format="{user:fn2nf(string(key('Cell', $varPos)/xa:FmtValue))}"><xsl:value-of select="$varMemCapValue" /></heading> -->

			</xsl:for-each>
			</columns>

	</xsl:template>

	<!-- Rows -->
	<xsl:template match="//xa:Axis[@name='Axis1']/xa:Tuples/xa:Tuple">
		<xsl:variable name="varAxisMember" select="." />
		<xsl:variable name="varAxisTuples" select="/xa:root/xa:Axes/xa:Axis[@name = 'Axis1']/xa:Tuples/xa:Tuple" />
		<xsl:variable name="varTupleOrdinal" select="position()-1" />

		<row>
		<xsl:variable name="varHierarchy" select="//xa:OlapInfo/xa:AxesInfo/xa:AxisInfo[@name='Axis1']/xa:HierarchyInfo" />
		<xsl:variable name="varMemDimCols" select="$varAxisMember/xa:Member[@Hierarchy = $varHierarchy/@name]" />
		<xsl:variable name="varMemCapValue" select="$varMemDimCols/xa:Caption" />

		<heading><xsl:value-of select="$varMemCapValue" /></heading>

		<!-- Display the cells for this tuple -->
		<xsl:variable name="varAxis0Coords" select="count(//xa:Axes/xa:Axis[@name='Axis0']/xa:Tuples/xa:Tuple)" />

		<xsl:call-template name="PrintCells">
			<xsl:with-param name="varStartCell" select="$varTupleOrdinal * $varAxis0Coords" />
			<xsl:with-param name="varEndCell" select="($varTupleOrdinal+1) * $varAxis0Coords" />
		</xsl:call-template>

		</row>
	</xsl:template>

	<!-- Print out cells within the range of ordinals -->
	<xsl:template name="PrintCells">
		<xsl:param name="varStartCell">0</xsl:param>
		<xsl:param name="varEndCell">0</xsl:param>
		<xsl:param name="varColCount">1</xsl:param>

		<xsl:if test="$varStartCell &lt; $varEndCell">
			<cell>
			<xsl:choose>
				<xsl:when test="key('Cell', $varStartCell)">
					<xsl:attribute name="value">
<xsl:variable name="varDecode" select="key('Cell', $varStartCell)/xa:Value" />
<xsl:choose>
	<xsl:when test="contains($varDecode,'IND')">0</xsl:when>
	<xsl:otherwise><xsl:value-of select="$varDecode" /></xsl:otherwise>
</xsl:choose>
					</xsl:attribute>
					<xsl:value-of select="key('Cell', $varStartCell)/xa:FmtValue" />
				</xsl:when>
				<xsl:otherwise><xsl:attribute name="value">0</xsl:attribute>0</xsl:otherwise>
			</xsl:choose>
			</cell>
			<xsl:call-template name="PrintCells">
				<xsl:with-param name="varStartCell" select="$varStartCell + 1" />
				<xsl:with-param name="varEndCell" select="$varEndCell" />
				<xsl:with-param name="varColCount" select="$varColCount + 1" />
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="no" externalpreview="no" url="file:///c:/work/MDX/XML/FoodMart 2000/foodmart basic2.xml" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->