创建具有限制的XSD可选十进制元素

时间:2011-10-24 13:05:12

标签: xml xsd xsd-validation

我设法用这个创建一个可选的十进制元素:

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

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

2 个答案:

答案 0 :(得分:6)

感谢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>

答案 1 :(得分:0)

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

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

祝你好运, 丹

相关问题