Dream Viewer
Dream Viewer

Reputation: 43

XSD pattern to restrict number

I wish to create a type in XSD which allows only positive numbers having 2 or 3 digits i.e. from 10 to 999 but does not contain a leading zero. e.g.:

Number: 15, 99, 215, 789 are all valid

but

Number: 0010, 00258 are invalid

Could someone please help me with this type?

Upvotes: 2

Views: 2706

Answers (1)

halfbit
halfbit

Reputation: 3464

You can use pattern restriction even on numbers to express that the first digit must not be zero. Example:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.example.org/so20022660" 
  xmlns:tns="http://www.example.org/so20022660"
  elementFormDefault="qualified">
    <simpleType name="so20022660">
        <restriction base="int">
            <minInclusive value="10"/>
            <maxInclusive value="999"/>
            <pattern value="[1-9][0-9]*"/>
        </restriction>
    </simpleType>
    <element name="root" type="tns:so20022660"/>
</schema>

Valid XML:

<?xml version="1.0" encoding="UTF-8"?>
<tns:root xmlns:tns="http://www.example.org/so20022660" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.example.org/so20022660 so20022660.xsd "
  >55</tns:root>

Value 055 is invalid. (Checked it with Eclipse IDE.)

Upvotes: 3

Related Questions