XML & DTD & Schema
2015-03-17 14:49
274 查看
some features of XML:
cross-platform
carry & transport data
label can be defined by user
DTD:
what is dtd:document type declaration
what can be done:define & constrain xml docs
sample:
1)
Schema:
what is xsd:XML schema definition
what is Schema:you can treat it as a strengthened dtd
what can be done:almost everything in xml can be defined with it
sample(from
w3school):
shiporder.xml:
shipboard.xsd version1.0 for the xml doc above:
version 2.0:
version 3.0:
cross-platform
carry & transport data
label can be defined by user
DTD:
what is dtd:document type declaration
what can be done:define & constrain xml docs
sample:
1)
<span style="white-space:pre"> </span><?xml version="1.0" ?> <!DOCTYPE note [ <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>xiery</to> <from>tristan</from> <body>this is a leter</body> </note>
</pre><p></p><p><span style="white-space:pre"> </span><strong>2)</strong><pre name="code" class="html"> <?xml version="1.0" ?> <!DOCTYPE note SYSTEM "nodeDefine.dtd"> <note> <to>xiery</to> <from>tristan</from> <body>this is a leter</body> </note>
Schema:
what is xsd:XML schema definition
what is Schema:you can treat it as a strengthened dtd
what can be done:almost everything in xml can be defined with it
sample(from
w3school):
shiporder.xml:
<?xml version="1.0" encoding="UTF-8"?> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</orderperson>
<shipto> <name>Ola Nordmann</name> <address>Langgt 23</address> <city>4000 Stavanger</city> <country>Norway</country> </shipto>
<item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item>
<item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
shipboard.xsd version1.0 for the xml doc above:
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element>
</xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema>
version 2.0:
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- definition of simple elements --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> <!-- definition of attributes --> <xs:attribute name="orderid" type="xs:string"/> <!-- definition of complex elements --> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType> </xs:element> </xs:schema>
version 3.0:
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/> </xs:simpleType> <xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complexType> <xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name= 4000 "item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complexType> <xs:element name="shiporder" type="shipordertype"/> </xs:schema>
相关文章推荐
- dtd与schema的区别&XML文件解析常用方式
- 【XML】——DTD & Schema
- JavaWeb学习笔记之xml&DTD&schema
- [xml]DTD & Schema 约束
- XML之DTD Schema总结
- 约束:HTML---->XML---->XML DTD---->XML Schema 解析:DOM---->+SAX----->Dom4J
- Eclipse关闭No grammar constraints (DTD or XML schema) detected for the document警告
- Resin启动Webservice客户端,调用Webservice服务报property "javax.xml.stream.supportDTD" not supported错误
- XML验证实践(DTD和Schema)
- XML——>总结之Schema
- XML DTD与Schema约束
- 一个简单的XML&nbsp;Schema的例子
- XML简介之Schema之XSD 复合类型指示器 VS XSD <any> 元素
- XML-Schema的DTD描述
- XML--DTD--Schema--DOM--SAX--DOM4J--XPATH
- webservice初次相识之XML限制文件DTD和Schema
- XML 文件中 "< "提示办法 即DTD 文件的引入
- dom4j通过dtd或者schema验证xml
- No grammar constraints (DTD or XML schema) detected for the document
- Eclipse里的No grammar constraints (DTD or XML schema) detected for the document警告