Re: [xsl] collapsing number ranges

Subject: Re: [xsl] collapsing number ranges
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 30 Aug 2004 10:12:09 +0100
Hi Bruce,

>> I wouldn't have thought of using idiv and mod to get particular
>> digits if I hadn't seen David C.'s solution. I would have just used
>> substring() instead.
>
> Just out of curiosity, what are tradeoffs between the two
> approaches?

Well, you (and future maintainers of your code) might find the
substring() version less obscure.

There are type-related implications: some people would say that since
the values of $begin and $end are integers, you should process them as
integers rather than pretending that they're strings. The substring()
functions return a string whereas the mod/idiv operators return
numbers, and that can have an impact on whether you need to cast the
result or not, depending on what you're doing with it.

There's also a subtlety regarding the "get the last two digits". If
you have $begin = 108 then "$begin mod 100" returns the integer 8
whereas "substring($begin, string-length($begin) - 1)" returns the
string "08". So if you do a <xsl:value-of> then the substring()
version gives you a leading zero (which you can get rid of by casting
to a number with number()).

Also, if your $begin or $end actually *start* with a zero then the
mod/idiv versions will ignore them whereas the substring() function
will preserve them.

I doubt that there are significant performance differences.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread