Numeric Calculation in XSL
Numeric Calculation in XSL
The Functions number do transforms its argument to number. XSL stylesheet 1
demonstrates the string conversion, XSL stylesheet 2 converts the boolean values
to either true and false.
A D V E R T I S E M E N T
XSL stylesheet 1
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE border="1">
<TR>
<TH>text</TH>
<TH>number</TH>
</TR>
<xsl:for-each select="//text">
<TR>
<TD>
<xsl:value-of select="."/>
</TD>
<TD>
<xsl:value-of select="number()"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
XSL stylesheet 2
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE border="1">
<TR>
<TH>text</TH>
<TH>number</TH>
</TR>
<xsl:for-each select="//text[text() = 'true' or text() = 'false()']">
<TR>
<TD>
<xsl:value-of select="."/>
</TD>
<TD>
<xsl:value-of select="number()"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
<P>
<xsl:text>but:</xsl:text>
</P>
<TABLE border="1">
<TR>
<TH>function</TH>
<TH>number</TH>
</TR>
<TR>
<TD>true()</TD>
<TD>
<xsl:value-of select="number(true())"/>
</TD>
</TR>
<TR>
<TD>false()</TD>
<TD>
<xsl:value-of select="number(false())"/>
</TD>
</TR>
<TR>
<TD>5>7</TD>
<TD>
<xsl:value-of select="number(5 > 7)"/>
</TD>
</TR>
<TR>
<TD>5<7</TD>
<TD>
<xsl:value-of select="number(5<7)"/>
</TD>
</TR>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Arithematic Operations
The Add, subtract and multiply do use the common syntax , see the XSL stylesheet 1.
The division syntax is less usual. A slash "/" symbol is used in the patterns and so
the keyword div is been used instead, see the XSL stylesheet 2. The operator mod returns
a remainder from truncating the division.
XML Source
<source>
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
<number>11</number>
</source>
Output
<P>1 + 3 = 4</P>
<P>4 - 17 = -13</P>
<P>8 * 11 = 88</P>
XSL stylesheet 1
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<P>
<xsl:value-of select="//number[1]"/>
<xsl:text> + </xsl:text>
<xsl:value-of select="//number[2]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[1] + //number[2]"/>
</P>
<P>
<xsl:value-of select="//number[3]"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="//number[4]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[3] - //number[4]"/>
</P>
<P>
<xsl:value-of select="//number[5]"/>
<xsl:text> * </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] * //number[6]"/>
</P>
</xsl:template>
</xsl:stylesheet>
XML Source
<source>
<number>1</number>
<number>3</number>
<number>4</number>
<number>17</number>
<number>8</number>
<number>11</number>
</source>
Output
<P>8 / 11 = 0.7272727272727273</P>
<P>8 mod 11 = 8</P>
XSL stylesheet 2
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<P>
<xsl:value-of select="//number[5]"/>
<xsl:text> / </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] div //number[6]"/>
</P>
<P>
<xsl:value-of select="//number[5]"/>
<xsl:text> mod </xsl:text>
<xsl:value-of select="//number[6]"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="//number[5] mod //number[6]"/>
</P>
</xsl:template>
</xsl:stylesheet>
Ceiling, Floor and Round
The functions ceilng(), floor() and the round() transforms a floating point numbers
into an integer in a specified way.
XML Source
<source>
<number>6</number>
<number>3.8</number>
<number>1.234</number>
<number>-6</number>
<number>-3.8</number>
<number>-1.234</number>
</source>
Output
<TABLE border="1">
<TR>
<TH>number</TH>
<TH>floor</TH>
<TH>ceiling</TH>
<TH>round</TH>
</TR>
<TR>
<TD>6</TD>
<TD>6</TD>
<TD>6</TD>
<TD>6</TD>
</TR>
<TR>
<TD>3.8</TD>
<TD>3</TD>
<TD>4</TD>
<TD>4</TD>
</TR>
<TR>
<TD>1.234</TD>
<TD>1</TD>
<TD>2</TD>
<TD>1</TD>
</TR>
<TR>
<TD>-6</TD>
<TD>-6</TD>
<TD>-6</TD>
<TD>-6</TD>
</TR>
<TR>
<TD>-3.8</TD>
<TD>-4</TD>
<TD>-3
</TR>
<TR>
<TD>-1.234</TD>
<TD>-2</TD>
<TD>-1</TD>
<TD>-1</TD>
</TR>
</TABLE>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE border="1">
<TR>
<TH>number</TH>
<TH>floor</TH>
<TH>ceiling</TH>
<TH>round</TH>
</TR>
<xsl:for-each select="//number">
<TR>
<TD>
<xsl:value-of select="."/>
</TD>
<TD>
<xsl:value-of select="floor(.)"/>
</TD>
<TD>
<xsl:value-of select="ceiling(.)"/>
</TD>
<TD>
<xsl:value-of select="round(.)"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
String Function
The Function string() transforms all its argument into the string. This function is not been
used directly usualy in the stylesheets since it is in most of the cases been called by
default. The XSL stylesheet 1 shows examples of the numbers to string conversions.
Notice the results of a zero divisions.
XML Source
<source>
<number>9</number>
<number>0</number>
<number>-9</number>
<number/>
</source>
Output
<P>9</P>
<P>NaN</P>
<P>9/0 = Infinity</P>
<P>-9/0 = -Infinity</P>
<P>0/0 = NaN</P>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:variable name="A" select="number(//number[1])"/>
<xsl:variable name="B" select="number(//number[2])"/>
<xsl:variable name="C" select="number(//number[3])"/>
<xsl:variable name="D" select="number(//number[4])"/>
<xsl:template match="/">
<P>
<xsl:value-of select="string(number($A))"/>
</P>
<P>
<xsl:value-of select="string(number($D))"/>
</P>
<P>
<xsl:value-of select="$A"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$B"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="string($A div $B)"/>
</P>
<P>
<xsl:value-of select="$C"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$B"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="string($C div $B)"/>
</P>
<P>
<xsl:value-of select="$B"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$B"/>
<xsl:text> = </xsl:text>
<xsl:value-of select="$B div $B"/>
</P>
</xsl:template>
</xsl:stylesheet>
Be the first one to comment on this page.
Share And Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords Numeric Calculation in XSL, xsl functions, xsl tutorial, xsl examples, xsl javascript,
xsl example, xsl function, xsl date, xsl syntax, xsl variable, xsl text, sql numeric,
c# xsl, java numeric, numeric tutorial, asp numeric, numeric number, xsl select,
xsl xpath, xsl output, oracle numeric, perl numeric, numeric time, mysql numeric,
format numeric, xsl html, sql server numeric, xsl vb net, asp net xsl, vb net numeric,
numeric data type