Detailed introduction to XML and Schema namespaces

Source: Internet
Author: User
XML and Schema have the characteristics of unrelated platforms, technical vendors, simplicity, unified specifications, and so on. they are very open, so they are widely used and simple to use. in XML and Schema and, I personally think it is hard to understand the namespace issue. I will give it a detailed understanding in this blog. Namespace is a mechanism provided by W3C Recommendation standards to uniformly name elements and attributes in XML documents. The namespace can be used to clearly identify and combine elements and attributes from different tag vocabulary in XML documents, avoiding conflicts between names. XML and Schema have the characteristics of unrelated platforms, technical vendors, simplicity, unified specifications, and so on. they are very open, so they are widely used and simple to use. in XML and Schema and, what I think is hard to understand is the # wiki/220.html "target =" _ blank "> namespace problem. I will give it a detailed understanding in this blog.

Namespace is a mechanism provided by W3C Recommendation standards to uniformly name elements and attributes in XML documents. The namespace can be used to clearly identify and combine elements and attributes from different tag vocabulary in XML documents, avoiding conflicts between names.

People who have used DTD should know that naming conflicts are a major problem with DTD, and the introduction of the namespace concept in Schema can solve this problem well. For details, refer:

1. declare a namespace

The namespace declaration is generally in the form of: the first part is a keyword xmlns:, the second part is the namespace prefix, the third part is an equal sign, and the fourth part is double quotation marks, include the namespace identifier URI in the fifth part. Note that the prefix of a namespace cannot be xml, because this string is reserved for special purposes in XML. Example:

Xmlns: tns = "http://www.whtest.com/" // where tns is the prefix.

You can also implicitly declare the namespace, that is, omit the colon and namespace prefix. Example:

Xmlns = "http://www.whtest.com/" // note that only one namespace can be implicitly declared in one document


2. namespace in Schema: (1) global component in Schema

Global composition refers to the direct sub-nodes of the element xsd: schema, including element declaration, attribute declaration, complex/simple type definition, group definition, and attribute group definition.

 
 
  
// The Target namespace of the Schema is defined on the root element using the targetNamespace attribute. // The Global Schema component is placed in the namespace http://www.test.com/ns/ns_test.
 
(2) non-global components in the Schema

You can use the following method to define non-global components in the target space.

 
 

The default value of the attribute elementFormDefault is unqualified, which specifies that only global components are defined in the target namespace. Assign the value of elementFormDefault to qualified so that the target namespace contains non-global element definitions. Similarly, assigning the attributeFormDefault value to qualified allows the target namespace to contain non-global attribute definitions. As follows:

 
 

You can also modify the value of the form attribute so that some non-global components are not included in the namespace. As follows:

 

(3) targetNamespace

After a targetNameSpace is defined in the xsd file, all the elements, attributes, and types internally defined by the targetNameSpace belong to the targetNameSpace, and these elements are used by the xsd file, attributes must be found in the defined targetNameSpace.

TargetNamespace defines the namespace of the new elements and attributes defined in the Schema. The namespace "www.w3.org/2001/XMLSchema" defines elements such as element, attribute, complexType, group, and simpleType.

If you do not use reusable components, you only need to define targetNameSpace without specifying aliases.

3. namespace in XML documents

In XML, the use of a namespace involves the concept of a category, that is, the coverage of a namespace. it refers to the elements and attributes in the namespace, which are not in the namespace. The namespace can be used to limit the entire XML document or only part of the XML document.

(1). namespace limits the entire XML document

 
    
  
   Tom
     12   
  
   male
  
 
(2) namespaces are only part of the XML document.

 
    
  
   Tom
     12   
  
   male
  
 
(3) nested namespaces

 
    
  
   
Tom
  12   
  
   
Male
  
 // In this example, except that the element sex is defined in the new namespace, other elements still use the original namespace.
(4) schemaLocation

The schemaLocation attribute references the XML Schema document (. xsd) with the target namespace ). All newly created elements, attributes, and Other. xsd files used in the xml file must be declared here.

 

AnyURI is a schema location that contains a limited (namespace architecture) schema structure. The first URI reference in each pair is the namespace name, and the second is the location that describes the namespace architecture.

Associate schema documents with target namespaces with instance documents. Multiple pairs of URI references can be listed. each pair has different namespace names.
According to the W3C XML architecture recommendation, the XML instance documentation can specify the xsi: schemaLocation and xsi: noNamespaceSchemaLocation attributes at the same time. You can also list the same namespace multiple times.

The following example shows how to use xsi: the schemaLocation attribute provides location information for multiple XML schema documents.

    
  
   John
     28   
  
   59
     ....
 
(5) noNamespaceSchemaLocation

The noNamespaceSchemaLocation attribute references XML schema documents without a target namespace.

    
 

Like SchemaLocation, anyURI is an architecture location that contains a non-limited (namespace-free architecture) architecture structure.

The XML architecture is not required to have namespace. To specify a location for an XML schema without a target namespace, use the noNamespaceSchemaLocation attribute. The XML schema referenced in this attribute cannot have a target namespace. Because this attribute does not accept the URL list, only one architecture location can be specified.
According to the W3C XML architecture recommendation, the XML instance documentation can specify the xsi: schemaLocation and xsi: noNamespaceSchemaLocation attributes at the same time.

The following example shows how to use the xsi: noNamespaceSchemaLocation attribute for instance documents that contain non-finite elements.

 
 
  
   
    Java in action
   
   
    Java is good
   
   
    Bruce
   
  
 


Through the above analysis, we can see that the namespace labels of XML and Schema use the same format (this is also the advantage of Schema over DTD ), however, both XML and Schema have their own unique attributes, which are determined by their different functions. Schema mainly provides services for XML, therefore, targetNameSpace is defined to declare the namespace name, while XML requires schema services. Therefore, SchemaLocation is required to declare the namespace used.

The above is a detailed description of the XML and Schema namespaces. For more information, see other related articles in the first PHP community!

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.