I am in the process of creating an XML Schema and one of my values is a year. As such, I’d like to ensure that all values have exactly 4 characters. To do so, I am using the following syntax:
<xs:element name='publish_year' maxOccurs='1'> <xs:simpleType> <xs:restriction base='xs:positiveInteger'> <xs:totalDigits value='4'/> </xs:restriction> </xs:simpleType> </xs:element>
If I’m understanding ‘totalDigits’ correctly, someone could pass in a ‘publish_year’ value of ‘2008’ or ‘200’. Both would be valid. As such, how can I structure my XSD to ensure 4 digits are required? At first blush, I’m guessing I’d use a regex, but I’d like to know if I’m overlooking something that’s already baked in (like ‘totalDigits’)
UPDATE:
I went with the following solution. It may be overkill, but it gets the point across:
<xs:simpleType> <xs:restriction base='xs:positiveInteger'> <xs:totalDigits value='4' fixed='true'/> <xs:minInclusive value='1900'/> <xs:pattern value='^([1][9]\d\d|[2]\d\d\d)$'/> </xs:restriction> </xs:simpleType>
How about a plage of value as an additional restriction ?
(minInclusive – maxInclusive)
For instance ?
But to get back to the totalDigits constraint, why do you not set the
fixedattribute to true ?