Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making an element optional using xsd -cvc-length-valid ERROR

Tags:

xsd

<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 ?

like image 450
Praneel PIDIKITI Avatar asked Feb 18 '11 10:02

Praneel PIDIKITI


People also ask

How do you make a field optional in XSD?

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.

What does Nillable mean in XSD?

The presence of the xsd:nillable attribute in an XSD element means that the corresponding element in the XML file permits null values.

What is Nillable false in XSD?

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.


1 Answers

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>
like image 77
Filburt Avatar answered Sep 24 '22 16:09

Filburt