xml文档详解

一、正规文档

1、必须有xml声明语句
属性是否必须说明
versionxml文档的版本基本是:1.0
encodingxml文档字符默认:UTF-8
standalonexml是否为独立文档(解释:是否依赖外部约束文件默认:UTF-8。yes、no

例子:

<?xml version="1.0"?>
2、必须有且仅有一个根元素
3、标签大小写敏感
4、属性值用双引号

例子:

<content attr1="value1" attr2="value2">hello</conemt>
5、标签成对

例子:

<content>hello</conemt>
6、空标签关闭

例子:

<content  />
7、元素正确嵌套

二、规则

元素的命名

<元素名 属性名=“属性值” 属性名=“属性值”>;
(1)可以是字母、属性、符号(_-)
(2)不可以数字、符号开头
(3)不能包含空格

转义字符

转义字符都是:&开始,;结束

字符 预定义字符
<&lt;
>&gt;
"&quot;
&apos;
&&amp;
CDATA

当大量有转义字符时,可以是xml可读性增强
格式:<![CDATA[********]]>
注意:不能有 “]]>”

例子:

<content><![CDATA[<body><div attr="val1"></div></body>]]></content>
PCDATA

代表解析的文本
例子:

<content>hello word</content>

三、DTD技术——xml文件的验证机制

概述

文档类型定义——Document Type Definition

DTD分成三大类

2.第一是内部DTD,第二是外部DTD,第三是内外结合的DTD

DTD的目的

1.团队使用某个标准的DTD文档交换数据
2.使用DTD验证自身的数据

内部文档定义
<!DOCTYPE root-element [element-declarations]> 

例子:

<?xml version="1.0"?>
<!DOCTYPE note [
	<!ELEMENT note (to,from,heading,body)>
	<!ELEMENT to (#PCDATA)>
	<!ELEMENT from (#PCDATA)>
	<!ELEMENT heading (#PCDATA)>
	<!ELEMENT body (#PCDATA)>
	<!ELEMENT em empty>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forge
<em/>

解释:

  • DOCTYPE note (第二行)定义此文档是 note 类型的文档。
  • !ELEMENT note (第三行)定义 note 元素有四个元素:“to、from、heading,、body”
  • !ELEMENT to (第四行)定义 to 元素为 “#PCDATA” 类型
  • !ELEMENT from (第五行)定义 from 元素为 “#PCDATA” 类型
  • !ELEMENT heading (第六行)定义 heading 元素为 “#PCDATA” 类型
  • !ELEMENT body (第七行)定义 body 元素为 “#PCDATA” 类型
  • !ELEMENT em(第七行)定义 em元素为空标签
外部DTD声明
<!DOCTYPE root-element SYSTEM "filename"> 

例子
note.dtd

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)> 
<!ELEMENT em EMPTY>

note.xml

<!DOCTYPE root-element SYSTEM "note.dtd"> 
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
  <em/>
</note> 
声明属性
<!ATTLIST element-name attribute-name attribute-type attribute-value>

DTD实例

<!ATTLIST content title PCDATA "hello">

XML实例

<note title="hello"></note>

属性类型

类型描述
CDATA字符数据(character data)
(val|val2)此值为枚举列表的一个值
ID值为唯一ID
IDREF值为另外一个元素ID
IDREFS值为另外元素的ID列表
NMTOKEN值为合法的xml名称
NMTOKENS值为合法的xml名称列表
ENTITY值为一个实体
NOTATION值为符号名称
xml 一个预定义的XML值

默认值

说明
#REQUIRED:属性必须
#IMPLIED属性非必须
#IMPLIED属性非必须
#FIXED value固定值
实体

概述:
1、类似预定义变量
2、实体使用:& + 实体名称 + ;
3、实体可以外部定义

语法:

<!ENTITY entity-name "entity-value">

实例:

# DTD
<!ENTITY name "xiaoliao">
<!ENTITY ID "12">
# xml
<content>&name;&ID;</content>

外部实体声明

<!ENTITY entity-name SYSTEM "URI/URL"> 

实例

# DTD
<!ENTITY name SYSTEM "http://xxxxx.xx.com/entities.dtd">
<!ENTITY ID SYSTEM "http://xxxxx.xx.com/entities.dtd">
# xml
<content>&name;&ID;</content>

四、xml Schema

说明:DTD的替代品

简易元素

语法

<xs:element name="xxx" type="yyy"/> 

内置类型

类型说明
xs:string字符串
xs:decimal数字
xs:integer整数
xs:Boolean布尔型
xs:date日期。格式:2020-01-01
xs:time时间。格式:00:00:00
属性

语法

<xs:attribute name="atr1" type="xs:string" />

内置类型

类型说明
xs:string字符串
xs:decimal数字
xs:integer整数
xs:Boolean布尔型
xs:date日期。格式:2020-01-01
xs:time时间。格式:00:00:00

实例

<xs:attribute name="ID" type="xs:string"/>

属性默认值和固定值\、可选(默认)或必填

# 默认。自动分配给元素额
<xs:attribute name="ID" type="xs:string" default="123"/>
# 固定。自动分配给元素额
<xs:attribute name="ID" type="xs:string" fixed="123"/>
# 必选
<xs:attribute name="ID" type="xs:string" use="requried"/>
限定 / Facets

数据类型的限定

限定说明
enumeration定义枚举值
fractionDigits小数位的最大位数。必须大于等于0
fractionDigits字符或列表的精确数目。必须大于或等于0
maxExclusive所允许的值必须小于此值。
maxInclusive所允许的值必须小于或等于此值。
maxLength所允许字符或列表的最大数目。必须大于或等于0
minExclusive所允许的值必须大于此值。
minInclusive所允许的值必须大于或等于此值。
minLength所允许字符或列表的最小数目。必须大于或等于0
pattern正则表达式
totalDigits?定义所允许的阿拉伯数字的精确位数。必须大于0。
whiteSpacepreserve、replace、collapse。分别:不做任何处理、移除所有空格、(换行,制表,多个空格)替换成一个空格

实例:

<xs:element name="age">
	<xs:simpleType>
		<xs:restriction base="xs:integer">
			<xs:minInclusive>18</minInclusive>
			<xs:maxInclusive>60</maxInclusive>
		</xs:restriction>
	</xs:simpleType>
<xs:element>
<xs:element name="username">
	<xs:simpleType>
		<xs:restriction base="xs:string">
			<xs:pattern>([a-zA-Z])*</pattern>
		</xs:restriction>
	<xs:simpleType>
</xs:element>
<xs:element name="sex">
	<xs:simpleType>
		<xs:restriction base="xs:string">
			<xs:enumeration></enumeration>
			<xs:enumeration></enumeration>
		</xs:restriction>
	<xs:simpleType>
</xs:element>'
复合元素
  • 1.合空元素
  • 2.包含其他元素的元素
  • 3.仅包含文本的元素
  • 4.包含元素和文本的元素

1.直接声明

<xs:element name="note">
	<xs:complexType>
		<xs:element name="title" type="xs:string" />
		<xs:element name="body" type="xs:string" />
	</xs:complexType>
</xs:element>

实例

<note>
	<title>this is title</title>
	<body>body content</body>
</note>

被包围在指示器\ <sequence>中。这意味着子元素必须以它们被声明的次序出现

2.先定义复合类型(好处:多个地方一起引用)

<xs:element name="note" type="noteInfo" />
<xs:complexType name="noteInfo">
	<xs:element name="title" type="xs:string" />
	<xs:element name="body" type="xs:string" />
</xs:complexType>

3.已有复合类型基础上,添加元素(类似继承)

<xs:element name="note-full" type="noteFullInfo"/>
<xs:complexType name="noteFullInfo">
	<xs:complexContent>
		<xs:extension base="noteInfo">
			<xs:element name="remark" type="xs:string" />
		<xs:extension>
	</xs:complexContent>
</xs:complexType>
<note-full>
	<title>this is title</title>
	<body>body content</body>
	<remark>this is remark</remark>
</note-full>
复合-空元素
<xs:element name="content">
	<xs:complexType>
		<xs:attribute name="title" type="xs:string">
	</xs:complexType>
</xs:element>

实例:

 <content title="测试标题属性" /> `
复合-仅元素
<xs:element name="note">
	<xs:complexType>
		<xs:element name="title" type="xs:string" />
		<xs:element name="body" type="xs:string" />
	</xs:complexType>
</xs:element>

实例

<note>
	<title>this is title</title>
	<body>body content</body>
</note>
复合-仅文本(文本+属性)

此类型仅包含简易的内容(文本和属性),因此我们要向此内容添加 simpleContent 元素

语法:

<xs:element name="somename">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="basetype">
        ....
        ....
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element><xs:element name="somename">
  <xs:complexType>
    <xs:simpleContent>
      <xs:restriction base="basetype">
        ....
        ....
      </xs:restriction>
    </xs:simpleContent>
  </xs:complexType>
</xs:element> 

实例

<xs:element>
	<xs:complexType>
		<xs:simpleContent>
			<xs:extension base="xs:integer">
				<xs:attribute name="title" type="xs:string"/>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
</xs:element>
<contenet title="测试标题">100</content>
指示器

说明:控制元素的使用方式

七种指示器
Order指示器:

  • All
  • Choice
  • Sequence

Occurrence指示器:

  • maxOccurs
  • minOccurs

Group指示器

  • Group name
  • attributeGroup name
Order指示器

all

说明:元素顺序任意,每个元素必须重现一次

sequence

说明:元素必须按着特定的顺序出现

choice

说明:只能出现其中的一个元素

<xs:element>
	<xs:complexType>
		<xs:all>
			<xs:element name="title" type="xs:string"/>
			<xs:element name="body" type="xs:string"/>
		</xs:all>
	</xs:complexType>
</xs:element>
occurrence指示器

说明:定义某个元素出现的次数
注意: 对于所有的 “Order” 和 “Group” 指示器。maxOccurs、minOccurs都是“1”

minCccurs

元素数目必须大于或等于这个值。取值必须大于0

manCccurs

元素数目必须小于或等于这个值。取值必须大于0。
注意:unbounded。代表无限个

<xs:element>
	<xs:complexType>
		<xs:sequence>
			<xs:element name="title" type="xs:string"/>
			<xs:element name="body" type="xs:string" maxOccurs="10" minOccurs="1"/>
		</xs:sequence>
	</xs:complexType>
</xs:element>
group指示器

说明:定义一批元素

group name实例

<xs:group name="noteGroup">
	<xs:sequence>
		<xs:element name="title" type="xs:string" />
		<xs:element name="body" type="xs:string" />
	</xs:sequence>
</xs:group>

<xs:element name="note">
	<xs:complexType>
		<xs:sequence>
			<xs:group ref="noteGroup">
			<xs:element name="remark" type="xs:string"/>
		<xs:sequence>
	</xs:complexType>
</xs:element>

attributeGroup name实例

<xs:attribute name="noteAttrGroup">
	<xs:attribute name="author" type="xs:string" />
	<xs:attribute name="creatorDate" type="xs:date"/>
</xs:attribute>

<xs:element name="note">
	<xs:complexType>
		<xs:sequence>
			<xs:element name="title" type="xs:string"/>
			<xs:element name="body" type="xs:string"/>
		</xs:sequence>
		<xs:attributeGroup ref="noteAttrGroup"/>
		<xs:attribute name="creatorTime" type="xs:time"/>
	</xs:complexType>
</xs:element>
any元素

概述: 扩展未被schema定义的元素
note.xsd

<?xml version="1.0"?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
	<xs:element name="note">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="title" type="xs:string" />
				<xs:element name="body" type="xs:string" />
				<xs:any minOccurs="0"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

child.xrd

<?xml version="1.0"?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
	<xs:element name="chiild">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="number" type="xs:string" />
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

note.xml

<?xml version="1.0" ?>
<note 
xmlns="http://www.xxx.xxx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="note.xsd child.xsd">
	<title>this is a title</title>
	<body>this is a body</body>
	<child>
		<number>123465</number>
	</child>
</note>
anyAttribute

note.xsd

<?xml version="1.0"?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
	<xs:element name="note">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="title" type="xs:string" />
				<xs:element name="body" type="xs:string" />
			</xs:sequence>
			<xs:anyAttribute/>
		</xs:complexType>
	</xs:element>
</xs:schema>

child.xrd

<?xml version="1.0"?>
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
	<xs:attribute name="size">
		<xs:simpleType>
			<xs:restriction extension="xs:string">
				<xs:pattern>large|small</xs:pattern>
			</xs:restriction>
		<xs:simpleType>
	</xs:attribute>
</xs:schema>

note.xml

<?xml version="1.0" ?>
<note 
xmlns="http://www.xxx.xxx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="note.xsd child.xsd"
size="large"
>
	<title>this is a title</title>
	<body>this is a body</body>
</note>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廖钺焕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值