[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: "Jesper Tverskov" <jesper@xxxxxxxxxxx>
Date: Fri, 25 May 2007 07:14:45 +0200
Re:  XPath MOD 10 calculation
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>

Cheers,
Jesper Tverskov

http://www.xmlplease.com/xslt

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.