一、引用实体
语法格式(dtd中定义):
<!ENTITY 实体名称 “实体内容” >:直接转变成实体内容
引用方式(xml中引用)
&实体名称;
如:
1. 在dtd中定义<!ELEMENT NET (视频网站,社交网站)>
<!--定义一个引用实体,建议大家把引用实体的定义放在前面-->
<!ENTITY intro "介绍的信息”>
2. 在xml文件中引用
<优酷网> &intro;好的网站<优酷网>
二、参数实体
语法格式:
<!ENTITY % 实体名称 "实体内容" >
引用方式:
%实体名称;
如:
<!--定义一个参数实体-->
<!ENTITY % sNET "类型名字">
<!ELEMENT NET (%sNET;,视频网站,社交网站)>
<!ELEMENT %sNET; (#PCDATA)>
以下是案例:
products.dtd文件:
<!ENTITY AUTHOR "John Doe"> <!ENTITY COMPANY "JD Power Tools,Inc."> <!ENTITY EMAIL "jd@jd-tools.com"> <!ELEMENT CATALOG (PRODUCT+)> <!ELEMENT PRODUCT (SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)> <!ATTLIST PRODUCT NAME CDATA #IMPLIED CATEGORY (HandTool|Table|Shop-Professional) "HandTool" PARTNUM CDATA #IMPLIED PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago" INVENTORY (InStock|Backordered|Discontinued) "InStock"> <!ELEMENT SPECIFICATIONS (#PCDATA)> <!ATTLIST SPECIFICATIONS WEIGHT CDATA #IMPLIED POWER CDATA #IMPLIED> <!ELEMENT OPTIONS (#PCDATA)> <!ATTLIST OPTIONS FINISH (Metal|Polished|Matte) "Matte" ADAPTER (Included|Optional|NotApplicable) "Included" CASE (HardShell|Soft|NotApplicable) "HardShell"> <!ELEMENT PRICE (#PCDATA)> <!ATTLIST PRICE MSRP CDATA #IMPLIED WHOLESALE CDATA #IMPLIED STREET CDATA #IMPLIED SHIPPING CDATA #IMPLIED> <!ELEMENT NOTES (#PCDATA)>
products.xml文件:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE CATALOG SYSTEM "products.dtd"> <CATALOG> <PRODUCT NAME="电脑" CATEGORY="HandTool" PLANT="Chicago" INVENTORY="Discontinued"> <SPECIFICATIONS WEIGHT="80kg" POWER="1000w">THINKPAD</SPECIFICATIONS> <PRICE>900</PRICE> <NOTES>轻拿轻放</NOTES> </PRODUCT> </CATALOG>
checkProductDtd.html文件:
<html> <head> <title>test dtd</title> <Meta http-equiv="content-type" content="text/html;charset=utf-8"/> <script language="javascript"> <!-- //创建一个xml解析器 var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); //开启校验功能 xmldoc.validateOnParse =true; //指定对哪个 xml文件校验 xmldoc.load("products.xml"); //如果有错误信息,则输出 document.write("错误信息="+xmldoc.parseError.reason+"<br/>"); document.write("错误的行号="+xmldoc.parseError.line+"<br/>"); //--> </script> </head> </html>