views:

209

answers:

2

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>
+2  A: 

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" />
VonC
Why doesn't it answer the question?
David Norman
The first comment was written at the time when I did not yet added the 'fixed' suggestion on the totalDigits element ;-)
VonC
A: 

Dear Friends,

I have one xml which having following tag,

How can do the validation for formatString, also values given for padding and other attribute

jayraj