Defining an XML element that must be empty or decimal

后端 未结 3 1617
星月不相逢
星月不相逢 2021-01-22 05:52

I have the following XSD which was generated by VS from an example XML file:

                
                  

        
相关标签:
3条回答
  • 2021-01-22 06:20

    You can combine the two types you wish to allow via xs:union:

    <xs:element name="amperage_rating" type="empty_or_decimal"/>
    
    <xs:simpleType name="empty_or_decimal">
      <xs:union memberTypes="empty xs:decimal"/>
    </xs:simpleType>
    
    <xs:simpleType name="empty">
      <xs:restriction base="xs:string">
        <xs:enumeration value=""/>
      </xs:restriction>
    </xs:simpleType>
    
    0 讨论(0)
  • 2021-01-22 06:35

    It sounds as if your use case is very like some of the motivating cases for xsi:nil. From your description, it sounds as if you made the element nillable, but then fed it an non-nilled instance of the form

    <amperage_rating unit="A"></amperage_rating>
    

    instead of specifying that the element should be nilled:

    <amperage_rating unit="A" xsi:nil="true"></amperage_rating>
    

    If you want to modify the type, the methods described by kjhughes and Michael Kay should work fine.

    0 讨论(0)
  • 2021-01-22 06:43

    There are two approaches. One is what @kjhughes suggests: define a union between xs:decimal and a zero-length-string type. The other, which I personally prefer, is to define a list type with item type xs:decimal and maxLength 1. Although the effect on validation is exactly the same, I think the list type is easier to use when the schema is used for data typing: certainly with XSLT and XQuery, where atomizing the element will give you a sequence of zero or one decimals.

    0 讨论(0)
提交回复
热议问题