xml..Schema



anyType(任意类型):
  不对元素的内容进行任何,当然也对元素的数据类型也不进行约束,anyType是每个元素的默认类型。
注:一旦某个元素被声明为任意类型,其属性和内容是不受任何约束的。
例:
Schema文档:
  <?xml version="1.0"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="book" type="xs:anyType"/>
  </xs:schema>
XML文档:
  <?xml version="1.0"?>
  <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="009.xsd">123.55</book>
说明:该元素的内容可以是字符型,也可以是数值型。



simpleType(简单类型):一般用来约束元素的值。

complexType(复杂类型):
  复杂元素按内容的复杂程度可以分为两类:simpleContent(简单内容)元素和complexContent(复杂内容)元素

simpleContent(简单内容)元素:
  是指元素内容中仅包含文本,不含子元素的复杂元素。
  判断一个复杂元素是否为simpleContent类型的标准,是看其内容是否只含有文本。
  定义simpleContent最主要的任务就是通过extension(扩展派生)声明属性。
  simpleContent元素它首先是一个复杂类型元素,声明一个元素为复杂类型需要用到关键字complexType。

格式:
  <xs:complexType>    /声明复杂类型
      <xs:simpleContent>    /声明简单内容元素
           <!--在这里放代码-->
       </xs:simpleContent>
  </xs:complexType>


类型派生:
restriction(约束派生):
  是指通过对基类型的值或内容进行限定,从而得到新的类型。
注:自定义简单类型即时为一个很好的约束派生的例子。
例:
XML文件:
  <?xml version="1.0"?>
  <medal>sliver</medal>

Schema文件:
  <?xml version="1.0"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   <xs:element name="medal">  /声明元素奖章
    <xs:simpleType>   /声明简单类型
     <xs:restriction base="xs:string">  /声明新类型是基于字符类型的约束
      <xs:enumeration value="gold"/>  /声明自定义枚举型——金牌
      <xs:enumeration value="sliver"/>    /银牌
      <xs:enumeration value="copper"/>    /铜牌
     </xs:restriction>
    </xs:simpleType>
   </xs:element>
  </xs:schema>
说明:
  在XML文档中,约束了元素的内容只能为只能gold、sliver、copper里其中的一个。

extension(扩展派生):
  是指通过对基类型的值或内容模型进行扩充,从而得到新的类型。
  一般情况下,用来声明属性。

属性声明格式:
  <xs:attribute name="属性名" type="属性类型" use="required | optional | prohibited" />
说明:
  可以在一个元素里声多明个属性,属性里也有默认值和固定值,和元素的设置方法不样,在这里就不多加叙述了。
  在同一个类型定义中不能定义多个同名属性,一个元素可以含有多个属性,多个属性之间对先后顺序并不敏感。
   
use:用来定义属性是否必须出现在文档中。
   required:代表该属性必须出现在XML文档中。
   optional:代表该属性可以出现,也可以不出现XML文档中。
   prohibited:代表该属性不可以出现在XML文档中。

simpleContent(简单内容)元素实例:
XML文件:
  <?xml version="1.0" ?>
  <book id="001" type="IT">XML学习天下</book>

Schema文件:
  <?xml version="1.0"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   <xs:element name="book" >  /声明元素
    <xs:complexType>    /声明复杂类型
        <xs:simpleContent>    /声明简单内容元素
              <xs:extension base="xs:string">
       <xs:attribute name="id" type="xs:integer"/>
       <xs:attribute name="type" type="xs:string"/>
              </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
   </xs:element>
  </xs:schema>

创建一个htm文件判断定义的xml文件是否合法

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
<!--
#showError {
 color: #F00;
 font-size: 16px;
}
-->
</style></head>
<script type="text/javascript">
function validateXML(filename){
 var txt="";
 if (window.ActiveXObject){
  document.getElementById("showError").innerText ="";
  var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
    xmlDoc.async = false;
    xmlDoc.validateOnParse= true;
    xmlDoc.load(filename);

    if(xmlDoc.parseError.errorCode!=0){
      txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
      txt=txt+"Error Reason: " + xmlDoc.parseError.reason ;
      txt=txt+"Error Line: " + xmlDoc.parseError.line;
     }else{
      txt="没有错误";
     }
  document.getElementById("showError").innerText = txt;
 }else{
     alert("此浏览器不支持验证!");
 }
 
}
</script>
<body>
<center>
<h2>通过XSD验证XML文件</h2>
<p>
<input id="xmlfile" type="text" />
<input type="button" name="submit" value="验 证" onClick="validateXML(document.getElementById('xmlfile').value);" />
</p>
</center>
<div id="showError"></div>
</body>
</html>

1创建一个simple_demo.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book" type="bookType" />

 <xs:complexType name="bookType">
  <xs:sequence>           
   <xs:element name="name" type="xs:string" />
   <xs:element name="publisher" type="xs:string" />
   <xs:element name="company" type="xs:string" />
   <xs:element name="author" type="xs:string" />
   <xs:element name="ISBN" type="xs:string" />
   <xs:element name="price" type="xs:string" />
   <xs:element name="url" type="xs:string" />
  </xs:sequence>
 </xs:complexType>

</xs:schema>

或者创建一个复杂类型

<xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
     <xs:element name="publisher" type="xs:string" />

      .........................

    </xs:sequence>
  </xs:complexType>
 </xs:element>

<xs:complexType>复杂类型

创建一个simple_demo.xml

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com simple_demo.xsd">

 <name>《C#从入门到精通(第2版)》</name>
 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author>王小科</author>
 <ISBN>9787302226628</ISBN>
 <price>69.80</price>
 <url><![CDATA[http://www.mingribook.com/bookinfo.php?id=227&sid=4]]></url>

</book>

 2 xsd中使用拓展数据类型

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="publisher" type="xs:string" />
    <xs:element name="company" type="xs:string" />
    <xs:element name="author" type="xs:string" />
    <xs:element name="ISBN" type="xs:string" />
    <xs:element name="price">
     <xs:complexType>
      <xs:complexContent>
       <xs:extension base="discountPrice">
        <xs:sequence>
         <xs:element name="original" type="xs:double" />
        </xs:sequence>
        <xs:attribute name="RMB" type="xs:string" use="required" />
       </xs:extension>
      </xs:complexContent>
     </xs:complexType>
    </xs:element>
    <xs:element name="url" type="xs:string" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:complexType name="discountPrice">
  <xs:sequence>
   <xs:element name="discount" type="xs:double" />
  </xs:sequence>
 </xs:complexType>

</xs:schema>

创建xml文件

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com extension_complexContent_demo.xsd">

 <name>《C#从入门到精通(第2版)》</name>
 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author>王小科</author>
 <ISBN>9787302226628</ISBN>
 <price RMB="yuan">
  <discount>69.00</discount>
  <original>69.80</original>
 </price>
 <url><![CDATA[http://www.mingribook.com/bookinfo.php?id=227&sid=4]]></url>

</book>

3使用元素的条理化

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:group ref="otherType"></xs:group>
    <xs:element name="name" type="xs:string" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:group name="otherType">
  <xs:sequence>
   <xs:element name="publisher" type="xs:string" />
   <xs:element name="company" type="xs:string" />
   <xs:element name="author" type="xs:string" />
   <xs:element name="ISBN" type="xs:string" />
   <xs:element name="price" type="xs:double" />
   <xs:element name="url" type="xs:string" />
  </xs:sequence>
 </xs:group>

</xs:schema>

。。。。。。。。。。。。。。。。。。。。。。。。。。

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com group_demo.xsd">

 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author>王小科</author>
 <ISBN>9787302226628</ISBN>
 <price>69.80</price>
 <url><![CDATA[http://www.mingribook.com/bookinfo.php?id=227&sid=4]]></url>
 <name>《C#从入门到精通(第2版)》</name>

</book>

 

4多属性打包attributeGroup_demo.xsd  

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="publisher" type="xs:string" />
    <xs:element name="company" type="xs:string" />
    <xs:element name="author">
     <xs:complexType>
      <xs:simpleContent>
       <xs:extension base="xs:string">
        <xs:attributeGroup ref="att"></xs:attributeGroup>
       </xs:extension>
      </xs:simpleContent>
     </xs:complexType>
    </xs:element>
    <xs:element name="ISBN" type="xs:string" />
    <xs:element name="price" type="xs:double" />
    <xs:element name="url" type="xs:string" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:attributeGroup name="att">
                <xs:attribute name="tel" type="xs:string" use="required"></xs:attribute>
  <xs:attribute name="qq" type="xs:integer"></xs:attribute>
  <xs:attribute name="msn" type="xs:string"></xs:attribute>
 </xs:attributeGroup>

</xs:schema>

.................................................................

文件   attributeGroup_demo.xml

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com attributeGroup_demo.xsd">

 <name>《C#从入门到精通(第2版)》</name>
 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author msn="wangxiaoke@mingrisoft.com" qq="200958602" tel="0431-84978981">王小科
 </author>
 <ISBN>9787302226628</ISBN>
 <price>2.0</price>
 <url>http://www.mingribook.com/bookinfo.php?id=227</url>
</book>

5对数据取值进行限定  restriction_simpleContent_demo.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="publisher" type="xs:string" />
    <xs:element name="company" type="xs:string" />
    <xs:element name="author" type="xs:string" />
    <xs:element name="ISBN" type="xs:string" />
    <xs:element name="price" type="priceType" />
    <xs:element name="url" type="xs:string" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:complexType name="basePriceType">
  <xs:simpleContent>
   <xs:extension base="xs:double">
    <xs:attribute name="RMB" use="required"></xs:attribute>
   </xs:extension>
  </xs:simpleContent>
 </xs:complexType>


 <xs:complexType name="priceType">
  <xs:simpleContent>
   <xs:restriction base="basePriceType">
    <xs:minExclusive value="0" />
    <xs:maxExclusive value="1000.00" />
   </xs:restriction>
  </xs:simpleContent>
 </xs:complexType>

</xs:schema>

............................................................

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com restriction_simpleContent_demo.xsd">

 <name>《C#从入门到精通(第2版)》</name>
 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author>王小科</author>
 <ISBN>9787302226628</ISBN>
 <price RMB="yuan">69.80</price>
 <url>http://www.mingribook.com/bookinfo.php?id=227</url>
</book>

6xml声明属性

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="publisher" type="xs:string" />
    <xs:element name="company" type="xs:string" />
    <xs:element name="author">
     <xs:complexType>
      <xs:simpleContent>
       <xs:extension base="xs:string">
        

          <xs:attribute name="tel" type="xs:string" use="required"></xs:attribute>

         <xs:attribute name="qq" type="xs:integer"></xs:attribute>

         <xs:attribute name="msn" type="xs:string"></xs:attribute>

         
       </xs:extension>
      </xs:simpleContent>
     </xs:complexType>
    </xs:element>
    <xs:element name="ISBN" type="xs:string" />
    <xs:element name="price" type="xs:double" />
    <xs:element name="url" type="xs:string" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

</xs:schema>

............................................................

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com attribute_demo.xsd">

 <name>《C#从入门到精通(第2版)》</name>
 <publisher>清华大学出版社</publisher>
 <company>明日科技</company>
 <author msn="wangxiaoke@mingrisoft.com" qq="200958602" tel="0431-84978981">王小科
 </author>
 <ISBN>9787302226628</ISBN>
 <price>2.0</price>
 <url>http://www.mingribook.com/bookinfo.php?id=227</url>
</book>

7对字符进行限定

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:token" />
    <xs:element name="publisher" type="xs:NCName" />
    <xs:element name="company" type="xs:QName" />
    <xs:element name="author" type="xs:Name" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

............................................................

<?xml version="1.0" encoding="UTF-8"?>
<bo:book xmlns:bo="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com string_demo.xsd">

 <bo:name>《C#从入门到精通(第2版)》</bo:name>
 <bo:publisher>清华大学出版社</bo:publisher>
 <bo:company>bo:明日科技有限责任公司</bo:company>
 <bo:author>明日科技:王小科</bo:author>

</bo:book>

8对数值进行限定

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.mingrisoft.com" xmlns="http://www.mingrisoft.com"
 elementFormDefault="qualified">

 <xs:element name="book">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="ISBN" type="xs:positiveInteger" />
    <xs:element name="pageNum" type="xs:nonNegativeInteger" />
    <xs:element name="price" type="xs:double" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

....................................................................

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.mingrisoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.mingrisoft.com integer_demo.xsd">

 <ISBN>9787302226628</ISBN>
 <pageNum>650</pageNum>
 <price>69.80</price>

</book>

 

posted @ 2012-11-04 19:14  dahaidao101  阅读(256)  评论(0编辑  收藏  举报