Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does elementFormDefault do in XSD?

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> 
like image 470
Levi Avatar asked Sep 22 '09 23:09

Levi


People also ask

What is simpleType and complexType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is targetNamespace in XSD file?

The targetNamespace declares a namespace for other xml and xsd documents to refer to this schema. The target prefix in this case refers to the same namespace and you would use it within this schema definition to reference other elements, attributes, types, etc.

Is targetNamespace an attribute of schema element?

Specifying a target namespace. The following XSD schema specifies a target namespace by using the xsd:targetNamespace attribute. The schema also sets the elementFormDefault and attributeFormDefault attribute values to "unqualified" (the default value for these attributes).

What is Substitutiongroup in XSD?

A substitution group is a construct in XML Schema (XSD) that allows data architects to create a set of elements that can be substituted for a head element. Any top-level element can be defined as the head element of a substitution group.


1 Answers

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.

like image 189
Alohci Avatar answered Sep 19 '22 04:09

Alohci