NiceXSL logo Contents Download Update-Site NiceXSL logo

NiceXSL POW Example

This example applies the XSL2NiceXSL.xsl reverse engineering script to make the pow.xsl style sheet from FXSL more readable and maintainable.

pow.xsl


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:myMultiply="f:myMultiply" 
 exclude-result-prefixes="xsl myMultiply"
 >
 
  <xsl:import href="curry.xsl"/> 
  <xsl:import href="iter.xsl"/>

  <myMultiply:myMultiply/>
 
  <!-- This template implements the following general power fn:
  
	> power :: (Ord a, Num a, Num b) => a -> b -> b
	> power n x = iter n (multByX x) 1
	
	where
	> multByX :: Num a => a -> a -> a
	> multByX x y = (*x) y
	
	Note the use of the partial application in multByX !
    
    -->
  
  <xsl:template name="pow">
    <xsl:param name="pTimes" select="0"/>
    <xsl:param name="pX"/>
    
    <xsl:variable name="vMultiply" select="document('')/*/myMultiply:*[1]"/>
    
    <xsl:variable name="vrtfCurriedMultByX">
	  <xsl:call-template name="curry">
	    <xsl:with-param name="pFun" select="$vMultiply"/>
	    <xsl:with-param name="pNargs" select="2"/>
      <xsl:with-param name="arg2" select="$pX"/>
	  </xsl:call-template>
    </xsl:variable>
    
    <xsl:variable name="vCurriedMultByX" 
                  select="$vrtfCurriedMultByX/node()"/>
    
    <xsl:call-template name="iter">
      <xsl:with-param name="pTimes" select="$pTimes"/>
      <xsl:with-param name="pFun" select="$vCurriedMultByX"/>
      <xsl:with-param name="pX" select="1"/>
    </xsl:call-template>
    
  </xsl:template>

  
  <xsl:template match="myMultiply:*">
    <xsl:param name="arg1"/>
    <xsl:param name="arg2"/>
  
    <xsl:value-of select="$arg1 * $arg2"/>
  </xsl:template>

</xsl:stylesheet><

pow.nxsl


<!-- Generated by XSL2NiceXSL.xsl 2.0.2 -->

stylesheet version="2.0" exclude-result-prefixes="xsl myMultiply"
    xmlns:myMultiply="f:myMultiply" {
    import "curry.xsl";
    import "iter.xsl";
    <myMultiply:myMultiply>

    <!-- This template implements the following general power fn:
  
	> power :: (Ord a, Num a, Num b) => a -> b -> b
	> power n x = iter n (multByX x) 1
	
	where
	> multByX :: Num a => a -> a -> a
	> multByX x y = (*x) y
	
	Note the use of the partial application in multByX !
    
    -->
    template pow( pTimes := 0,  pX) {
        variable vMultiply := document('')/*/myMultiply:*[1];
        variable vrtfCurriedMultByX :=  {
            call curry( pFun := $vMultiply,  pNargs := 2,  arg2 := $pX);
        };
        variable vCurriedMultByX := $vrtfCurriedMultByX/node();
        call iter( pTimes := $pTimes,  pFun := $vCurriedMultByX,  pX := 1);
    }

    match (myMultiply:*)( arg1,  arg2) {
        value-of ($arg1 * $arg2);
    }
}

Source and auto-generated text have been edited solely to replace < characters by &lt; to make the text readable in HTML.


Edward D. Willink
14 February 2004