<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:variable name="Val" select="number(Main/Step1/Step4[Name = 'CountVal']/Value/text())"/>
	<xsl:variable name="Ret" select="number(Main/Step1/Step4[Name = 'CountRet']/Value/text())"/>

	<xsl:template match="/">
		<Main>
			<xsl:apply-templates select="Main/Step1/Step4[Name = 'XSL1Val']">
				<xsl:with-param name="CountVal" select="$Val"/>
				<xsl:with-param name="CountRet" select="$Ret"/>

				<xsl:with-param name="ValIndex" select="Main/Step1/Step4[Name = 'VALIndex']/Value/text()"/>
				<xsl:with-param name="RetIndex" select="Main/Step1/Step4[Name = 'RETIndex']/Value/text()"/>
			</xsl:apply-templates>
		</Main>
	</xsl:template>

	<xsl:template match="Step4">
		<xsl:param name="CountVal"/>
		<xsl:param name="CountRet"/>

		<xsl:param name="ValIndex"/>
		<xsl:param name="RetIndex"/>

		<xsl:if test="$CountVal &gt; 0 or $CountRet &gt; 0">
			<Table1>
				<XSLVal>
					<xsl:value-of select="Value"/>
				</XSLVal>
				<XSLRet>

					<xsl:choose>
						<xsl:when test="following-sibling::Step4[Name = concat('XSL', $Ret - $CountRet + 1,'Ret')]/Value">
							<xsl:value-of select="following-sibling::Step4[Name = concat('XSL', $Ret - $CountRet + 1,'Ret')]/Value"/>
						</xsl:when>
						<xsl:otherwise>-</xsl:otherwise> 
					</xsl:choose>
				</XSLRet>
				<ValIndex>
					<xsl:value-of select="substring-before($ValIndex, ';')"/>

				</ValIndex>
				<RetIndex>
					<xsl:value-of select="substring-before($RetIndex, ';')"/>
				</RetIndex>
			</Table1>

			<xsl:apply-templates select="following-sibling::Step4[Name = concat('XSL', $Val - $CountVal + 2,'Val')]">
				<xsl:with-param name="CountVal" select="$CountVal - 1"/>
				<xsl:with-param name="CountRet" select="$CountRet - 1"/>

				<xsl:with-param name="ValIndex" select="substring-after($ValIndex, ';')"/>
				<xsl:with-param name="RetIndex" select="substring-after($RetIndex, ';')"/>
			</xsl:apply-templates>
		</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="xsltval.xml" userelativepaths="yes" externalpreview="no" url="xsltval.xml" htmlbaseurl="" outputurl="" processortype="saxon8" 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>
-->