xsd - not allowing values defined in the enumeration list - xsd

I would like to enforce a rule on the attribute which should not allow a value defined in enumeration values associated with another attribute.
Here is my sample schema.
<xs:complexType name="component">
<xs:attribute name="type" type="componentMainType"/>
<xs:attribute name="category" type="forbiddenCategoryTypes" use="optional"/>
</xs:complexType>
<xs:simpleType name="forbiddenCategoryTypes">
<xs:restriction base="xs:string">
**<xs:pattern value="not in forbiddenCategoryTypes"/>**
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="componentMainType">
<xs:restriction base="xs:string">
<xs:enumeration value="Component1"/>
<xs:enumeration value="Component2"/>
<xs:enumeration value="Component3"/>
<xs:enumeration value="Component4"/>
<xs:enumeration value="Component5"/>
</xs:restriction>
</xs:simpleType>

In my opinion not possible, pattern only takes a regex. I could not find any other relevant restriction.
I would suggest to change approach to regex with values (I know it is not optimal solution).

Related

xs:attribute type attribute vs xs:simpleType xs:restriction base attribute

I'm looking at an auto-generated XSD and I noticed that there are quite a few types that are repeated & so am looking to minimize repetition. When I write my own XSDs I normally do it the way its done for the Option1 attribute in the code below, however this XSD does it the way that the Option2 attribute is done.
<xs:element name="baseElement">
<xs:complexType>
<xs:attribute name="Option1" type="NonEmptyString" />
<xs:attribute name="Option2">
<xs:simpleType>
<xs:restriction base="NonEmptyString" />
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
So I was wondering, is there a difference between these two methods & if not which is better practice? I would be more inclined to use Option1 as it makes the overall file shorter easier to manage

Is it possible to make XSD conditional on element and attribute values, not names?

Is it possible to write conditional based xsd. I have list of countries defined in my xsd, If user choose country as UNITED STATES, xsd should allow user to select the states which belong to above choosen country, Else it should show validation error. Below example, perfectly allow user to select country and state for the below list, But it does not perform any validations agaist country. If you have any suggestions to write choice based xsd, Please let me know. Please note that, I have tried <xs:choice>, it does not work for this scenario.
<xs:complexType name="header-type">
<xs:sequence>
<xs:element name="country" type="country-type" />
<xs:element name="states" type="state-type" />
</xs:complextType>
<xs:simpleType name="country-type">
<xs:restriction base="xs:string">
<xs:enumeration value="UNITEDSTATES"></xs:enumeration>
<xs:enumeration value="AUS"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="state-type">
<xs:restriction base="xs:string">
<xs:enumeration value="California"></xs:enumeration>
<xs:enumeration value="Sydney"></xs:enumeration>
<xs:enumeration value="Texas"></xs:enumeration>
</xs:simpleType>

XML Schema: how to ensure a "fixed" element to be not empty?

I have the following code:
<xs:element name="Lang" fixed="de-CH" nillable="false">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:minLength value="5"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I would like to ensure that the element Lang is not empty. If I delete fixed attribute, the validation for non-emptiness works. Is it a way to do it without removing fixed?
I managed to achieve both fixedness and non-emptyness using xs:pattern restriction:
<xs:element name="Lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:minLength value="5"/>
<xs:maxLength value="5"/>
<xs:pattern value="de-CH"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
What about
<xs:element name="Lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:enumeration value="de-CH" />
</xs:restriction>
</xs:simpleType>
</xs:element>

It's possible to use multiple value in single enumeration tag?

I'm using XSD validation in WEB API project.
now i'm using following method for check string value.
<xs:simpleType name="CoverageTierPattern">
<xs:restriction base="xs:string">
<xs:enumeration value="EE"/>
<xs:enumeration value="ES"/>
<xs:enumeration value="EC"/>
<xs:enumeration value="Fam"/>
<xs:enumeration value="WO"/>
<xs:enumeration value="WP"/>
<xs:enumeration value="NE"/>
<xs:enumeration value="RC"/>
</xs:restriction>
</xs:simpleType>
this list value must be added more in future.
So i need to know it's possible to use values in single enumeration element
like
<xs:enumeration value="EE|ES|EC|..."/>
other wise if any other method present plz tell me.
You can use pattern element instead of enumeration like this:
<xs:pattern value="EE|ES|EC|..."/>
Full example from here in the middle of the page.
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

How to define xsd restrinction xs:enumeration to also accept empty element?

I have xsd schema that define Gender element that accepts only F or M value but I want it also to accept empty <Gender/> element. How to fix following schema?
...
<xs:simpleType name="Gender">
<xs:restriction base="xs:string">
<xs:enumeration value="M" />
<xs:enumeration value="F" />
</xs:restriction>
</xs:simpleType>
...
Have you tried adding:
<xs:enumeration value="" />
?
However, this is bad practice. If you want to represent this information in the instance documents, you should omit the <Gender> element altogether, rather than providing an empty one. Also, it's unclear what <Gender/> means - does this mean "no gender", or "unknown gender"? If it's one of those possibilities, perhaps you should add those to the eneration:
<xs:simpleType name="Gender">
<xs:restriction base="xs:string">
<xs:enumeration value="M" />
<xs:enumeration value="F" />
<xs:enumeration value="unknown" />
</xs:restriction>
</xs:simpleType>
You could use a pattern instead:
<xs:simpleType name="Gender">
<xs:restriction base="xs:string">
<xs:pattern value="(M|F)?"/>
</xs:restriction>
</xs:simpleType>

Resources