XSD Element Not Null or Empty Constraint For Xml?

前端 未结 5 2179
栀梦
栀梦 2020-12-05 13:44

This is my sample XML Code:





相关标签:
5条回答
  • 2020-12-05 14:26

    @Kamal has given you basically right answer here. This is why - nillable always seems to cause problems. Effectively, you can consider nillable as meaning allow the xsi:nil attribute on this element. The XML Schema spec describes nillable as an out of band signal - it's basically used to indicate NULL to databases.

    What you want is an element that must be at least one character long as given by @Kamal

    0 讨论(0)
  • 2020-12-05 14:38

    This was my favourite solution.

    <xs:simpleType name="NonEmptyString">
        <xs:restriction base="xs:string">
            <xs:pattern value="[\s\S]*[^ ][\s\S]*"/>
        </xs:restriction>
    </xs:simpleType>
    
    0 讨论(0)
  • 2020-12-05 14:44

    Try

    <xs:element name="lastName" minOccurs="1" nillable="false">
      <xs:simpleType>
         <xs:restriction base="xs:string">
           <xs:minLength value="1"/>
         </xs:restriction>
      </xs:simpleType>
    </xs:element>
    
    0 讨论(0)
  • 2020-12-05 14:44

    This is IMHO a better pattern:

    <xs:simpleType name="NonEmptyString">
       <xs:restriction base="xs:string">
          <xs:pattern value="^(?!\s*$).+" />
       </xs:restriction>
    </xs:simpleType>
    

    or

    <xs:simpleType name="NonEmptyStringWithoutSpace">
       <xs:restriction base="xs:string">
          <xs:pattern value="\S+"/>
       </xs:restriction>
    </xs:simpleType>
    
    0 讨论(0)
  • 2020-12-05 14:45
    <xsd:element name="lastName" type="NonEmptyString" nillable="false"/>
    
    <xsd:simpleType name="NonEmptyString">
      <xsd:restriction base="xs:string">
        <xsd:minLength value="1" />
        <xsd:pattern value=".*[^\s].*" />
      </xsd:restriction>
    </xsd:simpleType>
    
    0 讨论(0)
提交回复
热议问题