【问题标题】:Create an XSD optional decimal element with restrictions创建具有限制的 XSD 可选十进制元素
【发布时间】:2011-10-24 21:47:07
【问题描述】:

我已经设法使用以下方法创建了一个可选的小数元素:

  <xs:simpleType name="OptionalDecimal">
    <xs:union memberTypes="xs:decimal empty-string" />
  </xs:simpleType>

但我还需要添加限制,以便如果已输入,例如将其限制为最大长度 10 和最大 3 位小数。所以我得到了这个:

<xs:restriction base="xs:decimal">
  <xs:maxInclusive value="9999999999"/>
  <xs:fractionDigits value="3"/>
</xs:restriction>

问题是我不知道如何组合它们。它们可以结合起来吗?或者有更好的方法吗?

【问题讨论】:

  • 你真的需要让 type 接受空字符串吗?你不能改用可选元素/属性吗?为什么不定义一个 RestrictedDecimal 类型,就像你做空字符串一样,然后让联合类型的成员类型为 RestrictedDecimal 或空字符串?
  • @Kevin 大声笑是的,我明白你的意思,不知道为什么我之前没看到!感谢您的帮助

标签: xml xsd xsd-validation


【解决方案1】:

感谢 Kevin 的建议,我想出了这个办法:

  <xs:simpleType name="Decimal10-2">
    <xs:restriction base="xs:decimal">
      <xs:maxInclusive value="9999999999"/>
      <xs:fractionDigits value="2"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="OptionalDecimal10-2">
    <xs:union memberTypes="Decimal10-2 empty-string" />
  </xs:simpleType>

【讨论】:

    【解决方案2】:

    你必须使用“选择”元素:看这个

    <xsd:simpleType name="OptionA">
        <xsd:restriction base="xsd:string">
            <xsd:length value="11" fixed="true"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    
    <xsd:simpleType name="OptionB">
        <xsd:restriction base="xsd:string">
            <xsd:length value="14" fixed="true"/>
            <xsd:whiteSpace value="collapse"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    
    <xsd:complexType name="MyField">
        <xsd:choice>
            <xsd:element name="OptA" type="OptionA" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="OptB" type="OptionB" minOccurs="1" maxOccurs="1"/>
        </xsd:choice>
    </xsd:complexType>
    

    最好的问候, 丹

    【讨论】:

    • 谢谢丹,当我看到凯文斯评论了一个更简单的方法时,我正要尝试这个
    猜你喜欢
    • 2018-04-17
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2013-02-03
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多