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>
Related
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).
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>
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>
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>
I need the xml parser/validator to ignore the presense of &
How do I accomplish it by using CDATA in xsd.
This is snippet of xsd:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
I tried using CDATA as follows but in vain as I get xsd validation error:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN <![CDATA[&]]> OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
Any help is appreciated.
Thansk in advance.
I believe you can use the entity reference & instead of the character &.
Try using an entity reference:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
or a decimal reference:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
Hmm. I find it interesting how that line in the second snippet is actually black instead of the properly coloured elements.
May I ask what the validation error is? It may help in pinpointing what is exactly wrong with that line of code. It may be true that the parser will ignore the & sign, but have you tried replacing the & with '&' ? The thing is, I have a feeling you're setting the enumeration value to
"IN <![CDATA[&]]> OUT"/>
Which would... obviously not pass the validation. Either that or the parser completely passes over that line and only takes in XYZ as an enum value.
What's the error.
Have you tried replacing & with
&
Since it's an escape entity?
Cheers.