1、dtd
2、schema
如果想学习详细的教程w3cschool的教程不错
XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 描述 XML 文档的结构。
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。
2.1、命名空间, 使用xmlns定义命名空间,
对比SpringBean.xml的命名空间,学习xmlns
2.2、01.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" <!-- xmlns定义命名空间, 此处是schema的标准命名空间 , 默认必须的-->
targetNamespace="http://www.example.org/01"
<!-- 此处xmlns:tns表示"http://www.example.org/01"处的命名空间叫做tns, 访问这个里面的元素需要使用tns:xx -->
xmlns:tns="http://www.example.org/01"
elementFormDefault="qualified">
<element name="User">
<!-- 定义一个复杂的数据类型 -->
<complexType>
<!-- 在这个类型中,是一个序列的 -->
<sequence>
<!-- 定义一个元素id, 类型为int -->
<element name="id" type="int" />
<element name="username" type="string" />
<element name="born" type="dateTime" />
</sequence>
</complexType>
</element>
</schema>
2.2.1、对应的xml文档的样式
2.2.2、创建一个XML file来引入上面的schema
2.2.2.1、将schema加入库中,
Window–>Perferences—>XML—>XML Catalog—>User Specified Entries–>add
2.2.2.2、有了自动提示。可以使用自定义的文档中的元素
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:xsi表示这是一个根据schema产生的实例 -->
<!-- xsi:schemaLocation引入schema, namespace就是xsd中的targetNamespace, localtion就是xsd中的定义的命名空间 -->
<user xmlns="http://www.example.org/01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/01">
<id>1</id>
<username>chb</username>
<born>1993-09-23</born>
</user>
2.2.3通过一个文件引入命名空间xsi:noNamespaceSchemaLocation="01.xsd"
<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:xsi表示这是一个根据schema产生的实例 -->
<!-- xsi:schemaLocation引入schema, namespace就是xsd中的targetNamespace, localtion就是xsd中的定义的命名空间 -->
<user xmlns="http://www.example.org/01"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="01.xsd">
<id>1</id>
<username>chb</username>
<born>1993-09-23</born>
</user>
2.3、元素与属性的定义
只要不是基本数据类型, 就是用complexType, <>
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/books"
xmlns:tns="http://www.example.org/books"
elementFormDefault="qualified">
<element name="books">
<complexType>
<!-- maxOccurs表示最多出现的此处 , unbonunded表示不限制 -->
<sequence maxOccurs="unbounded">
<!-- 复杂对象, book -->
<element name="book">
<complexType>
<sequence>
<element name="title" type="string"/>
<element name="content" type="string" />
<!-- 作者可能是一个, 也可以是多个, 使用choice, 子节点中多个选一个 -->
<choice>
<element name="author" type="string" />
<!-- 多个作者, 是一个复杂对象, -->
<element name="authors">
<complexType>
<sequence maxOccurs="3">
<element name="author" type="string" />
</sequence>
</complexType>
</element>
</choice>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
属性, attribute
3、建议编写方式: 百叶窗
一个根节点person, 通过simpleType完成重用
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/person"
xmlns:tns="http://www.example.org/person"
elementFormDefault="qualified">
<element name="person" type="tns:personType"></element>
<complexType name="personType">
<sequence>
<element name="name" type="string" />
<element name="age" type="ageType"></element>
<element name="email" type="emailType"></element>
</sequence>
<attribute name="sex" type="tns:sexType"></attribute>
</complexType>
<simpleType name="email">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"></enumeration>
<enumeration value="女"></enumeration>
</restriction>
</simpleType>
<simpleType name="ageType">
<restriction base="int">
<minInclusive value="1"></minInclusive>
<maxExclusive value="150"></maxExclusive>
</restriction>
</simpleType>
</schema>
3、组合多个schema完成操作
注意: 引用其他的schema, 必须保证两个schema的命名空间一致。否则使用xjc转化的时候会报错。
xjc -d G:\JavaWeb\qyyx_01\src -verbose classroom.xsd
可以讲schema转化为java对象。
<!-- 使用include 引用其他schema -->
<include schemaLocation="student.xsd"></include>