Re: How to handle Currency

Hi Phil,

> I have an XML document that stores currency amounts with all
> punctuation (eg. $1,234.00). A primitive data type will not work for
> this (other than string). Is there a way to validate an amount
> greater than $0.00 ? Should I use a pattern facet ?
 
Yes - I think that the closest type to what you're after here is a
xs:token that fulfils a pattern, which might be something like:

<xs:simpleType name="currency">
  <xs:restriction base="xs:token">
    <xs:pattern value="$[1-9][0-9]{0,2}(,[0-9]{3})*\.[0-9]{2}" />
  </xs:restriction>
</xs:simpleType>

The reason that I'd use xs:token as the base rather than xs:string is
that it means that you can include whitespace around the currency
without it messing up the validity. For example:

  <price>
    $12.99
  </price>

would be valid. This mirrors the other data types (aside from
xs:string and xs:normalizedString), so is more consistent and
therefore will be less prone to errors than basing the simple type on
xs:string (in my opinion).

You could use \d instead of the [0-9] but that would allow digits from
all sorts of languages, which you might not actually want. I'm sure
you're aware that other processors will not interpret these values as
numbers.

Cheers,

Jeni

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

Received on Saturday, 23 February 2002 18:44:14 UTC