W3C home > Mailing lists > Public > xmlschema-dev@w3.org > August 2001

The <Value>_<Unit> problem

From: Eddie Robertsson <eddie@allette.com.au>
Date: Thu, 02 Aug 2001 17:15:16 +1000
Message-ID: <3B68FE04.4C4C878A@allette.com.au>
To: XSD <xmlschema-dev@w3.org>
Hi all,

If you remember a couple of weeks ago we had some queries about how you
could specify an element or attribute value that contains both a _value_
and a _unit_. For example I want to specify a document with products and
one of my child elements is productCost. I want the value of productCost
to be a space separated pair of values like:

<productCost>20 AUD</productCost>
<productCost>10 USD</productCost>

The suggested solution I had at the time was to create an enumerated
simpleType of all the currencies like this:

 <xsd:simpleType name="myCurrency">
  <xsd:restriction base="xsd:string">
   <xsd:enumeration value="AUD"/>
   <xsd:enumeration value="USD"/>
   <xsd:enumeration value="SEK"/>

Then you create a myUnion datatype to be a Union of xsd:integer and

 <xsd:simpleType name="myUnion">
  <xsd:union memberTypes="xsd:integer myCurrency"/>

Now you create a List type that is derived from the productCostType and
put the facet length="2" as a restriction:

 <xsd:simpleType name="productCostType">
    <xsd:list itemType="myUnion"/>
   <xsd:length value="2"/>

Now you have a datatype that allows for values like the above.

<productCost>20 AUD</productCost>
<productCost>10 USD</productCost>

The problem is that it also allows the reversed values:

<productCost>AUD 20</productCost>
<productCost>USD 10</productCost>

However, yesterday when checking the datatypes spec on another issue I
saw that a List datatype actually can have a pattern facet. This means
we can actually restrict our datatype to validate only the correct
values by applying the pattern:

<xs:pattern value="/d+ .+"/>   <!-- One or more digits (integer)
followed by a space followed by one or more characters. -->

So, the productCostType would be:

 <xsd:simpleType name="productCostType">
    <xsd:list itemType="myUnion"/>
   <xsd:length value="2"/>
   <xs:pattern value="/d+ .+"/>

The only validator that seems to support this is MSXML4 July release but
according to my reading of the spec this should be allowed.


Complete schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

 <xs:simpleType name="myCurrency">
  <xs:restriction base="xs:string">
   <xs:enumeration value="AUD"/>
   <xs:enumeration value="USD"/>
   <xs:enumeration value="SEK"/>

 <xs:simpleType name="myUnion">
  <xs:union memberTypes="xs:integer myCurrency"/>

 <xs:simpleType name="productCostType">
    <xs:list itemType="myUnion"/>
   <xs:length value="2"/>
   <xs:pattern value="\d+ .+"/>

 <xs:element name="Root">
    <xs:element name="Child" type="productCostType"

Received on Thursday, 2 August 2001 03:17:19 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:55:52 UTC