Subject: Re: [xsl] XPath MOD 10 calculation From: Abel Braaksma <abel.online@xxxxxxxxx> Date: Fri, 25 May 2007 16:03:13 +0200 |
I have a feeling that my original code for UPC was right, as seen also in the "Check digit calculation" section here, http://en.wikipedia.org/wiki/Universal_Product_Code.
Inspired by Abel, I have now made an improved version of my original code for testing if UPC code is legal. The two versions, both tested, are shown side by side.
<improved>
<xsl:variable name="y" select="for $i in string-to-codepoints(substring(., 1, string-length(.) - 1)) return codepoints-to-string($i)"/>
<xsl:variable name="z" select="sum((for $i in $y[(position()) mod 2 = 1] return xs:integer($i)*3, for $i in $y[(position()) mod 2 = 0] return xs:integer($i))) mod 10"/>
<xsl:if test="(if ($z ne 0) then (10 - $z) else $z) ne xs:integer(substring(., string-length(.), 1))">UPC not legal</xsl:if>
</improved>
sum((number(substring(., 12, 1)), for $i in (1,3,5,7,9,11) return number(substring(., $i, 1)) * 3, for $i in (2,4,6,8,10) return number(substring(., $i, 1)))) mod 10 = 0
Cheers, -- Abel Braaksma
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XPath MOD 10 calculation, Andrew Welch | Thread | [xsl] XSLT 2.0 multi-level grouping, cknell |
RE: [xsl] Page numbering, Mulberry Technologie | Date | Re: [xsl] Page numbering, Eike Jordan |
Month |