Schema restriction problem fixed values with optional character allowed
-
Tuesday, September 11, 2012 3:18 AM
Hi,
I created a schema where I would like to restrict by allowing certain fixed values like "XML", "READ", "READ180" and "," (comma) character, otherwise, schema would return an invalid restriction exception/error... How is this can be done?
xs:enumeration is for actual exact value...and xs:pattern is any values following certain character format and not fixed values with optional characters like a comma...
Allowed:
<DETAIL>READ</DETAIL>
<DETAIL>XML</DETAIL>
<DETAIL>FLAG</DETAIL>
<DETAIL>READ,XML</DETAIL>
<DETAIL>FLAG,XML</DETAIL>
<DETAIL>READ,FLAG,XML</DETAIL>
I tried using union with simple type of enumertaion and simple type of pattern with value="[, ]"...
I do not want to use <xs:pattern value="[EADFGLMRX,]" />...
Follow up..how about optional elements like LON and SVN? If they are not in the xml, xml is valid, if they exist they must conform to what was defined in schema, in order xml to be valid...How to do this?
Let's say xml...
Allowed:
Sample # 1
<DETAIL>XML</DETAIL>
<LON>FUN</LON>
Sample # 2
<DETAIL>XML</DETAIL>
<SVN>Tear</SVN>
Sample # 3
<DETAIL>READ</DETAIL>
** UPDATE this last issue having optional Elements this issue is resolved **
Thanks In Advanced
All Replies
-
Wednesday, September 12, 2012 2:13 AMModerator
Hi den2005,
Welcome to MSDN Forum.
I found you have an 'UPDATE' in the end, does it mean the issue has resolved?
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, September 12, 2012 2:51 AMThe second issue about "optional" element is resolved, but like to know another approach for the first issue by restricting only allowed values...
-
Wednesday, September 12, 2012 5:37 AMModerator
Hi den2005,
If the values are not many, you can also use enumeration restriction to validate the values. For example,
<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> <xs:enumeration value="BMW,Audi"/> <xs:enumeration value="BMW,Golf"/> </xs:restriction> </xs:simpleType> </xs:element>
But I don't think this is a better way than using pattern restriction as you mentioned. Obviously, using pattern restriction is more flexible than enumeration restriction. In addition to above two solutions, I don't know other ways to validate. Below is all the restrictions for DataTypes in XSD.
enumeration Defines a list of acceptable values fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value) maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value) maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value) minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero pattern Defines the exact sequence of characters that are acceptable totalDigits Specifies the exact number of digits allowed. Must be greater than zero whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Allen Li - AI3Microsoft Contingent Staff, Moderator Monday, September 17, 2012 1:32 AM

