[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
[Recent Entries]
[Reply To This Message]
Re: XPath MOD 10 calculation
Subject: Re: XPath MOD 10 calculation
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 24 May 2007 13:46:18 +0200
|
Oops, sorry, I see now that I missed a spot in your algorithm, the *3
part. I read-up a bit on the mod10. Is yours the same as describe here
http://en.wikipedia.org/wiki/Luhn_algorithm ? Because it seems to me
that your code does not equal that description.
-- Abel
Abel Braaksma wrote:
Well, 'elegant' is in the eye of the beholder, I think ;)
Perhaps this is something that you consider more elegant. But
depending on your audience, it may win the beauty contest, but not
receive praise for elegance:
<xsl:variable name="x"
select="sum(for $i in (1,3,5,7,9,11,2,4,6,8,10) return
number(substring(., $i, 1))) mod 10" />
and leave the xsl:if as-is. Or you can rewrite your xsl:if like this:
<xsl:if test="(10 - $x) mod 10 ne substring(., 12, 1)" />
which brings us to a possibility to combine the two in one expression
without repetition:
<xsl:if test="(10 - sum(for $i in (1,3,5,7,9,11,2,4,6,8,10) return
number(substring(., $i, 1))) mod 10) mod 10 ne substring(., 12, 1)" />
I'm not certain of the order of operator precedence, you may have to
insert some parentheses here and there. I didn't test it, because I
don't know the MOD 10 algorithm and you did not provide enough
examples to really test it. I chose for number() instead of xs:integer
because sum() takes doubles and xs:integer can fail with an error,
where number() cannot fail.
Cheers,
-- Abel Braaksma
|
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format
RSS 2.0 |
|
Atom 0.3 |
|
|