Robert Gould
Robert Gould

Reputation: 69865

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

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.

Upvotes: 21

Views: 42402

Answers (3)

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Try

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

as in XSL you have to use "mod" to get modulus

Upvotes: 19

bstoney
bstoney

Reputation: 6784

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.

Upvotes: 45

geoffc
geoffc

Reputation: 4100

Also watch out when doing addition/subtraction. When doing addition all should be good with $var1+$var2. But in Subtraction, since a dash (-) is valid in a variable name $var1-$var2 does not work. But $var1 - $var2 should, and number($var1) - number($var2) always should work and you can see an article I wrote about in relation to the use of XPATH in Novell's Identity Manager product.

XPATH Math thoughts

Upvotes: 6

Related Questions