Share the specific instance code of the XML and Schema namespaces

Source: Internet
Author: User
In the above section, we have introduced in detail some of the content of the namespace in XML and Schema. The following is an example to illustrate the concept of namespace: Example 1. The following example is an XMLSchema file named note. xsd & lt; xsd: schemaxmlns: xsdwww. w3.org2001XMLSchematargetNamespacewww. w the above section describes in detail some of the namespace content of # wiki/220.html "target =" _ blank "> in XML and Schema. The following is an example to learn more:

Example 1: understand the concept of namespaces.

The following example is an XML Schema file named "note. xsd"

 
 
        
            
               
                
                
                
             
          
   
  
 

The following XML document is associated with the preceding XML Schema named "note. xml ". The two documents will be discussed below.

 
 
  
   Tove
  
  
   Jani
  
  
   Reminder
  Don't forget me this weekend!
 


Xmlns: xsd = "www.w3.org/2001/XMLSchema" indicates that the elements and data types used in this schema come from the namespace of "www.w3.org/2001/XMLSchema ). It also indicates that the elements and data types from the "www.w3.org/2001/XMLSchema" namespace must use the "xsd:" prefix. You can also use xsd or xsi as the namespace identifier (prefix of an element or attribute in the declaration. The xmlns attribute contains basic XML schema elements, such as element, attribute, complexType, group, and simpleType.

Any XML Schema Definition Document (XSD) has a top-level schema (XSD) element. The schema (XSD) element definition must contain the namespace www.w3.org/2001/xmlschema. That is, this namespace is a standard namespace defined by the XML Schema Specification-all XML schema elements must belong to this namespace.
This clip: targetNamespace = "www.w3schools.com", indicating that the elements defined by this schema (note, to, from, heading, body) come from the "www.w3schools.com" namespace. The targetNamespace attribute indicates the URI of the namespace corresponding to the schema. That is to say, to declare a namespace in other documents that reference this Schema (including their own documents), its URI should be the property value of targetNamespace. For example, because the extended data types (note, to, from, heading, and body) defined by note. xsd are used, the namespace xmlns = "www.w3schools.com" is also declared ". The namespace is the default namespace (with no prefix ). The targetNamespace attribute declares the XML namespace for all new types explicitly created in the mode.

Let's take a look at what the beginning of the XML document note. xml specified by this schema looks like:

 

The default namespace declaration xmlns = "www.w3schools.com" specifies the XML document in combination with the namespace of the declared XML Schema. (This document uses the data defined in this namespace) xmlns: xsi = "www.w3.org/2001/XMLSchema-instance" is an inherent XML Schema instance namespace in any XML instance documentation, it is defined by the XML Schema specification. Xsi: schemaLocation = "www.w3schools.com note. xsd" specifies the schema location of the namespace, that is, the note. xsd file in the same path.

Example 2: focus on understanding how to use the Schema document to define the type

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. Modify note. xsd to remove the declaration of the default namespace and add a complex type:

 
 
        
           
           
            
     
            
            
          
   
  
     
     
       
        
       
     
   
 

In the above code, the complex type stu cannot be found, because you have defined a namespace "www.w3schools.com", the complex type exists in "www.w3schools.com", so you should modify the code as follows:

 
 
        
           
           
            
     
            
            
          
   
  
     
     
       
        
       
     
   
 

If you do not use reusable components, you only need to define targetNameSpace without specifying aliases.
Through the above example, we can have a deep understanding of 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.

After understanding the two examples above, the content of the Schema namespace should be clear.



The above is the details shared by the specific instance code 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.