What does elementFormDefault do in XSD? What does elementFormDefault do in XSD? xml xml

What does elementFormDefault do in XSD?


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 SchemaComponent 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.


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 attribute 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</name>   <x:phone>(801)390-4552</phone></x:author>

please refer this link for more details


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 is unqualified.
    • This arguably is a design mistake by the creators of XSD.
    • Standard practice is to always use elementFormDefault="qualified"so that assignment is in the target namespace as one wouldexpect.
  • It is a rarely used form attribute on xs:element declarations for which elementFormDefault 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 places assignments 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 wasfound starting with element 'assignment'. One of '{assignment}' isexpected.

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 an assignment element. (WTF)
  • What this really means: The { 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.

Solution

  • Vast majority of the time: Add 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.
  • Tiny minority of the time: Change the XML to comply with the XSD'srequirement that 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.