DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

MathOps: Simple Math Operations In Xslt

12.02.2005
| 22612 views |
  • submit to reddit
        cfMathOps.xml:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="cfMathOps.xsl" ?>
<!--
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfMathOps.xml
    ###     desc : xml data file to use with cfMathOps.xsl
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 11:57:41"
    ###     lang    : xslt
    ###     tags    : xml xslt math cfMathOps
    ### </region-file_info>
    -->
<numbers>
  <x>4</x>
  <y>3.2</y>
  <z>11</z>
</numbers>

cfMathOps.xsl
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfMathOps.xsl
    ###     desc : quick example of math operations using xslt
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 11:57:41"
    ###     lang    : xslt
    ###     tags    : xml xslt math hello cfMathOps
    ### </region-file_info>
    -->

<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="numbers">
<h2>math operations from xslt</h2>
    <br />  A. 4 + 3.2        = <xsl:value-of select="x + y"/>
    <br />  B. 3.2 - 4        = <xsl:value-of select="y - x"/>
    <br />  C. 4 * 3.2        = <xsl:value-of select="x * y"/>
    <br />  D. 11/3.2         = <xsl:value-of select="z div y"/>
    <br />  E. 4 + 3.2 * 11   = <xsl:value-of select="x+y*z"/>
    <br />  F. (4 + 3.2) * 11 = <xsl:value-of select="(x+y)*z"/>
    <br />  G. 11 mod 4       = <xsl:value-of select="z mod x"/>
    <br />  H. 4 + 3.2 + 11   = <xsl:value-of select="sum(*)"/>
    <br />  I. floor(3.2)     = <xsl:value-of select="floor(y)"/>
    <br />  J. ceiling(3.2)   = <xsl:value-of select="ceiling(y)"/>
    <br />  K. round(3.2)     = <xsl:value-of select="round(y)"/>
    <br />  L. 11 + count(*)  = <xsl:value-of select="11+count(*)"/>
    <br />  M. 11 + "hello"                 = <xsl:value-of select="z + 'hello'"/>
    <br />  N. 3.2 + string-length("3.2")   = <xsl:value-of select="y + string-length(y)"/>
</xsl:template>

</xsl:stylesheet>