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

Modulus

Operator "mod" calculates modulus: the remainder resulting from dividing the first argument by the second one. The operator is very useful when alternative sequences are needed.

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="a[. mod 2 = 1]"/>
                  </bbb>
                  <ccc>
                        <xsl:value-of  select="for $a in (1 to 100) return if ($a mod 9 = 0) then $a else ()"/>
                  </ccc>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <a>5</a>
            <a>3</a>
            <a>7</a>
            <a>2</a>
      </aaa>
Output

      <bbb>5 3 7</bbb>
      <ccc>9 18 27 36 45 54 63 72 81 90 99</ccc>


Previous chapter: Implicit behaviour
Next chapter: Functions operating on strings
Previous page: Multiplication and division
Next page: Summation