<xs:element name="CurrencyCode" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
But if my value is empty it returns an error
cvc-length-valid: Value '' with length = '0' is not facet-valid with respect to length '3' for type '#AnonType_CurrencyCodeServiceServiceList'.
So how can i deal with it ?
Using <xsd:choice> in an XSD The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.
The presence of the xsd:nillable attribute in an XSD element means that the corresponding element in the XML file permits null values.
nillable="false" means you can't have the attribute xsi:nil="true". But you don't have this attribute so this won't make it invalid.
Your schema allows to omitt the CurrencyCode
element but if it is present its value must be a string with exactly 3 characters length.
You could weaken your restriction to allow 0-length values by specifying min and max length:
<xs:element name="CurrencyCode" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0" />
<xs:maxLength value="3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
This will however allow values like "EU " which is not a valid currency code.
A different approach would be, to specify a list of valid currency code values and include an empty string as a valid code:
<xs:element name="CurrencyCode" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
<xs:enumeration value="USD"/>
<xs:enumeration value="GBP"/>
<xs:enumeration value="EUR"/>
<!-- add all currency codes you need -->
</xs:restriction>
</xs:simpleType>
</xs:element>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With