XML Schema: Elementformdefault and Attributeformdefault: The Definition of Elementformdefault
XML Schema: Elementformdefault and Attributeformdefault: The Definition of Elementformdefault
XML Schema: Elementformdefault and Attributeformdefault: The Definition of Elementformdefault
attributeFormDefault
by Jason Shapiro | Dec 15, 2012
When I teach the XML Schema portion of our Complete Java Web Services class, I eventually
reach the subject of form defaults. This is a difficult topic to understand, so I’ve created the
following example as a review, to simplify and solidfy the concepts and usage of this attribute.
1. Namespaces
2. Qualified vs. Unqualified Elements
3. Global vs. Local Elements
Namespaces
In this context, a namespace is a unique string used to disambiguate different XML elements that
have been defined with the same name. Typically, the namespaces are defined as URLs, because
URLs are more likely to be unique than a random word. No one outside of InterTech is likely to
use http://www.intertech.com/band as their namespace, whereas simply using the word band
might cause a conflict. A common source of confusion, due to the use of URLs, is the belief that
a namespace kicks off some network activity. The URL here is simply a unique string; there
may be an actual document at the URL specified, but it is not used by the XML parser.
1|Page
For example, here are two “title” elements that need to be treated differently by the parser. One
title is a title of a course and the other title is a job title for an employee. To disambiguate, two
different namespaces are used in this XML document:
In addition, namespaces can be used to link XML elements to their schema definition. A
schema defines a namespace for all of its elements with the targetNamespace attribute.
When one or more elements in an XML document are to be associated with a schema, their
source namespace must match the schema’s targetNamespace. Note that the band prefix below
has been associated with the same http://www.intertech.com/band targetNamespace defined in
the schema element. Now, any element that has “band:” as a prefix is associated to that
schema. Also, the xsi:schemaLocation attribute is used to associate a namespace with the
location of the schema file.
There can only be one namespace associated without a prefix per XML document.
2|Page
1: <?xml version="1.0" encoding="UTF-8"?>
2: <!-- The "http://www.intertech.com/band" namespace does not have a prefix.
3: Therefore it is the "default namespace." -->
4: <!-- "band" is qualified in the default namespace -->
5: <band xmlns="http://www.intertech.com/band"
6: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7: xsi:schemaLocation="http://www.intertech.com/band ./band.xsd">
8: <!-- "bandName" is qualified in the default namespace -->
9: <bandName>The Intertech Experience</bandName>
10: </band>
Once a default namespace has been established for an element, all of its descendants are
considered qualified, unless the default namespace is overridden with an empty set of quotes:
band.xsd
1: <?xml version="1.0" encoding="UTF-8"?>
2: <schema xmlns="http://www.w3.org/2001/XMLSchema"
3: targetNamespace="http://www.intertech.com/band"
4: xmlns:tns="http://www.intertech.com/band"
5: elementFormDefault="qualified"
6: attributeFormDefault="unqualified">
7:
8: <complexType name="Band_Member_Type">
9: <sequence>
10: <!-- LOCAL: not an immediate child of "schema" -->
11: <element name="fullName" type="string" />
12:
13: <!-- GLOBAL: a ref to an immediate child of "schema" -->
14: <element ref="tns:instrument" maxOccurs="unbounded" />
15: </sequence>
16: </complexType>
17:
18: <!-- GLOBAL: an immediate child of "schema" -->
3|Page
19: <element name="instrument" type="string" />
20:
21: <!-- GLOBAL: an immediate child of "schema" -->
22: <element name="bandName" type="string" />
23:
24: <!-- GLOBAL: an immediate child of "schema" -->
25: <element name="band">
26: <complexType>
27: <sequence>
28: <!-- GLOBAL: a ref to an immediate child of "schema" -->
29: <element ref="tns:bandName" />
30:
31: <!-- LOCAL: not an immediate child of "schema" -->
32: <element name="member" type="tns:Band_Member_Type" minOccurs="1"
33: maxOccurs="unbounded" />
34: </sequence>
35: <attribute name="id" type="string" />
36: </complexType>
37: </element>
38: </schema>
elementFormDefault=”qualified”
Setting this attribute to “qualified” means all global and local elements from this schema must
be qualified when used in an XML document. For example, lets use the band.xsd schema from
above, along with the following:
cd.xsd
1: <?xml version="1.0" encoding="UTF-8"?>
2: <schema xmlns="http://www.w3.org/2001/XMLSchema"
3: targetNamespace="http://www.intertech.com/cd"
4: xmlns:tns="http://www.intertech.com/cd"
5: xmlns:band="http://www.intertech.com/band"
6: elementFormDefault="qualified"
7: attributeFormDefault="unqualified">
8: <import namespace="http://www.intertech.com/band"
9: schemaLocation="./band.xsd" />
10:
11: <!-- GLOBAL: an immediate child of schema -->
12: <element name="cd">
13: <complexType>
14: <sequence>
15: <!-- GLOBAL: a ref of an immediate child of schema -->
16: <element ref="band:band" />
17:
18: <!-- LOCAL: not an immediate child of schema -->
19: <element name="title" type="string" />
20: </sequence>
21: </complexType>
22: </element>
4|Page
23: </schema>
Now lets create an XMLdocument from these schemata. Note that since each schema defines
elementFormDefault as qualified, each element must be associated with a namespace.
We could also define one of the namespaces as the “default namespace.” In this example, we’ll
set the http://www.intertech.com/band namespace as the default and remove the prefix. Even
without the prefix, all of the elements in this XML document are qualified:
5|Page
15: <fullName>Jim White</fullName>
16: <instrument>Vocals</instrument>
17: </member>
18: <member>
19: <fullName>Davin Mickelson</fullName>
20: <instrument>Guitar</instrument>
21: </member>
22: <member>
23: <fullName>Andrew Trolesen</fullName>
24: <instrument>Drums</instrument>
25: </member>
26: </band>
27: <cd:title>One</cd:title>
28: </cd:cd>
elementFormDefault=”unqualified”
Setting this attribute to “unqualified” means all global elements from this schema must be
qualified, and all local elements must be unqualified, when used in an XML document.
Using the previous schemata, with the exception of changing each elementFormDefault to
unqualified, we’re left with a rather odd looking XML document due to a seemingly random
collection of elements that require prefixes while others do not. In this case, cd, band,
bandName, and instrument require a prefix because they are global elements, and all global
elements must be qualified with a namespace. The rest are local so they do not have a prefix.
6|Page
26: </band:band>
27: <title>One</title>
28: </cd:cd>
So why would anyone use it? If we were following patterns that produce a minimal amount of
global elements, we could reduce the amount of prefix “bulk” we have in our XML documents
by using “unqualified.” For example, lets rewrite our schemata as follows:
band.xsd
1: <?xml version="1.0" encoding="UTF-8"?>
2: <schema xmlns="http://www.w3.org/2001/XMLSchema"
3: targetNamespace="http://www.intertech.com/band"
4: xmlns:tns="http://www.intertech.com/band"
5: elementFormDefault="unqualified"
6: attributeFormDefault="unqualified">
7:
8: <complexType name="Band_Member_Type">
7|Page
9: <sequence>
10: <!-- LOCAL: not an immediate child of "schema" -->
11: <element name="fullName" type="string" />
12:
13: <!-- LOCAL: not an immediate child of "schema" -->
14: <element name="instrument" type="string"
15: maxOccurs="unbounded" />
16: </sequence>
17: </complexType>
18: <complexType name="Band_Type">
19: <sequence>
20: <!-- LOCAL: not an immediate child of "schema" -->
21: <element name="bandName" type="string" />
22:
23: <!-- LOCAL: not an immediate child of "schema" -->
24: <element name="member" type="tns:Band_Member_Type" minOccurs="1"
25: maxOccurs="unbounded" />
26: </sequence>
27: </complexType>
28: </schema>
cd.xsd
1: <?xml version="1.0" encoding="UTF-8"?>
2: <schema xmlns="http://www.w3.org/2001/XMLSchema"
3: targetNamespace="http://www.intertech.com/cd"
4: xmlns:tns="http://www.intertech.com/cd"
5: xmlns:band="http://www.intertech.com/band"
6: elementFormDefault="unqualified"
7: attributeFormDefault="unqualified">
8: <import namespace="http://www.intertech.com/band"
9: schemaLocation="./band.xsd" />
10:
11: <!-- GLOBAL: an immediate child of schema -->
12: <element name="cd">
13: <complexType>
14: <sequence>
15: <!-- LOCAL: not an immediate child of schema -->
16: <element name="band" type="band:Band_Type" />
17:
18: <!-- LOCAL: not an immediate child of schema -->
19: <element name="title" type="string" />
20: </sequence>
21: </complexType>
22: </element>
23: </schema>
With so many local elements in our schemata, we are now able to create an XML document with
a minimal amount of prefixes:
8|Page
3: xmlns:cd="http://www.intertech.com/cd"
4: xmlns:band="http://www.intertech.com/band"
5: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6: xsi:schemaLocation="http://www.intertech.com/cd ./cd.xsd
7: http://www.intertech.com/band ./band.xsd">
8: <band>
9: <bandName>The Intertech Experience</bandName>
10: <member>
11: <fullName>Jason Shapiro</fullName>
12: <instrument>Bass</instrument>
13: </member>
14: <member>
15: <fullName>Jim White</fullName>
16: <instrument>Vocals</instrument>
17: </member>
18: <member>
19: <fullName>Davin Mickelson</fullName>
20: <instrument>Guitar</instrument>
21: </member>
22: <member>
23: <fullName>Andrew Trolesen</fullName>
24: <instrument>Drums</instrument>
25: </member>
26: </band>
27: <title>One</title>
28: </cd:cd>
Whereas the default namespace causes problems for elements if elementFormDefault is set to
unqualified, attributes have the opposite problem. Any attribute that does not have a prefix is
considered unqualified, even if a default namespace exists. Therefore if a default namespace has
been created for the attribute, and the attributeFormDefault is set to qualified, there will be a
conflict because an attribute cannot be qualified and unqualified at the same time.
9|Page
3: the default namespace), and the schema requires the attribute
4: to be qualified. -->
5: <band id="Inter001" xmlns="http://www.intertech.com/band">
Recommended Usage
In general, because of the potential confusion and odd syntax that can arise from a mix of global
and local elements in a single document, along with issues that can arise with the default
namespace, it is advised to stick with elementFormDefault=”qualified”. It may be more
verbose, but it is also much clearer which namespace/schema owns a given element. The XML
document author can always rely on the “default namespace” if they want to reduce the amount
of prefixes. In addition, attributeFormDefault should be unqualified due to the potential
conflict when using a default namespace.
10 | P a g e