Reputation: 12482
What does elementFormDefault
do, and when should it be used?
So I found some definitions for elementFormDefault
values:
qualified - elements and attributes are in the targetNamespace of the schema
unqualified - elements and attributes do not have a namespace
So from that definition I would think that if a schema is set to qualified then why must you prefix the type with the namespace? And what are the scenarios that you would even have one set to unqualified for that matter? I tried Googling, but all I got were a couple W3C pages that were extremely hard to understand.
This is the file I am working with right now, why do I need to declare the type as target:TypeAssignments
when I declare the targetNamespace
as the same one as xmlns:target
?
<?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"
elementFormDefault="qualified">
<element name="assignments">
<complexType>
<sequence>
<element name="assignments" type="target:TypeAssignments"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="TypeAssignments">
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
<element name="page" type="target:TypePage"/>
<element name="file" type="target:TypeFile"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
<simpleType name="TypePage">
<restriction base="integer">
<minInclusive value="50" />
<maxInclusive value="498" />
</restriction>
</simpleType>
<simpleType name="TypeFile">
<restriction base="string">
<enumeration value=".xml" />
<enumeration value=".dtd" />
<enumeration value=".xsd" />
</restriction>
</simpleType>
</schema>
Upvotes: 114
Views: 132917
Reputation: 916
Consider the following ComplexType AuthorType
used by author
element
<xsd:complexType name="AuthorType">
<!-- compositor goes here -->
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="phone" type="tns:Phone"/>
</xsd:sequence>
<xsd:attribute name="id" type="tns:AuthorId"/>
</xsd:complexType>
<xsd:element name="author" type="tns:AuthorType"/>
If elementFormDefault="unqualified"
then following XML Instance is valid
<x:author xmlns:x="http://example.org/publishing">
<name>Aaron Skonnard</name>
<phone>(801)390-4552</phone>
</x:author>
the authors's name element is allowed without specifying the namespace(unqualified). Any elements which are a part of <xsd:complexType>
are considered as local to complexType.
if elementFormDefault="qualified"
then the instance should have the local elements qualified
<x:author xmlns:x="http://example.org/publishing">
<x:name>Aaron Skonnard</x:name>
<x:phone>(801)390-4552</x:phone>
</x:author>
please refer this link for more details
Upvotes: 72
Reputation: 111716
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.
Upvotes: 77
Reputation: 83061
ElementFormDefault has nothing to do with namespace of the types in the schema, it's about the namespaces of the elements in XML documents which comply with the schema.
Here's the relevent section of the spec:
Element Declaration Schema Component Property {target namespace} Representation If form is present and its ·actual value· is qualified, or if form is absent and the ·actual value· of elementFormDefault on the <schema> ancestor is qualified, then the ·actual value· of the targetNamespace [attribute] of the parent <schema> element information item, or ·absent· if there is none, otherwise ·absent·.
What that means is that the targetNamespace you've declared at the top of the schema only applies to elements in the schema compliant XML document if either elementFormDefault is "qualified" or the element is declared explicitly in the schema as having form="qualified".
For example: If elementFormDefault is unqualified -
<element name="name" type="string" form="qualified"></element>
<element name="page" type="target:TypePage"></element>
will expect "name" elements to be in the targetNamespace and "page" elements to be in the null namespace.
To save you having to put form="qualified" on every element declaration, stating elementFormDefault="qualified" means that the targetNamespace applies to each element unless overridden by putting form="unqualified" on the element declaration.
Upvotes: 87
Reputation: 250
elementFormDefault="qualified" is used to control the usage of namespaces in XML instance documents (.xml file), rather than namespaces in the schema document itself (.xsd file).
By specifying elementFormDefault="qualified" we enforce namespace declaration to be used in documents validated with this schema.
It is common practice to specify this value to declare that the elements should be qualified rather than unqualified. However, since attributeFormDefault="unqualified" is the default value, it doesn't need to be specified in the schema document, if one does not want to qualify the namespaces.
Upvotes: 6
Reputation: 1
I have noticed that XMLSpy(at least 2011 version)needs a targetNameSpace defined if elementFormDefault="qualified" is used. Otherwise won't validate. And also won't generate xmls with namespace prefixes
Upvotes: 0
Reputation: 622
Important to note with elementFormDefault is that it applies to locally defined elements, typically named elements inside a complexType block, as opposed to global elements defined on the top-level of the schema. With elementFormDefault="qualified" you can address local elements in the schema from within the xml document using the schema's target namespace as the document's default namespace.
In practice, use elementFormDefault="qualified" to be able to declare elements in nested blocks, otherwise you'll have to declare all elements on the top level and refer to them in the schema in nested elements using the ref attribute, resulting in a much less compact schema.
This bit in the XML Schema Primer talks about it: http://www.w3.org/TR/xmlschema-0/#NS
Upvotes: 15