dtd
<!-- <!ELEMENT 元素(子元素,...)> --> <!ELEMENT students (student+,cat*) > <!ELEMENT student (name,age,sex,birth,phone*,email?,student?) ><!-- 注意元素的顺序 --> <!ELEMENT email EMPTY><!-- 元素里面不能包含内容 --> <!ELEMENT phone (#PCDATA)> <!ATTLIST sex value (男|女|保密) "男"><!-- 属性值可以使用枚举类型 --> <!-- 定义student元素的stuNo属性 ID类型: 保证唯一性(必须由数字和字母组成) CDATA类型: 指定是字符串类型 #REQUIRED 属性值是必须 #IMPLIED 属性值是可选 --> <!ATTLIST student stuNO ID #IMPLIED> <!ATTLIST email value CDATA #IMPLIED>
xml
<?xml version="1.0" encoding="UTF-8" ?><!-- xml文件的声明 --> <!DOCTYPE students SYSTEM "student.dtd"><!-- 引用外部的dtd校验文件 --> <students> <student stuNO="STU0001"> <name>关羽</name> <age>20</age> <sex>男</sex> <birth>1988-10-29</birth> <email value="guanyu@qq.com" /> <student stuNO="SUT11001"> <name>关羽</name> <age>20</age> <sex value="女">男</sex> <birth>1988-10-29</birth> <email value="guanyu@qq.com" /> </student> </student> <student stuNO="STU0002"> <name>李四</name> <age>20</age> <sex>男</sex> <birth>1988-10-29</birth> <phone>123332</phone> <email value="guanyu@qq.com" ></email> </student> <student stuNO="STU0003"> <name>貂蝉</name> <age>20</age> <sex>女</sex> <birth>1988-10-29</birth> <phone>123332</phone> <phone>123332</phone> <!-- <email value="guanyu@qq.com" >fdfj</email> --> </student> <cat> <sex>母</sex> <age>3</age> <name>喵喵</name> </cat> <cat> <sex>母</sex> <age>3</age> <name>喵喵</name> </cat> </students>
schema
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="abc" xmlns="abc" elementFormDefault="qualified"> <!-- XML Schema 缩写 当elementFormDefault="qualified" 时,所有全局元素的子元素将被以缺省方式放到目标命名空间,连同全局元素或者类型一起; 而当elementFormDefault="unqualified" 时,所有全局元素的子元素将被以缺省方式放到无名命名空间。而属性的命名空间类似地由attributeFormDefault="?"来指明。 需要明白的是,elementFormDefault="?" 是有作用域的,并且是被继承的,除非在子定义中覆盖父定义。 --> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to"> <xsd:complexType mixed="true"><!-- 混合 --> <xsd:sequence minOccurs="1" maxOccurs="unbounded"> <xsd:element name="content" type="xsd:string" /> </xsd:sequence> <xsd:attribute name="two" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> <xsd:element name="from" type="xsd:string" /> <xsd:element name="heading" type="xsd:string" /> <xsd:element name="body" type="xsd:string" /> <xsd:element name="age" type="xsd:integer" /> <xsd:element name="birth" type="xsd:date" /> <xsd:element name="sex"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="男" /> <xsd:enumeration value="女" /> <xsd:enumeration value="人妖" /> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="weight" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="one" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:schema>
xml
<?xml version="1.0" encoding="UTF-8" ?> <note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="abc" xsi:schemaLocation="abc note.xsd" one="属于note"> <to two="to的属性"> <content>fdsf</content> <content>fdsf</content> <content>fsdf</content> </to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> <age>20</age> <birth>1988-10-23</birth> <sex>人妖</sex> <weight>75</weight> </note>