Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

modulo

xslt

xpath

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.

like image 926
Robert Gould Avatar asked Mar 04 '09 03:03

Robert Gould


People also ask

How do you do absolute value in XSLT?

In XPath 2.0 (XSLT 2.0) use the abs() function. This can be achieved using the xpath abs function.

What is mod in XSLT?

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


3 Answers

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.

like image 141
bstoney Avatar answered Oct 06 '22 00:10

bstoney


Try

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

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

like image 36
Bhushan Bhangale Avatar answered Oct 06 '22 00:10

Bhushan Bhangale


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

like image 26
geoffc Avatar answered Oct 05 '22 23:10

geoffc