[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: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 25 May 2007 09:45:13 +0100
|
On 5/25/07, Jesper Tverskov <jesper@xxxxxxxxxxx> wrote:
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.
<?xml version="1.0"?>
<!-- legal UPC e.g.: 639382000393-->
<test>
<upc>639382000393</upc>
</test>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="test/upc">
<topelement>
<title>Test of UPC code. Legal code returns empty elements.</title>
<original>
<xsl:variable name="x"
select="((xs:integer(substring(., 1, 1)) + xs:integer(substring(.,
3,1)) + xs:integer(substring(., 5, 1)) + xs:integer(substring(., 7,
1)) + xs:integer(substring(., 9, 1)) + xs:integer(substring(., 11,
1))) * 3 + (xs:integer(substring(., 2, 1)) + xs:integer(substring(.,
4, 1)) + xs:integer(substring(., 6, 1)) + xs:integer(substring(., 8,
1)) + xs:integer(substring(., 10, 1)))) mod 10"/>
<xsl:if test="(if ($x ne 0) then (10 - $x) else $x) ne
xs:integer(substring(., 12, 1))">UPC not legal</xsl:if>
</original>
<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>
</topelement>
</xsl:template>
</xsl:stylesheet>
Running that gave me 7 - when the check digit should be 3 shouldn't
it? You don't appear to be doing the mod 10 calculations correctly
(according to the steps on wikipedia) - you do the summing, mod 10 it,
take that from 10 and then mod 10 it again.
If you separate out the steps into variables then it becomes easier I think:
<xsl:variable name="tokens"
select="for $i in 1 to string-length(.) return
xs:integer(substring(., $i, 1))"
as="xs:integer+"/>
<xsl:variable name="code" select="$tokens[position() ne last()]"
as="xs:integer+"/>
<xsl:variable name="check-digit" select="$tokens[last()]" as="xs:integer+"/>
<xsl:variable name="odd" select="$code[position() mod 2 = 1]"
as="xs:integer+"/>
<xsl:variable name="even" select="$code[position() mod 2 = 0]"
as="xs:integer+"/>
<xsl:variable name="calc" select="(10 - ((sum($odd) * 3) +
sum($even)) mod 10) mod 10" as="xs:integer"/>
<improved tokens="{$tokens}" code="{$code}"
check-digit="{$check-digit}" calc="{$calc}">
<xsl:if test="$calc ne $check-digit">UPC not legal</xsl:if>
</improved>
For this input:
<test>
<upc>639382000393</upc>
<upc>036000291452</upc>
</test>
It returns:
<improved tokens="6 3 9 3 8 2 0 0 0 3 9 3" code="6 3 9 3 8 2 0 0 0 3
9" check-digit="3"
calc="3"/>
<improved tokens="0 3 6 0 0 0 2 9 1 4 5 2" code="0 3 6 0 0 0 2 9 1 4
5" check-digit="2"
calc="2"/>
The second test is taken from the example on wikipedia.
cheers
andrew
|
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 |
|
|