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?
you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .
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.
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).
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.
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. ...
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.).
- 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)
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).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With