Exploring the elementFormDefault attribute in the XSD schema File

Source: Internet
Author: User

I believe that friends who have used this attribute may be confused by this attribute. during use, it is often confusing to check and report errors. I think there is nothing wrong with this attribute, if you can write or set it again, but you cannot tell why.

The official document on this attribute is not detailed. Online users also have different opinions on this attribute. A typical online discussion post is: what is the role of elementFormDefault = "qualified" in Schema? (Http://www.xml.org.cn/dispbbs.asp? BoardID = 23 & id = 25672 & replyID = 17053 & star = 1 & skin = 0)

The following is a lot of experiments. In contrast to the statements on MSDN, we can draw some comments from the discussion by netizens.

 

First lookMSDNMediumElementFormDefaultAttribute explanation:

The form of elements declared in the target namespace of the schema. The value must be one of the following strings: qualified or unqualified. The default value is unqualified.

If the value is unqualified, you do not need to use the namespace prefix to limit the elements of the target namespace.

If the value is qualified, you must use the namespace prefix to limit the elements of the target namespace.

 

The description of MSDN is incomplete. The complete description should be:

If the value is unqualified, the root element of the Instance xml must have a namespace limit, which must be the targetNameSpace defined in the schema. However, the sub-elements do not need to and cannot use the namespace prefix to limit the target namespace. The namespace of the sub-element is empty.

If the value is qualified, the xml root element of the instance and all its child elements must use the namespace prefix to limit the target namespace. The namespace must be the targetNameSpace defined in the schema.

 

Let's look at the settings of the namespace of the root element and child element in the xml instance:

1. Use prefix to define a namespace

Use the xmlns: xxx = url syntax in the attributes of the root element to define a namespace prefix xxx. Then, add the prefix and colon Before the elements that need to be specified by this namespace, for example:

<? Xml version = "1.0" encoding = "UTF-8"?>

<N: c xmlns: n = "aaaa">

<N: c1> 3.141593E0 </n: c1>

<N: c2> String </n: c2>

<C3> 0 </c3>

</N: c>

This xml instance defines a prefix named "aaaa". All elements with "n:" added before the element name will have the "aaaa" namespace, element (c3) without a prefix does not have a namespace restriction. Its namespace attribute is "".

2. Use the default namespace to define the namespace of all elements

The separate xmlns attribute of the root element defines the default namespace of an xml instance. The elements under it are all limited by the default namespace except for the prefix. For example:

<? Xml version = "1.0" encoding = "UTF-8"?>

<C xmlns = "aaaa">

<C1> 3.141593E0 </c1>

<C2> String </c2>

<C3> 0 </c3>

</N: c>

The namespace of all elements (c, c1, c2, c3) in the xml instance is "aaaa"

 

Summary:

The elementFormDefault attribute is only used to set whether the sub-elements of an xml instance must have a namespace limit. It does not control which namespace the elements belong. The namespace of elements in an xml instance is determined by the settings of the xml instance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.