[Java] Java Core 74: XML (Part 2)



insert image description here

7 Schema constraints (just understandable)

Like dtd constraints, schema is also used to constrain xml files. When schema constraints are written, it follows the grammatical rules of xml. When writing the schema, it is the same as writing the document structure of the xml file.

Note: When writing a schema file, its file extension is xsd.

1 Write schema constraints

1. First create an xml file. Then write the schema constraints that conform to the specification according to the xml file.

Create the books.xml file:

insert image description here

The code looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book>
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
	</book>
	<book>
		<name>java从入门到精通</name>
		<author>黑旋风</author>
		<price>88.8</price>
	</book>
</books>

2. Next we need to create a schema file:

step:

Step 1: First enter the file creation window, select the project and right-click with the mouse, new–>File, and then enter the following page:

insert image description here

Step 2: Enter the name of the created file and the suffix xsd of the schema file;

insert image description here

Step 3: Because the schema constraint file itself is xml, declare that the header of the xml file is applicable to the schema constraint file.

insert image description here

Step 4: Copy the following content to the books.xsd that has been created above. Then we can write schema constraints.

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		   targetNamespace="http://www.example.org/books"
		   elementFormDefault="qualified">
</schema>

illustrate:

1) The structure of the schema constraint is the same as the writing specification of the xml file, <?xml version="1.0" encoding="UTF-8"?> represents the declaration of xml, which is the root tag;

2) xmlns="http://www.w3.org/2001/XMLSchema" indicates that this schema file is subject to the specified constraints of the w3 organization;

3) targetNamespace="http://www.example.org/books", called the namespace, which is equivalent to the function of the package in java, distinguishing different labels in different constraints. When the xml file constrained by the current schema file is required, the current schema file needs to be imported by the current name.

4) elementFormDefault="qualified", if the value is qualified, then all tags in the current schema are placed in the namespace by default. If the value is unqualified, then except for the root tag in the schema, which is in the namespace http://www.example.org/books package, other tags will not be in this package. In development, we all write the default value qualified.

After the above understanding is completed, let's read a complete schema constraint:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/books"
		elementFormDefault="qualified">
		<!-- 在此处书写schema约束语法 -->
		<element name="books">
			<complexType><!-- 声明books是复杂标签 -->
				<sequence><!-- 通过sequence标签指定子标签的顺序 -->
					<element name="book" maxOccurs="unbounded">
						<complexType>
							<sequence>
								<element name="name" type="string"></element>
								<element name="author" type="string"></element>
								<element name="price" type="double"></element>
							</sequence>
							<attribute name="address"></attribute>
						</complexType>
					</element>
				</sequence>
			</complexType>
		</element>
</schema>

illustrate:

1. The tags appearing in xml need to be defined using such syntax. That is, first know how many tags are needed in the xml, and how many element tags are written in the Schema file.

So the first books tag that appears in xml needs to be declared with .

The name attribute in the element tag is the name of the tag that can be written in xml.

2. In order to facilitate the writing of schema constraints, we simply divide the tags in xml into two categories:

​ a) Simple label: only text data in the label;

b) Complex tags: There are sub-tags or attributes plus text data in the tag;

​In the element tag, you need to use complexType to declare that the name attribute of the current element tag specifies a complex tag.

​If it is a simple tag, you can use simpleType.

3. In the above-mentioned books.xml file, we found that the books tag is a complex tag. For complex tags, it is necessary to write sub-tags in the current tag to limit other content in the current complex tag.

So we need to use tags to declare that the books tag is a complex tag. And for subtags appearing in complex tags, we need to use tags to declare the order in which subtags appear.

A) The subtag that appears in the books tag is book. Since book is also a tag, we also need to use tags to declare the book in xml;

B) And the book tag is also a complex tag, so we need to use tags to declare;

C) There are also sub-tags in the book tag, so it needs to be used to declare the order of the sub-tags;

D) Finally, it was found that the book tag appeared multiple times in books.xml, so it was necessary to add the maxOccurs="unbounded" attribute. Indicates that the book tag can appear many times. (no limit on number of times)

Greater than or equal to 1 time

5. Finally, write the statement of the three name, author, and price sub-tags that appear in the book tag. And for the attributes that appear in the book tag. we need to use

Such syntax to declare. Note the placement of the labels.

Add an address attribute to the books.xml file:

insert image description here

So add an attribute in the books.xsd schema constraint. Note the placement of the labels.

2 Introduce schema constraints in the xml file

Steps to introduce schema constraints in the books.xml file:

Step 1: First place the mouse behind the root label books, make a space, and then copy the following content to the back of books

code show as below:

<books xmlns="default namespace"
		xsi:schemaLocation="{namespace} {location}"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Then introduce the diagram of schema constraints

insert image description here

Final complete code:

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/books"
		xsi:schemaLocation="http://www.example.org/books books.xsd"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		>
	<book address="藏经阁">
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
	</book>
	<book>
		<name>java从入门到精通</name>
		<author>黑旋风</author>
		<price>88.8</price>
	</book>
</books>

illustrate:

1) xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", indicating that the current xml file is an instance subject to schema constraints, and there is no need to change here;

2) xmlns="http://www.example.org/books", the value of targetNamespace in the schema constraint file, means that the name space is introduced in the books.xml file;

3)xsi:schemaLocation=“http://www.example.org/books books.xsd”,

​The value of targetNamespace in the schema path of the schema constraint file



insert image description here

Guess you like

Origin blog.csdn.net/m0_60915009/article/details/131400370