XSD Date or no date validation
The following seems to work for MM-YYYY format but I now have a case where it can either be blank or have a date. Is this possible or should I push to only include the attribute in the XML if there is a date and make the attribute optional?
<xs:attribute name="edition_date" use="required">
<xs:simpleType>
<xs:restriction base="AT_STR">
<xs:length value="7"/>
<xs:pattern value="(0[1-9]|1[012])[-](19|20)\d\d"/>
</xs:restriction&g开发者_JAVA百科t;
</xs:simpleType>
Only include the attribute in the XML if there is a date and make the attribute optional
Seems like the right option to me
Making the attribute optional, and only including the attribute in the XML if there is a date is a good option, but in some cases you may not have control over the generation of the XML. In this case, you could try the following pattern in the XSD
<xs:simpleType name="edition_date">
<xs:restriction base="xs:string">
<xs:maxLength value="7"/>
<xs:pattern value="((0[1-9]|1[012])[-](19|20)\d\d)?"/>
</xs:restriction>
</xs:simpleType>
This is a similar pattern to yours, but the use of the ? operator indicates the pattern can occur zero or one times, and so will allow an empty string. Also note the use of xs:maxLength instead of xs:length. Not that is bit is needed, because the pattern only allows a fixed length anyway.
精彩评论