Search code examples
xmlvalidationxsdxerces

XML validation fails on attributes


I've some problem validating an xml against a schema that i defined. The weird thing is that the validation fails only if I use the default namespace xmlns="http://retis.sssup.it/duck-lab" while it works like a charm if I define it like xmlns:dl="http://retis.sssup.it/duck-lab".

When I use the empty namespace the validation fails only on the attributes with the following messages:

cvc-complex-type.3.2.2: Attribute 'data_len' is not allowed to appear in element 'data'.    
cvc-complex-type.4: Attribute 'data_len' must appear on element 'data'.

VALID XML:

<dl:duck xmlns:dl="http://retis.sssup.it/duck-lab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://retis.sssup.it/duck-lab ../duck.xsd ">
...
        <dl:data dl:data_len="1" dl:data_name="name uint" dl:data_type="uint16" dl:endianess="big-endian"/>

INVALID XML:

<duck xmlns="http://retis.sssup.it/duck-lab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://retis.sssup.it/duck-lab ../duck.xsd ">
...
    <data data_len="1" data_name="name uint" data_type="uint16" endianess="big-endian"/>

--EDIT--

DUCK.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab"
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified">

    <include schemaLocation="datatypes.xsd"/>
    <include schemaLocation="duck_message.xsd"/>

    <complexType name="DuckDefinitionType" block="#all" final="#all">
        <sequence>
            <element type="dl:MessageType" name="message_description" form="qualified"/>
        </sequence>
    </complexType>

    <element name="duck" type="dl:DuckDefinitionType" />

</schema>

DATATYPES.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab"
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified">
        <attribute name="data_name" type="string"/>
    <attribute name="data_len" type="nonNegativeInteger"/>
    <attribute name="data_type" type="string"/>
    <attribute name="endianess" type="string"/>
        <element name="data">
        <complexType>
            <attribute ref="dl:data_name" use="required"/>
            <attribute ref="dl:data_len" use="required"/>
            <attribute ref="dl:data_type" use="required"/>
            <attribute ref="dl:endianess" use="required"/>
        </complexType>
    </element>
</schema>

DUCK_MESSAGE.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab"
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified">

    <include schemaLocation="datatypes.xsd"></include>
    <complexType name="MessageType">
        <sequence maxOccurs="unbounded">
            <element ref="dl:data"></element>
        </sequence> 
    </complexType>
</schema>

Obviously I can bypass the problem defining a non empty namespace, but I would like to understand what's wrong.

Thanks a lot.


Solution

  • The handling of the default namespace is different for attributes - an attribute without a namespace prefix is NOT associated with the default namespace, it has no namespace, see here.

    This means that in the invalid XML the various attributes data-len, data-name etc. have no namespace, whereas the schema declares them to be in the http://retis.sssup.it/duck-lab namespace due to the form="qualified" directive.