1.xml Introduction
XML refers to Extensible Markup Language, which is designed to structure, store and transmit information, and is an information transfer tool independent of software and hardware.
2. Basic syntax
1. Document Declaration
When you write an XML document, you need to declare the XML document with the document declaration first. and must appear in the first line of the document .
<?xml version= "1.0" encoding= "UTF-8"?>
This means that this is an XML file that is encoded as UTF-8 when it is written to memory.
2. Tags
The XML must contain a root element that is similar to the parent element of all other elements (HTML).
<beans>
<bean>
</bean>
</beans>
All elements in the XML must have a close tag:
Illegal Tags:
<p>name
<p>age
Legal Tags:
<p>name</p>
The label name must start with a letter or underscore: a,_a;
Avoid using "-", ".", ":" to name the sign
Cannot use XML as a label;
Cannot have spaces: AB;
You cannot use semicolons between characters;
Case sensitive (title and title are not the same tag);
The label attribute value must be preceded by a semicolon:
<title name= "" ></title>
Some characters have special meanings
<title>10 < </title> to write <title>10 < 100<title>
The spaces in the element are preserved;
Comment Format:
<!--This is a comment--
In XML, you should try to avoid using attributes . If the information feels much like data, use the first format.
First format:
<user>
<name>tom</name>
</user>
The second type of format:
<user name= "Tom" >
</user>
Sometimes an ID reference is assigned to an element. These ID indexes can be used to identify XML elements that work in the same way as the id attribute in HTML. It is not part of the label data, it only plays the role of identification.
<user id= "10001" ></user>
3.XML constraints
1.DTD constraints
The role of a DTD is to define the structure of an XML document. It uses a series of legitimate elements to define the document structure:
The DTD can be written in a separate file with the extension DTD and must be saved using UTF-8 encoding.
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE root element SYSTEM "file name" >
Detailed Address http://www.w3school.com.cn/dtd/dtd_intro.asp
2.schema Constraints
The role of XML schemas is to define the legitimate building blocks of XML documents, similar to DTDs.
Role:
1) You can define the elements that can appear in the document
2) You can define properties that can appear in the document
3) You can define which element is a child element
4) You can define the order of child elements
5) You can define the number of child elements
6) You can define whether an element is empty or if it can contain text
7) You can define data types for elements and attributes
8) You can define default values for elements and attributes and fixed valuesone of the most important capabilities of XML Schema is support for data types.
The XML Schema file itself is an XML file, but its extension is usually. xsd. Supports namespaces. An XML Schema document is often called a schema document (a constraint document), and an XML file that follows this document is called an instance document. As with an XML file, an XML Schema document must have a root node, but this root node is named schema. After writing an XML Schema constraint document, it is often necessary to bind the elements declared in this file to a URI address, and there is a professional term in XML Schema technology that describes the process of binding an XML Schema document declaration element to a namespace. The XML file can then be used to tell the parsing engine through this URI (the namespace), where the elements written in the XML document come from and who is bound.
Constraint document:
Schema document (constraint document):
<?xml version= "1.0"?>
<!--XML:XS Displays the elements and data types used in the schema from the namespace "Http://www.w3.org/2001/XMLSchema-
<!--targetnamespace display schema-defined elements from the namespace http://www.w3school.com.cn--
<!--elementformdefault= "Qualifield" indicates that any XML instance document is used and that the elements declared in this schema must be namespace-qualified
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "HTTP://WWW.CNBLOGS.COM/ROOKIE404/"
Xmlns= "HTTP://WWW.CNBLOGS.COM/ROOKIE404/"
elementformdefault= "Qualifield" >
<xs:element name= "User" >
<xs:complexType>
<xs:sequence>
<!--declaration of an element--
<!--fixed to immutable--
<xs:element name= "name" type= "xs:string" fixed= "rookie404"/>
<!--Default--
<xs:element name= "Sex" type= "xs:string" default= "man" >
<xs:element name= "Age" type= "Xs:integer" >
<xs:element name= "Date" type= "Xs:date" >
<!--declare a default element element--
<xs:attribute name= "lang" type= "xs:string" default= "string" >
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:schema>
Instance Document:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--xmlns specifies that the declaration of the default namespace informs the schema validator that all elements used in this XML document are declared in the
"Http://www.cnblogs.com/rookie404"-
<!--xmlns:xsi the element or attribute in this namespace must be written in xsi: this way, such as SchemaLocation is one of his attributes,
So write xsi:schemalocation, and the default namespace does not take like XSI this, in fact, XML tag name has a professional term called QName, and if there is no previous xsi: this is generally called NCName--
<!--The SchemaLocation property has two values. The first value is the namespace that needs to be used. The second value is the location of the XML schema used by the namespace:-->
<user xmlns= "http://www.cnblogs.com/rookie404/"
Xmlns:xsi= "Www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.cnblogs.com/rookie404/http://www.cnblogs.com/rookie404/user.xsd" >
<name>rookie404</name>
<sex>man</sex>
<age>23</age>
<date>2016-09-15</date>
</user>
Detailed Address http://www.w3school.com.cn/schema/index.asp
XML Learning Notes