<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<xsl:output method="xml" /> 
<xsl:template match="/pega">
	<xsl:apply-templates /> 
</xsl:template>
<xsl:template match="page">
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
	<ns1:endSession xmlns:ns1="urn:CampaignService" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
	<requestVo xmlns:ns2="http://request.vo.campaign.os.sears.com/" xsi:type="ns2:EndSessionRequestVO">
	<clientId xsi:type="xsd:string">
		<xsl:value-of select="tag[@name='CLIENTID']" /> 
	</clientId>

	<!-- 
	call template for leads items array and initialize counter at the value of 1 
	-->  
	<leads xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Array" xmlns:ns4="http://vo.campaign.os.sears.com/" ns3:arrayType="ns4:EndSessionLeadVO[]">
		<xsl:call-template name="buildLeadsArr">
		<xsl:with-param name="repeat" select="number(1)" /> 
		</xsl:call-template>
		<!-- 
		call template for propositions items array and initialize counter at the value of 1 
		--> 
		<propositions xsi:type="ns3:Array" ns3:arrayType="ns4:EndSessionLeadPropositionVO[]">
		<xsl:call-template name="buildpropositionsArr">
		<xsl:with-param name="repeat" select="number(1)" /> 
		</xsl:call-template>
		</propositions>
	</leads>

	<sessionId xsi:type="xsd:string">
		<xsl:value-of select="tag[@name='SESSIONID']" /> 
		</sessionId>
	</requestVo>
	</ns1:endSession>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</xsl:template>

	<!-- 
	build array for leads items with the array maximum being limited to the counter value 
	--> 
	<xsl:template name="buildLeadsArr">
		<xsl:param name="repeat" /> 
		<xsl:variable name="LeadsArrCount" select="number(tag[@name='LEADSARRCNT'])" /> 
		<xsl:if test="number($repeat) &lt;= $LeadsArrCount">
		<item xsi:type="ns4:EndSessionLeadVO">
			<leadStatus xsi:type="xsd:string">
			<xsl:value-of select="tag[@name=concat('LEADSTAT(',$repeat,')')]" /> 
			</leadStatus>
			<orderType xsi:type="xsd:string">
			<xsl:value-of select="tag[@name=concat('ORDERTYPE(',$repeat,')')]" /> 
			</orderType>
			<orderReference xsi:type="xsd:string">
			<xsl:value-of select="tag[@name=concat('ORDERREFF(',$repeat,')')]" /> 
			</orderReference>
			<interactionId xsi:type="xsd:string">
			<xsl:value-of select="tag[@name=concat('INTERACTID(',$repeat,')')]" /> 
			</interactionId>
		</item>
		<xsl:call-template name="buildLeadsArr">
		<xsl:with-param name="repeat" select="$repeat +1" /> 
		</xsl:call-template>
		</xsl:if>
	</xsl:template>
				<xsl:template name="buildpropositionsArr">
				<xsl:param name="repeat" /> 
				<xsl:variable name="propositionsArrCount" select="number(tag[@name='PROARRCNT'])" /> 
				<xsl:if test="number($repeat) &lt;= $propositionsArrCount">
					<item xsi:type="ns4:EndSessionLeadPropositionVO">
					<statusCode xsi:type="xsd:string">
					<xsl:value-of select="tag[@name=concat('STATCODE(',$repeat,')')]" /> 
					</statusCode>
					<reasonCode xsi:type="xsd:string">
					<xsl:value-of select="tag[@name=concat('RESCODE(',$repeat,')')]" /> 
					</reasonCode>
					<interactionId xsi:type="xsd:string">
					<xsl:value-of select="tag[@name=concat('INTRID(',$repeat,')')]" /> 
					</interactionId>
					</item>
				<xsl:call-template name="buildpropositionsArr">
				<xsl:with-param name="repeat" select="$repeat +1" /> 
				</xsl:call-template>
				</xsl:if>
			  </xsl:template>
</xsl:transform>