What does elementFormDefault do in XSD?

What does elementFormDefault and when should it be used?

So, I found some definitions for elementFormDefault values:

qualified - elements and attributes are in the target namespace schema

unskilled - elements and attributes do not have a namespace

So, from this definition, I would think that if the circuit is set to qualified, then why should you type prefix with namespace? And what are the scenarios in which you will have at least one set for the unconditional? I tried Googling, but all I had was a couple of W3C pages that were extremely hard to understand.

This is the file I'm working with right now, why do I need to declare the type as target:TypeAssignments when I declare targetNamespace the same 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> 
+62
xml xml-namespaces xsd xml-validation xsd-validation
Sep 22 '09 at 23:16
source share
6 answers

ElementFormDefault has nothing to do with the type namespace in the schema, this refers to the element namespaces in XML documents that match the schema.

Here is the relevant specification section:

 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 this means is that the targetNamespace that you indicated at the top of the schema applies only to elements of an XML document compatible with the schema if either elementFormDefault is "qualified" or the element is explicitly declared in the schema as having the form = "qualified".

For example: if elementFormDefault doesn't matter -

 <element name="name" type="string" form="qualified"></element> <element name="page" type="target:TypePage"></element> 

will put the "name" elements in the targetNamespace and "page" objects so that they are in the zero namespace.

To save money, you need to put form = "qualified" in each element declaration, indicating elementFormDefault = "qualified" means that targetNamespace is applied to each element if it is not redefined if form = "unqualified" is placed in the element declaration.

+56
Sep 23 '09 at 0:25
source share

Consider the following ComplexType AuthorType used by the 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 the 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 author name attribute is allowed without specifying a namespace (unqualified). Any elements that are part of <xsd:complexType> are considered local to complexType.

if elementFormDefault="qualified"

then the instance must have local elements qualified

 <x:author xmlns:x="http://example.org/publishing"> <x:name>Aaron Skonnard</name> <x:phone>(801)390-4552</phone> </x:author> 

refer to this for more details.

+47
May 04 '14 at 5:54
source share

It is important to note that elementFormDefault refers to locally defined elements, commonly called elements within the complexType block, in contrast to global elements defined at the top level of the schema. With elementFormDefault = "qualified", you can address local elements in a schema from an XML document using the target schema namespace as the default namespace of the document.

In practice, use elementFormDefault = "qualified" to be able to declare elements in nested blocks, otherwise you will have to declare all top-level elements and refer to them in the scheme in nested elements using the ref attribute, which leads to a much less compact scheme.

This bit in the XML Schema Primer says it: http://www.w3.org/TR/xmlschema-0/#NS

+11
Jun 17 '11 at 15:05
source share

elementFormDefault = "qualified" is used to control the use of namespaces in XML instance documents (XML file), and not in the namespaces of the schema document itself (.xsd file).

By specifying elementFormDefault = "qualified", we apply the namespace declaration, which will be used in documents verified using this scheme.

It is customary to indicate this value to declare that items should be qualified, not unskilled. However, since attributeFormDefault = "unqualified" is the default, it does not need to be specified in the schema document unless you want to qualify namespaces.

+5
May 4 '14 at 20:34
source share

A new detailed answer and explanation to the old, frequently asked question ...

The short answer . If you do not add elementFormDefault="qualified" to xsd:schema , then the default value of unqualified means that locally declared elements are in no namespace .

There is a lot of confusion about what elementFormDefault does, but this can be quickly clarified with a short example ...

Optimized 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 do not have a namespace by default.
    • This is because the default value for elementFormDefault is unqualified .
    • This may be a design error for the creators of XSD.
    • The standard practice is to always use elementFormDefault="qualified" so that assignment is in the target namespace, as you would expect.

Looks like Valid XML

This XML looks as if 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> 

Note:

  • By default, the namespace on assignments places assignments and all its descendants in the default namespace ( http://www.levijackson.net/web340/ns ).

Startup validation error

Although it looks correct, the above XML gives the following confusing validation error:

[Error] try.xml: 4: 23: cvc-complex-type.2.4.a: Invalid content was found starting from the assignment element. One of the "{assignments" is expected.

Notes:

  • You would not be the first developer to curse this diagnostic, which seems to indicate that the content is not valid because it is expected to find the assignment element, but actually found the assignment element. ( WTF )
  • What this actually means: { and } around assignment means that validation expected assignment without a namespace . Unfortunately, when he says that he found the assignment element, he does not mention that he found it in the default namespace, which is different from the namespace.

Decision

  • The vast majority of the time: Add elementFormDefault="qualified" to the xsd:schema XSD element. This means that valid XML must place elements in the target namespace when locally declared in the XSD; otherwise, valid XML must host locally declared elements without a namespace.
  • A tiny minority of time: Modify XML to meet XSD requirements so that assignment not in the namespace. This can be achieved, for example, by adding xmlns="" to the assignment element.
+5
Oct 15 '17 at 18:02
source share

I noticed that XMLSpy (at least the 2011 version) requires the definition of targetNameSpace if elementFormDefault = "qualified" is used. Otherwise will not be checked. And also will not generate xmls with namespace prefixes

0
Jul 22 '11 at 9:52
source share



All Articles