XSLT function (template) syntax

06/12/2012 13:57

Simple code translated from C# to Xslt

public static void main()

{

int number1 = 10;

int number2 = 12;

    Console.Write("{0}", number1);

    Console.Write("{0}", (number1 > number2?"-", "+"));

    Console.Write("{0}", number1);

    Console.Write("=");

    Console.Write("{0}", MyAddingFunction(number1, number2));

    Console.Write(".");

}

 

 

public static int MyAddingFunction(int a, int b)

{

    if (a > b)

    {

        return a - b;

    }

    else

    {

        return a + b;

    }

}

Here is Xslt code

xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:param name="number1">10</xsl:param>

<xsl:param name="number2">12</xsl:param>

<xsl:template match="/">

<html>

    <body>

        <span>Adding function example:span>

        <xsl:value-of select="$number1"/>

        <xsl:choose>

            <xsl:when test="$number1 > $number2">

                <xsl:text>-</xsl:text>

            </xsl:when>

            <xsl:otherwise>

                <xsl:text>+xsl:text>

            </xsl:otherwise>

        </xsl:choose>

        <xsl:value-of select="$number2"/>

        =

        <xsl:call-template name="MyAddingFunction">

            <xsl:with-param name="a" select="$number1"/>

            <xsl:with-param name="b" select="$number2"/>

        </xsl:call-template>

        .

        <body>

    <html>

</xsl:template>

 

<xsl:template name="MyAddingFunction">

    <xsl:param name="a"/>

    <xsl:param name="b"/>

    <xsl:choose>

        <xsl:when test="$a > $b">

            <xsl:value-of select="number($a)-number($b)"/>

        </xsl:when>

        <xsl:otherwise>

            <xsl:value-of select="$a+$b"/>

        </xsl:otherwise>

        </xsl:choose>

 </xsl:template>

</xsl:stylesheet>

 In case of input values

<xsl:param name="number1">10</xsl:param>

<xsl:param name="number2">12</xsl:param>

 result is

Adding function example:10+12 = 22 .

 

<xsl:param name="number1">10</xsl:param>

<xsl:param name="number2">12</xsl:param>

result is

Adding function example:10-6 = 4 .