[Spring] Spring xml configuration dtd and xsd format analysis

1. DTD format

1.1 DTD example in spring

Initially, the header of the Spring configuration file is declared as follows:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
<beans>  
  
</beans>  

Explanation:
1. The first line represents the xml declaration. Any well-formed xml document must be the first line of declaration. It is equivalent to telling the parser that this is an xml document, and you can use the xml parser to parse it.

2. The dtd statement indicates that the elements and attributes in the xml must conform to spring-beans-2.0.dtdthe document type definition standard.

1.2 What is DTD

DTD: The file type definition ( Document Type Definition) of the file can be regarded as a template of one or more XML files, where you can define the elements in the XML file, the attributes of the elements, the arrangement of the elements, the content contained in the elements, and so on.

Disadvantages:
DTD compared to XSD: DTD is written using non-XML syntax.
DTD is not extensible, does not support namespaces, and only provides very limited data types.

Because of some limitations of DTD, and XML Schema's support for data types and namespaces. XML Schema will soon replace DTD.

2. XSD format

2.1 XSD example in spring

<?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"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  
</beans>  
  

XML Schema namespace functions:
1. Avoid naming conflicts, like package in Java.
2. Separate tags with different functions (like tx namespace in Spring for transaction type tags, context namespace for component tags)

Code explanation:
1. xmlns="http://www.springframework.org/schema/beans"
Declare the default namespace of the xml file to indicate the default namespace of all tags that do not use other namespaces.

2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Declare the XML Schema instance, and use the schemaLocation attribute after the declaration

3. xmlns:aop="http://www.springframework.org/schema/aop"
Declare the namespace prefixed with aop, and the URL used to indicate the address of the namespace will not be used by the parser to find information. Its sole function is to give the namespace a unique name. When a namespace is defined in the start tag of an element, all child elements with the same prefix will be associated with the same namespace.

4、

xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

It can be seen from the naming that this attribute must be used in conjunction with the namespace to specify the location of the Schema. This attribute has two values:

  • The first value indicates the namespace that needs to be used
  • The second value indicates the location of the XML schema used by the namespace

So when we need what kind of label, we can introduce what kind of namespace and Schema definition.

2.2 What is XSD

XSD is the abbreviation of XML structure definition, which is called XML Schemas Definition in English. XML Schema describes the structure of XML documents.

You can use a specified XML Schema to verify an XML document to check whether the XML document meets its requirements. The document designer can specify the allowed structure and content of an XML document through XML Schema, and can check whether an XML document is valid based on this. XML Schema itself is an XML document, which conforms to the XML grammatical structure. It can be parsed with a general XML parser.

An XML Schema will define: the elements that appear in the document, the attributes that appear in the document, the sub-elements, the number of sub-elements, the order of sub-elements, whether the elements are empty, the data types of elements and attributes, the default and fixed of elements or attributes value.

Now generally use XSD instead of DTD:

  • First, it can be expanded according to future conditions
  • Second, it is richer and more useful than DTD
  • The third is to write in XML
  • The fourth is to support data types
  • The fifth is to support namespaces.

Advantages of XML Schema:

  1. XML Schema is based on XML and has no special syntax
  2. XML can be parsed and processed like other XML files
  3. XML Schema supports a series of data types (int, float, Boolean, date, etc.)
  4. XML Schema provides an extensible data model.
  5. XML Schema supports comprehensive namespace
  6. XML Schema supports attribute groups.

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/115288121