Demo of XML Validation

@draft

@在专门的XML编辑工具下试试看, 比如XML Spy

对于以下XML文本:

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

用DTD验证的话, 需要把文本改写成如下(添加了第二句)

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE note SYSTEM "Note.dtd">

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

DTD 文件文本如下:

<!DOCTYPE note

[

<!ELEMENT note (to,from,heading,body)>

<!ELEMENT to (#PCDATA)>

<!ELEMENT from (#PCDATA)>

<!ELEMENT heading (#PCDATA)>

<!ELEMENT body (#PCDATA)>

]>

(在用MyEclipse自动生成DTD时候, 第一行是: <?xml version="1.0" encoding="UTF-8"?>, 奇怪.)

在MyEclipse中, XML和DTD的验证关系没有提现出来, XML文本明明不合法, 也没什么报错信息. 换XML Spy等软件试试看.

如果用XML Schema, 那么校验文本是:

<xs:element name="note">

<xs:complexType>

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="heading" type="xs:string"/>

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

</xs:sequence>

</xs:complexType>

</xs:element>

但是, 对应的被校验的XML文本, 应该添加什么语句还暂时不明(TODO)

以上校验的页面当有和DTD专题, XML Schema专题的链接.

注意, 在http://www.w3schools.com/xml/xml_cdata.asp的DTD例子中, 居然还包含逻辑运算.