XSD - allow element type as integer OR empty

后端 未结 5 1561
挽巷
挽巷 2020-12-10 01:55

I need to be able to set a simple element type as an integer but allow it to also be empty. This example sends an error if its empty and a blank field is not an integer. H

相关标签:
5条回答
  • 2020-12-10 02:16

    What you have to do is assign restrictions on the same element plus make a union on them such as the following example:

    <xs:element name="job_code">
      <xs:simpleType>
        <xs:union>
          <xs:simpleType>
            <xs:restriction base='xs:string'>
              <xs:length value="0"/>
            </xs:restriction>
          </xs:simpleType>
          <xs:simpleType>
            <xs:restriction base='xs:integer' />
          </xs:simpleType>
        </xs:union>
      </xs:simpleType>
    </xs:element>
    

    By using this restriction, you tell the xml validation to allow any integer value and allowing the element if it is empty.

    0 讨论(0)
  • 2020-12-10 02:17

    I came up to this answer by searching how to get an attribute to be a nullable Integer, using xsd schema, and based on which JAXB classes are generated. I found no answer here, so after I discovered the answer, I decided to share it. The following xsd portion will generate a not nullable type of int (int):

    <xsd:attribute name="length" type="xsd:int" use="required"/>
    

    In Java code this will result:

    @XmlAttribute(name = "length", required = true)
    protected int length;
    

    If we drop the use required, we will get:

    @XmlAttribute(name = "length")
    protected Integer length;
    

    Hope this helps someone. And even if this answer might not be related to the actual question, I find it useful to be here for those who will get here by doing the same search I did!

    0 讨论(0)
  • 2020-12-10 02:18

    You need to set the "nillable" attribute as true:

    <xsd:element name="weight" type="xsd:integer" nillable="true"/>
    

    See the XML Schema Primer.

    0 讨论(0)
  • 2020-12-10 02:26

    We can achieve this by making a SimpleType

    <xs:simpleType name="NullOrInteger">
        <xs:restriction base="xs:string">
             <xs:pattern value="\d*|\s{0}" />
        </xs:restriction>
    </xs:simpleType>
    

    Add NullOrInteger as the type where you want restriction for an integer or null value.

    for example:

    <xs:element name="null_or_int" type="NullOrInteger" />
    
    0 讨论(0)
  • 2020-12-10 02:29
    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
    <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <product>
            <weight xsi:nil="true"/>
        </product>
    </products>
    

    Try the above, should work; most likely you forgot to add the xsi:nil attribute. Also, make sure that the weight element has no character as children (a white space would still not be acceptable). If you do have to pass some characters instead of an integer, than you have to define a union type to allow for both.

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