[ad_1]
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…
Streamlined version of your XSD:
<?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:
- The
assignment
element is locally defined. - Elements locally defined in XSD are in no namespace by default.
- This is because the default value for
elementFormDefault
isunqualified
. - This arguably is a design mistake by the creators of XSD.
- Standard practice is to always use
elementFormDefault="qualified"
so thatassignment
is in the target namespace as one would
expect.
- This is because the default value for
- It is a rarely used
form
attribute onxs:element
declarations for whichelementFormDefault
establishes default values.
Seemingly Valid XML
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:
- The default namespace on
assignments
placesassignments
and all of its descendents in the default namespace (http://www.levijackson.net/web340/ns
).
Perplexing Validation Error
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:
- You would not be the first developer to curse this diagnostic that seems to say that the content is invalid because it expected to find an
assignment
element but it actually found anassignment
element. (WTF) - What this really means: The
{
and}
aroundassignment
means that validation was expectingassignment
in no namespace here. Unfortunately, when it says that it found anassignment
element, it doesn’t mention that it found it in a default namespace which differs from no namespace.
Solution
- Vast majority of the time: Add
elementFormDefault="qualified"
to thexsd: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. - Tiny minority of the time: Change the XML to comply with the XSD’s
requirement thatassignment
be in no namespace. This can be achieved,
for example, by addingxmlns=""
to theassignment
element.
Credits: Thanks to Michael Kay for helpful feedback on this answer.
[ad_2]