Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD schema: How to specify the number of digits in a value?

i want to limit the number of digits allowed in an element to 6:

<AccountNumber>123456</AccountNumber>
<AccountNumber>999999</AccountNumber>
<AccountNumber>000000</AccountNumber>

The field format specification is 6-digit, zero-padded, numeric.

i read that i might want to use the totalDigits restriction, based on:

totalDigits Specifies the exact number of digits allowed. Must be greater than zero

So i have the simple type:

<xs:simpleType name="AccountNumber">
   <xs:restriction base="xs:int">
      <xs:totalDigits value="6"/>
   </xs:restriction>
</xs:simpleType>

And while it catches invalid numbers, such as:

<AccountNumber>1234567</AccountNumber>
<AccountNumber>0000000</AccountNumber>
<AccountNumber></AccountNumber>

it doesn't catch invalid numbers:

<AccountNumber>12345</AccountNumber>
<AccountNumber>01234</AccountNumber>
<AccountNumber>00123</AccountNumber>
<AccountNumber>00012</AccountNumber>
<AccountNumber>00001</AccountNumber>
<AccountNumber>00000</AccountNumber>
<AccountNumber>0000</AccountNumber>
<AccountNumber>000</AccountNumber>
<AccountNumber>00</AccountNumber>
<AccountNumber>0</AccountNumber>

What is a suggested restriction to specify the exact number of digits allowed?

like image 552
Ian Boyd Avatar asked Nov 19 '10 19:11

Ian Boyd


People also ask

How is length defined in XSD?

you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .

How can we define within an XSD?

Defining XML ElementsEach element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

What is XSD decimal?

xsd:decimal is the datatype that represents the set of all decimal numbers with arbitrary lengths. Its lexical space allows any number of insignificant leading and trailing zeros (after the decimal point).

What is XSD integer?

Description. The value space of xsd:int is the set of common single-size integers (32 bits), the integers between -2147483648 and 2147483647. Its lexical space allows any number of insignificant leading zeros.

How do I create a schema in XSD?

An XSD Example 1 An XML Document. The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid". 2 Create an XML Schema. Now we want to create a schema for the XML document above. ... 3 Divide the Schema. ... 4 Using Named Types. ...

What is XSD in XML?

XML schema (XSD) Overview. An XML schema, commonly known as an XML Schema Definition (XSD), formally describes what a given XML document can contain, in the same way that a database schema describes the data that can be contained in a database (i.e. table structure, data types, constraints etc.).

What is totaldigits in XML Schema?

- XML Schema [Book] xs:totalDigits — Facet to define the total number of digits of a numeric datatype. May be included in: xs:restriction (simple type), xs:restriction (simple content)

What is an attribute in XSD?

An attribute provides extra information within an element. Attributes have name and type properties and are defined within an XSD as follows: An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default they are optional).


1 Answers

You need to use xs:pattern and provide a regular expression to limit it to a number.

<xs:simpleType name="AccountNumber">
   <xs:restriction base="xs:int">
      <xs:pattern value="\d{6}"/>
   </xs:restriction>
</xs:simpleType>
like image 190
Jeff Yates Avatar answered Oct 01 '22 10:10

Jeff Yates