XML namespace Analysis

Source: Internet
Author: User
XML instance

Before introducing the XML namespace, let's take a look at the XML Code Section:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">          <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">    <property name="scopes">      <map>        <entry>          <bean class="org.springframework.context.support.SimpleThreadScope"/>        </entry>        </map>    </property>  </bean>    <bean id="bar" class="x.y.Bar" scope="thread">    <property name="name" value="Rick"/>    <aop:scoped-proxy/>  </bean>  </beans>

This code is taken from a configuration code section of the Spring framework.

A Brief Analysis: many elements in this section of XML Code do not use any prefix, because their default namespace is "http://www.springframework.org/schema/beans", the address of the schema corresponding to the namespace: http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

This Code also has another namespace prefixed with AOP "http://www.springframework.org/schema/aop", which corresponds to the schema address: http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

<AOP: scoped-proxy/> This Code uses the scoped-proxy element prefixed with AOP.

Other elements, including beans, beans, property, and map, are defined through schema. Let's take a look at schema knowledge.

XML Schema

XML schema is an XML-based DTD substitution.
XML Schema describes the structure of an XML document.
The XML schema language is also called XML schema definition (XSD ).

The above text is taken from the introduction to schema in W3C. Yes, XML schema is used to describe the structure of XML documents.

 

Next we will write a schema file:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"    targetNamespace="http://www.gogogo.com"    xmlns="http://www.gogogo.com"    elementFormDefault="qualified">          <xs:element name="name" type="xs:string"/>          <xs:element name="Employee">    <xs:complexType>      <xs:sequence>        <xs:element name="id" type="xs:int"/>        <xs:element ref="name"/>        <xs:element name="age" type="xs:int"/>        <xs:element name="birth" type="xs:date"/>      </xs:sequence>    </xs:complexType>  </xs:element>  </xs:schema>

This simple schema means defining a complex type named "employee. The element has four sub-elements: ID, name, age, and birth.

Introduction to XML Namespaces

The XML namespace provides methods to avoid element naming conflicts.

  The above text is taken from the W3C introduction to the XML namespace.

 

How can this problem be solved. In fact, this namespace is the same as the namespace in. Net or the package in Java. It is used to solve some naming conflicts.

For example:

For example, the elements of the employee type are defined in the schema files a. XSD and B. XSD. How can we differentiate the two employee elements without the same type name? The answer is to use namespaces for differentiation.

 

Next we will analyze the code in the schema defined in this article:

Next we will re-write this schema code in another way:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"    targetNamespace="http://www.gogogo.com"    xmlns:my="http://www.gogogo.com"    elementFormDefault="qualified">          <xs:element name="name" type="xs:string"/>          <xs:element name="Employee">    <xs:complexType>      <xs:sequence>        <xs:element name="id" type="xs:int"/>        <xs:element ref="my:name"/>        <xs:element name="age" type="xs:int"/>        <xs:element name="birth" type="xs:date"/>      </xs:sequence>    </xs:complexType>  </xs:element>  </xs:schema>

The Code remains unchanged, but the default namespace is not used. Use the schema's external namespace address under the my prefix. In this way, the ref name must use the my prefix, because the default namespace is no longer available, and the schema does not know how to find the "name" element.

 

I read two sections of code and summarized the usage of the namespace in the schema:

            XML: Your prefix = "Your namespace address"

Examples:

            XML: My = "http://www.my.org", XML: OMG = "http://www.omg.org", XML: Java = "http://www.java.org "........

References

Http://www.w3school.com.cn/schema/index.asp

Http://www.w3school.com.cn/xml/xml_namespaces.asp

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.