标签:xml约束
xml约束 class.dtd
`<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT root (student+)>
<!ELEMENT student (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
`
arg_entity.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root SYSTEM "class.dtd">
<root>
<student>
<name>张三</name>
<age>30</age>
</student>
<student>
<name>李四</name>
<age>31</age>
</student>
</root>
validte.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>自定义XML的检查器(语法+约束)</title>
</head>
<body>
<script type="text/javascript">
//创建IE内置解析器
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
//开启xml校验
xmldoc.validateOnParse="true";
//加载需要检查的XML文件
xmldoc.load("arg_entity.xml");
//正确
if(xmldoc.parseError.reason.length==0){
window.alert("有效的XML文件(语法正确+DTD约束");
//出错
}else{
window.alert(xmldoc.parseError.reason);
}
</script>
</body>
</html>
写到一个文件中的拘束
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ELEMENT root (student)>
<!ELEMENT student (name,age,alias)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (real|virtual)>
<!ELEMENT alias ANY>
<!ELEMENT real (#PCDATA)>
<!ELEMENT virtual (#PCDATA)>
]>
<root>
<student>
<name>jack</name>
<age>
<virtual>11</virtual>
</age>
<alias/>
</student>
</root>
标签:xml约束
原文地址:http://blog.51cto.com/357712148/2103858