开发者

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

<xs:element name="CurrencyCode" mi开发者_C百科nOccurs="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 ?


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>


The <CurrencyCode> element is optional. Instead of using an empty value, the most obvious thing to do is to just remove the whole element if you don't want to give any real value for it.

If you for some reason still want to allow an empty string and a 3 characters long string, then you have at least 3 options that don't require you to weaken the restriction or list every possible value (=no enumerations).

Solution 1, restrict with a regex

Restrict the element value with a regular expression that allows an empty string and all 3 characters long strings.

<xs:element minOccurs="0" name="CurrencyCode-regex">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="|.{3}" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Vertical bar | separates allowed options, dot . allows any character and braces {3} limit the length to 3. Empty string is presented as eh... that "nothing" that appears between the first quotation mark and the vertical bar.

Solution 2, allow element to be nillable

Add attribute nillable="true" on <xs:element>. Then you can use attribute xsi:nil="true" in the instance document to allow this element to have an empty value.

<xs:element minOccurs="0" name="CurrencyCode-nil" nillable="true">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="3" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Usage in the instance XML:

<CurrencyCode-nil xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:nil="true"></CurrencyCode-nil>

Note that an empty value is not allowed if you don't also specify the xsi:nil="true" attribute. You also need to define that xsi namespace.

<!-- This causes a validation error -->
<CurrencyCode-nil></CurrencyCode-nil>

Solution 3, define a default value

Add a suitable default value for the element in your schema. The default value must also fulfil the restrictions of the element's value. Default value will be added to parsed XML tree after schema validation and only if the element contents is empty.

<xs:element minOccurs="0" default="XXX" name="CurrencyCode-default">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="3" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

XML instance before validation:

<CurrencyCode-default></CurrencyCode-default>

How the instance document is interpreted after the schema validation:

<CurrencyCode-default>XXX</CurrencyCode-default>

Here is an example schema and an example XML document that is valid against this schema

Schema:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence maxOccurs="2">
        <xs:element minOccurs="0" name="CurrencyCode-regex">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="|.{3}" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" name="CurrencyCode-nil" nillable="true">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="3" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element minOccurs="0" default="XXX" name="CurrencyCode-default">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="3" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

A valid instance document:

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CurrencyCode-regex>ABC</CurrencyCode-regex>
  <CurrencyCode-nil>ABC</CurrencyCode-nil>
  <CurrencyCode-default>ABC</CurrencyCode-default>
  <CurrencyCode-regex></CurrencyCode-regex>
  <CurrencyCode-nil xsi:nil="true"></CurrencyCode-nil>
  <CurrencyCode-default></CurrencyCode-default>
</root>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜