Few days before I came across one XSL transformation business requirement where an xml element of Input data is having two sub elements named “NumberBlockVON” and “NumberBlockBIS” . Name of these two is mentioned by our business user. “NumberBlockVON” will have a positive Integer value and same happens for “NumberBlockBIS” . Now the requirement was whatever value specified as “NumberBlockBIS” that will be treated in output xml as “the power of” the value specified for “NumberBlockVON” . It means in out put xml “NumberBlockVON” will pass as it is but the value for “NumberBlockBIS” will be replaced by the <NumberBlockVON> to the power of <NumberBlockBIS>.
<PickPackShipCustomElement> <NumberBlockVON>5</NumberBlockVON> <NumberBlockBIS>6</NumberBlockBIS> </PickPackShipCustomElement>
We use a custom XSL template to achieve this and call the template recursively with tuned parameters.<NumberBlockVON> value and <NumberBlockBIS>value is passed as a parameter to the template.How we achieve this is explained below with code snippet.
<xsl:templatename="whileImplTemplate">
< !--parameter limit_var is used to hold value of NumberBlockBIS which is used as a power value-->
<xsl:paramname="limit_var"/>< !--parameter var is used to hold value of NumberBlockVON which is used as a base value-->
<xsl:param name="var"/>< !--parameter Outvar is used to hold output value-->
<xsl:param name="Outvar" select="1"/> <xsl:param name="count_index" select="0"/> <xsl:variable name="out_var" select="$Outvar"/>< !--while logic Implementation-->
<xsl:choose>< !--Is limit achieved --> <xsl:when test="$count_index < $limit_var"> < !--recursive template call if limit does not achieved-->
<xsl:call-template name="whileImplTemplate"> <xsl:with-param name="limit_var" select="$limit_var"/> <xsl:with-param name="var" select="$var"/> <xsl:with-param name="count_index" select="number($count_index)+1"/> <xsl:with-param name="Outvar" select="number($var)*number($out_var)"/> </xsl:call-template>
< !-- End template --></xsl:when> <xsl:otherwise> <xsl:value-of select="$out_var"/>
< !--If limit achieved then print value --> </xsl:otherwise> <xsl:choose> </xsl:template>
<PickPackElement> <NumberBlockVON>5</NumberBlockVON> <NumberBlockBIS>15625</NumberBlockBIS> </PickPackElement>