Search code examples
xmlvalidationxsdschemaxmllint

XML schema validation with xmllint: complains about empty tags like <foobar/>


I am having a problem with validating a XML file against a XSD schema with xmllint: xmllint compains with a validity error that a tag like <foobar/> is not expected although foobar is defined in the XSD schema like this:

<xs:element name="foobar" minOccurs="0">
    <xs:simpleType>
        <xs:restriction base="xs:positiveInteger">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="9999"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

For comparison:

<foobar>123</foobar> is valid according to xmllint. xmllint also does not complain, if I strip ths foobar tag completely from the XML file.

Question:

So, what's the point in denying <foobar/>?

Thanks!

P.S.: The actual error message:

myfile.xml:135298: element foobar: Schemas validity error : Element '{http://www.foobaz.com/namespace}foobar': '' is not a valid value of the local atomic type.

P.P.S.: xmllint version 20901


Solution

  • You have said in your schema that the value of the element must be an integer in the range 1 to 9999, but the actual value of the element is empty content. I can't quite see why there's any question that your schema disallows this value.

    If you want to allow either an integer in this range or empty content, there are two possible ways of doing it:

    (a) define a union type whose member types are (i) an integer in the range 1 to 9999, and (ii) a string with facet length=0, or

    (b) define a list type whose item type is an integer in the range 1 to 9999, with the list type having minLength=0, maxLength=1.

    You can also use nillable="true" but then <foobar/> isn't valid content, it has to be <foobar xsi:nil="true"/> which (to my mind) totally defeats the purpose.