user1099385
user1099385

Reputation:

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

Upvotes: 3

Views: 820

Answers (2)

Michael Kay
Michael Kay

Reputation: 163468

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.

Upvotes: 2

Xstian
Xstian

Reputation: 8282

The number or normal values can be either a positive integer or an empty string.

below a possible solution, otherwise you should use nillable="true".

<xs:simpleType name="positive-integer-or-empty">
    <xs:annotation>
        <xs:documentation>The number-or-normal values can be either a positive integer or an empty string. This is used for the content of the ensemble element.</xs:documentation>
    </xs:annotation>
    <xs:union memberTypes="positive-integer-restricted">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value=""/>
            </xs:restriction>
        </xs:simpleType>
    </xs:union>
</xs:simpleType>
<xs:simpleType name="positive-integer-restricted">
    <xs:restriction base="xs:positiveInteger">
        <xs:minInclusive value="1"/>
        <xs:maxInclusive value="9999"/>
    </xs:restriction>
</xs:simpleType>

Upvotes: 1

Related Questions