How to get the modulus of a number in XPath/XSLT?

Robert Gould picture Robert Gould · Mar 4, 2009 · Viewed 34.3k times · Source

I want to calculate the modulus of a number in the XPath, but this is not working:

<xsl:if test="(count()%8)">

How would I do it? I Looked at the XPath function reference here, but didn't see anything like that.

Answer

bstoney picture bstoney · Mar 4, 2009

try "mod"

see http://www.w3.org/TR/xpath#numbers

Details from that link:

The mod operator returns the remainder from a truncating division. For example,

5 mod 2 returns 1

5 mod -2 returns 1

-5 mod 2 returns -1

-5 mod -2 returns -1

NOTE: This is the same as the % operator in Java and ECMAScript.