New, detailed answer and explanation to an old, frequently asked question...
Short answer: If you don't add elementFormDefault="qualified"
to xsd:schema
, then the default unqualified
value means that locally declared elements are in no namespace.
There's a lot of confusion regarding what elementFormDefault
does, but this can be quickly clarified with a short example...
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.levijackson.net/web340/ns"
targetNamespace="http://www.levijackson.net/web340/ns">
<element name="assignments">
<complexType>
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
</schema>
Key points:
assignment
element is locally defined.elementFormDefault
is unqualified
.elementFormDefault="qualified"
so that assignment
is in the target namespace as one would
expect.form
attribute on xs:element
declarations for which elementFormDefault
establishes default values.This XML looks like it should be valid according to the above XSD:
<assignments xmlns="http://www.levijackson.net/web340/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.levijackson.net/web340/ns try.xsd">
<assignment id="a1">
<name>John</name>
</assignment>
</assignments>
Notice:
assignments
places assignments
and all of its descendents in the default namespace (http://www.levijackson.net/web340/ns
).Despite looking valid, the above XML yields the following confusing validation error:
[Error] try.xml:4:23: cvc-complex-type.2.4.a: Invalid content was found starting with element 'assignment'. One of '{assignment}' is expected.
Notes:
assignment
element but it actually found an assignment
element. (WTF){
and }
around assignment
means that validation was expecting assignment
in no namespace here. Unfortunately, when it says that it found an assignment
element, it doesn't mention that it found it in a default namespace which differs from no namespace.elementFormDefault="qualified"
to the xsd:schema
element of the XSD. This means valid XML must place elements in the target namespace when locally declared in the XSD; otherwise, valid XML must place locally declared elements in no namespace.assignment
be in no namespace. This can be achieved,
for example, by adding xmlns=""
to the assignment
element.Credits: Thanks to Michael Kay for helpful feedback on this answer.