xml schema:ELEMENTFORMDEFAULT

XML Schema elementFormDefault meaning

When working with XML or Web service sometime you will get issue with namespaces  To make more clear and solve some issues relate this i will explain on this post the use of elementFormDefault on XML Schema

To understand how elementFormDefault affect to our XML document on validate with XML schema we need to understand following concepts:

  • Namespace
  • Global vs. Local elements
  • Qualified vs. Unqualified Elements

1. Namespace

In XML concept, namespace help to distinguish the elements and help to solve issue conflict element names across XML document, For example as below two “name” element in xml but belong difference element should have difference namespaces

<?xml version="1.0" encoding="UTF-8"?>
<Post xmlns:post="http://devjav.com/namespace/post" xmlns:author="http://devjav.com/namespace/author" >
<post:name>SPRING SECURITY WITH CUSTOM AUTHENTICATION</post:name>
<author:name>Pham Thai Thinh</author:name>
</Post>

As above example namespace declare with xmls or xmls:[prefixname]. With just xmls it mean default namespace.

2. Global vs. Local elements

Global elements are elements that are declared immediate under the schema element xs:schema. And other elements are local elements. The local element cannot refer by other declaration but global elements can, also all global elements must be unique. You can look on two samples below for more clear.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://devjav.com/book" xmlns:bk="http://devjav.com/book">
 <xs:element name="Book">
 <xs:complexType>
 <xs:sequence>
 <xs:element ref="bk:name"/>
 <xs:element ref="bk:Author"/>
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 <xs:element name="Author">
 <xs:complexType>
 <xs:sequence>
 <xs:element ref="bk:name"/>
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 <xs:element name="name" type="xs:string"/>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="Book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="Author">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

In localelements.xsd as we see almost element declare as local except Book element.

3.  Qualified vs. Unqualified Elements

A element call qualified if it is associated with a namespace. This associate can be through prefix tag that defined by xmlns attribute for example xmls:book=”http://devjav.com/book”. The associate can be through default namespace it mean without prefix through xmlns without prefix( xmls=”http://devjav.com/book”).

4.  elementFormDefault

After understand three core concept above now we can discuss the meaning of elementFormDefault attribute. This attribute can accept two value qualified and unqualified, without specific default value will be unqualified.

When elementFormDefault=”qualified” it mean all global elements and local elements need qualified with namespaces.

<?xml version="1.0" encoding="UTF-8"?>
<Book xmlns="http://devjav.com/book"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <name>name0</name>
    <Author>
        <name>name1</name>
    </Author>
</Book>

When elementFormDefault=”unqualified” it mean all global elements must qualified with namespace, local element not have that.

<?xml version="1.0" encoding="UTF-8"?>
<bk:Book xmlns:bk="http://devjav.com/book"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <name>name0</name>
    <Author>
        <name>name1</name>
    </Author>
</bk:Book>

Hope with this post you can clear understand about use of elementFormDefault in xml schema define.

Leave a Reply