Huuuze
Huuuze

Reputation: 16357

How do I set a minimum length for an integer in an XML Schema?

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>

Upvotes: 4

Views: 13114

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

I would use the base type xs:gYear, with additional constraining facets, as needed. For more info, this might help.

Upvotes: 0

VonC
VonC

Reputation: 1323115

How about a plage of value as an additional restriction ?

(minInclusive - maxInclusive)

For instance ?

<xs:minInclusive value="1900"/> et <xs:maxInclusive value="2008"/>

But to get back to the totalDigits constraint, why do you not set the fixed attribute to true ?

If {fixed} is true, then types for which the current type is the {base type definition} cannot specify a value for totalDigits other than {value}.

<xs:totalDigits value="4" fixed="true" />

Upvotes: 7

Related Questions