您好,欢迎访问一九零五行业门户网

xml约束技术之dtd的详解

1.dtd官方教程
##2.xml约束技术:
dtd约束:语法相对简单,功能也相对简单。先出现
schema约束:语法相对复杂,功能也相对强大。采用和xml语法类似的编写方式,schema约束出现就是为了替换dtd约束。
3.dtd简介:
  文档类型定义(dtd)可定义合法的xml文档构建模块。它使用一系列合法的元素来定义文档的结构。dtd 可被成行地声明于 xml 文档中,也可作为一个外部引用。
3.1导入dtd的方式:
1.内部导入:
<code>#导入方式: <!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)> ]> <note> <to>tove</to> <from>jani</from> <heading>reminder</heading> <body>don't forget me this weekend</body> </note> </code>
(第二行)定义此文档是 note(根标签) 类型的文档。<br> (第三行)定义 note 元素有四个元素(标签):"to、from、heading,、body"<br> (第四行)定义 to 元素为 "#pcdata" 类型<br> (第五行)定义 frome 元素为 "#pcdata" 类型<br> (第六行)定义 heading 元素为 "#pcdata" 类型<br> <p>(第七行)定义 body 元素为 "#pcdata" 类型</p> <p>外部导入方式:<br> 本地文件:</p> <pre class="brush:xml;"><code>#导入方式: <!doctype note system "note.dtd"> #note.dtd文件内容: <!element note (to,from,heading,body)> <!element to (#pcdata)> <!element from (#pcdata)> <!element heading (#pcdata)> <!element body (#pcdata)></code></pre> <p> 公共的外部导入:一般项目采用公共外部导入,比如ssh的xml文件基本上就是采用了这种方式</p> <pre class="brush:xml;"><code>#导入方式: <!doctype 根元素 public "http://rlovep.com/peace.dtd"> #如hibernate.cfg.xml: <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"></code></pre> <h3>3.2dtd语法:</h3> <p> 1.约束标签<br> 语法:</p> <pre class="brush:xml;"><code> <!element 元素名称 类别> 或 <!element 元素名称 (元素内容)></code></pre> 类别:<br> 空标签: empty。 表示元素一定是空元素.例如:<bb/>:<!--element bb empty--><br> 普通字符串: (#pcdata)。表示元素的内容一定是普通字符串(不能含有子标签)。例如:<!--element to (#pcdata)--><br> <p>任何内容: any。表示元素的内容可以是任意内容(包括子标签) 例如:<!--element note any--></p>
元素内容:
<code>顺序问题: <!element 元素名称 (子元素名称 1,子元素名称 2,.....)>: 按顺序出现子标签 次数问题: 标签 : 必须且只出现1次。 标签+ : 至少出现1次 标签* : 0或n次。 标签? : 0 或1次。 声明"非.../既..."类型的内容</code>
2.约束属性:
语法:
<code><!attlist 元素名称 属性名称 属性类型 默认值></code>
属性类型:
<code>cdata :表示普通字符串 (en1|en2|..): 表示一定是任选其中的一个值 id:表示在一个xml文档中该属性值必须唯一。值不能以数字开头</code>
默认值:
<code>#required 属性值是必需的 #implied 属性不是必需的 #fixed value 属性不是必须的,但属性值是固定的</code>
3.3测试如下,请细看注释:
<code><?xml version="1.0"?> <!doctype note [ <!element note (to,from+,heading*,body?,(br|b))> <!--带有子序列的元素,需要按照先后顺序出现; to只能出现一次 from最少出现一次 heading次数随意 body出现零次或者一次 非出现br就出现b --> <!--元素约束--> <!element to (#pcdata)><!--pcdata元素--> <!element from any><!--任何内容的元素--> <!element heading (#pcdata)> <!element body (#pcdata)> <!element br empty><!--空元素--> <!element b empty><!--空元素--> <!--属性约束--> <!attlist to number cdata #required><!--必须有属性值出现,且属性值类型为字符串--> <!attlist from length cdata "10"><!--默认属性值,不写出属性时属性值为10--> <!--假如您不希望强制作者包含属性,并且您没有默认值选项的话,请使用关键词 #implied。--> <!attlist heading length cdata #implied> <!attlist body length cdata #fixed "123"><!--属性拥有固定的值,并不允许作者改变这个值--> <!attlist br type (check|cash) "cash"><!--属性值可以为check和cash中的一个--> ]> <note> <to number="1234">tove</to> <from>jani</from> <heading length="10">reminder</heading> <body length="123">don't forget me this weekend</body> <br type="check"/> </note></code>
以上就是xml约束技术之dtd的详解的详细内容。
其它类似信息

推荐信息