使用 xsd -cvc-length-valid 错误使元素可选 [英] making an element optional using xsd -cvc-length-valid ERROR

查看:43
本文介绍了使用 xsd -cvc-length-valid 错误使元素可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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:对于类型#AnonType_CurrencyCodeServiceServiceList",长度为0"的值"对于长度3"不是方面有效的.

cvc-length-valid: Value '' with length = '0' is not facet-valid with respect to length '3' for type '#AnonType_CurrencyCodeServiceServiceList'.

那我该怎么处理呢?

推荐答案

您的架构允许省略 CurrencyCode 元素,但如果它存在,其值必须是长度正好为 3 个字符的字符串.

Your schema allows to omitt the CurrencyCode element but if it is present its value must be a string with exactly 3 characters length.

您可以通过指定最小和最大长度来削弱您的限制以允许 0 长度值:

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>

然而,这将允许诸如EU"之类的值,这不是有效的货币代码.

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>

这篇关于使用 xsd -cvc-length-valid 错误使元素可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆