XML grammar basic grammar

One, XML overview

  • XML Extensible Markup Language: The tags in XML are extensible.
  • XML usage:
    1. Storing data
    2. As the format of the transmission data between the system
    3. As the configuration file of the project
    4. Save structured relational data
  • Comparison of xml and html:
    1. The tags of xml documents can be extended at will, and the tags of html are predefined.
    2. xml is case sensitive, html is not case sensitive
    3. HTML is mainly used to display data, and xml is used to save data.
    4. In html, spaces will be automatically filtered, while xml will not
    5. There can be multiple root nodes in html, and there is only one in xml.

2. Specific grammar

(1), XML document declaration

语法: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

Attributes:

  • version: used to represent the version number of XML
  • encoding: Specify the XML encoding format
  • standalone: ​​used to indicate whether the XML file depends on an external file

Note: The xml document declaration must be placed in the first row and the first column.

(Two), XML element definition

语法: <元素名></元素>

(Three), attribute definition

语法: <元素名 属性="属性值" 属性2="属性值2"></元素>

(Four), notes

语法:<!-- 这是一条注释-->

(5) Notes for XML writing

  • xml is case sensitive
  • XML tags cannot start with numbers or _underscores.
  • xml tag cannot start with xml

Three, XML file DTD constraints

Constraints: In an xml document, a file can be used to constrain the content specification in the document, and the specification here is the constraint.

(1) Writing constraint documents

<!ELEMENT 元素名称>
<!ELEMENT 书架 (书+)>
	<!ELEMENT  (书名,作者,价格)>
	<!ELEMENT 书名 (#PCDATA)>
	<!ELEMENT 作者 (#PCDATA)>
	<!ELEMENT 价格 (#PCDATA)>

(Two), constraint file introduction grammar

<!-- 第一种 内嵌式  -->
<!ELEMENT 书架 (书+)>
	<!ELEMENT  (书名,作者,价格)>
	<!ELEMENT 书名 (#PCDATA)>
	<!ELEMENT 作者 (#PCDATA)>
	<!ELEMENT 价格 (#PCDATA)>
<!-- 第二种 外联式 -->
	<!-- 引入本地DTD文件 -->
	<!DOCTYPE 根元素名称 SYSTEM "本地文件地址">
	<!-- 引入公共DTD文件 -->
	<!DOCTYPE 根元素名称 PUBLIC "dtd名称" "本地文件地址">

(3) Case presentation

Create book.dtd constraint file

<!ELEMENT  (书名,作者,价格)>
	<!ELEMENT 书名 (#PCDATA)>
	<!ELEMENT 作者 (#PCDATA)>
	<!ELEMENT 价格 (#PCDATA)>

Introduce book.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!--  -->
<!DOCTYPE 书架 SYSTEM "book.dtd">
<书架>
	<>
		<书名>JavaWeb</书名>
		<作者>某某</作者>
		<价格>23</价格>
	</>
	<>
		<书名>JavaWeb</书名>
		<作者>某某</作者>
		<价格>23</价格>
	</>
</书架>

(Four), DTD specific grammar

1. Element definition

Basic syntax:

<!ELEMENT 元素名称 元素内容>

Element content:

Setting instructions meaning
PCDATA Indicates that the content nested in the element is normal text
Child element Explain that the element contains elements such as: (element 1, element 2,...)
Mixed content Indicates that the element can contain both characters and data, as well as sub-elements
EMPITES Indicates that the element does not contain character data, nor does it contain child elements
ANY Indicates that the element can contain any character data, or it can already contain sub-elements

Meaning of symbols in content

symbol Specific meaning
question mark[?] Indicates that the object can appear 0 or 1 time
Asterisk[*] Indicates that the object can appear 0 or more times
Plus sign [+] Indicates that the object can appear 1 or more times
Vertical line Indicates that one of the objects in the column is selected
comma[,] Indicates that the objects must appear in order
brackets[()] Used to group elements
2. Attribute definition

grammar

<!ATTLIST 元素名称 元素类型 元素值>

Setting instructions

Setting instructions meaning
#REQUIRED Indicates that the attribute of the element is required
#IMLIED Indicates that the element may or may not contain the attribute
#FIXED Represents a fixed default value

Attribute type

Attribute type Meaning description
CDATA Indicates attribute type character data, which is the same as #PCDATA
Enumerated Enumerated type, when declaring properties, you can limit the value of the property to be selected from a list.
ID A type of attribute is used to uniquely represent the xml document element.

CDATA case

Four, XML file Schema constraints

(1) The difference between Schema and DTD

the difference:

Schema DTD
File suffix xsd File suffix dtd

(2) Name space

The name space is used to introduce the Schema file in the xml document, but multiple schena files can be introduced in an xml' file, and the name space can be used to distinguish different constraint files

(3) Introduce Schema documents

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

Five, summary

Now we rarely have the opportunity to write xml files by ourselves in practical applications. It is enough to copy and import directly, as long as you understand simple grammar specifications.

Guess you like

Origin blog.csdn.net/weixin_44676935/article/details/104789719