Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Arithmetics (2/6) >

Multiplication and division

Operator "*" is used for multiplications. Spaces around the operator are not mandatory.

In most programming languages slash "/" is used for division. Because XPath expressions are using slash as path separator (e.g. /aaa/bbb/ccc) another operator must be used. XPath 2.0 uses operator "div" for floating point division and operator "idiv" for integer division.

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <bbb>
                        <xsl:value-of  select="12 * 23"/>
                  </bbb>
                  <ccc>
                        <xsl:value-of  select="x*y"/>
                  </ccc>
                  <ddd>
                        <xsl:value-of  select="x div y"/>
                  </ddd>
                  <eee>
                        <xsl:value-of  select="x idiv y"/>
                  </eee>
                  <fff>
                        <xsl:value-of  select="-3 idiv 2"/>
                  </fff>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <x>5</x>
            <y>3</y>
      </aaa>
Output

      <bbb>276</bbb>
      <ccc>15</ccc>
      <ddd>1.6666666666666667</ddd>
      <eee>1</eee>
      <fff>-1</fff>


Previous chapter: Implicit behaviour
Next chapter: Functions operating on strings
Previous page: Addition and subtraction
Next page: Modulus