4

I've managed to create an optional decimal element using this:

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

but I also need to add restrictions so that if it has been entered, to limit it to a maximum length of 10 and maximum number of 3 decimal places for example. So I've got this:

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

The problem is I don't know how to combine them. Can they be combined? Or is there a better way of doing this?

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • 1
    Do you really need to have the type accept empty strings? Can't you use optional elements/attributes instead? Why not define a RestrictedDecimal type, much as you did empty-string and then let the union type's member types be RestrictedDecimal or empty-string? – Kevin Oct 24 '11 at 13:33
  • @Kevin lol yeah I see what you mean, don't know why I didn't see that earlier! Thanks for your help – Iain Ward Oct 24 '11 at 13:45

2 Answers2

6

Thanks to Kevin's suggestion I've come up with this which does the trick:

  <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>
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
0

you must use a "choice" element: look this

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

Best regards, Dan

Dan
  • 1,518
  • 5
  • 20
  • 48