Question

I have some problems with XML validation using SAX parser. Here is a simple XML Schema I made to present the problem:

<?xml version="1.0"?>
<xs:schema targetNamespace="urn:test"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    xmlns="urn:test">

    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="content" type="ContentType"
                    maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="ContentType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute ref="title" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:attribute name="title" type="xs:string" />

</xs:schema>

And here is a pretty simple XML file which in my opinion should be valid with regards to my schema:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="urn:test">
        <content title="Title">
            Content comes here...
        </content>
</root>

The interesting thing is that when I try to parse this XML file, I receive the following validation error:

cvc-complex-type.3.2.2: Attribute 'title' is not allowed to appear in element 'content'.

But if I remove the title attribute of the content element from the XML file, I still receive validation errors:

cvc-complex-type.4: Attribute 'title' must appear on element 'content'.

I have no idea what the problem is. Of course, this is just a simple example presenting the problem. I would like to understand the cause of this behavior. Also, it would be nice to find a resolution. I am not sure whether my Java code doing the validation is important in this case, I will post it later if it is necessary.

Any help would be highly appreciated.

Was it helpful?

Solution

The global declaration of the title attribute puts that attribute in the target namespace urn:test. That also means that you must qualify the reference to the attribute, both in the schema and in the instance document. By default, unqualified attributes have no namespace.

<xs:schema targetNamespace="urn:test"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="urn:test" xmlns:test="urn:test" >
....    
<xs:complexType name="ContentType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute ref="test:title" use="required" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

<xs:attribute name="title" type="xs:string" />

and

<root xmlns="urn:test" xmlns:test="urn:test" >
    <content test:title="Title">
        Content comes here...
    </content>
</root>

This whole thing is quite subtle and when I try to validate the original instance document in ecplise I get two very confusing errors:

  1. The title attribute cannot appear on the content element. This refers to the unqualified use of the attribute, and
  2. The title element must appear on the content element. This refers to the missing qualified test:title attribute.

Granted, the error message could use a bit more context information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top