<?xml version="1.0" encoding="gb2312"?>
<studentlist>
  <student id="A101">
    <name>李华</name>
    <sex>男</sex>
    <birthday>1978.9.12</birthday>
    <score>92</score>
    <skill>Java</skill>
    <skill>Oracle</skill>
    <skill>C Sharp</skill>
    <skill>SQL Server</skill>
  </student>
<studentlist>

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="原子类型" targetNamespace="http://student.com" elementFormDefault="qualified"
    xmlns="http://student.com" xmlns:mstns="http://student.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="student">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="name" type="nameType"/>  
          <xs:element ref="age"/>
          <xs:element ref="sex"/>
          <xs:element ref="phone"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    
    <xs:simpleType name="nameType">
      <xs:restriction base="xs:string">
        <xs:minLength value="4"/>
        <xs:maxLength value="8"/>
      </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="age">
      <xs:simpleType>
        <xs:restriction base="xs:int">
          <xs:minInclusive value="1"/>
          <xs:maxInclusive value="100"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    
      <xs:element name="sex">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="男"/>
          <xs:enumeration value="女"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    
    <xs:element name="phone">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="\d{3}-\d{8}"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
</xs:schema>

MSDN上面一个例子:

  <!-- booksSchema.xml  -->     
    
  <?xml   version='1.0'?>   
  <!--   This   file   represents   a   fragment   of   a   book   store   inventory   database   -->   
  <bookstore   xmlns   =   "schema.xsd">   
      <book   genre="autobiography"   publicationdate="1981"   ISBN="1-861003-11-0">   
          <title>The   Autobiography   of   Benjamin   Franklin</title>   
          <author>   
              <first-name>Benjamin</first-name>   
              <last-name>Franklin</last-name>   
          </author>   
          <price>8.99</price>   
      </book>   
      <book   genre="novel"   publicationdate="1967"   ISBN="0-201-63361-2">   
          <title>The   Confidence   Man</title>   
          <author>   
              <first-name>Herman</first-name>   
              <last-name>Melville</last-name>   
          </author>   
          <price>11.99</price>   
      </book>   
      <book   genre="philosophy"   publicationdate="1991"   ISBN="1-861001-57-6">   
          <title>The   Gorgias</title>   
          <author>   
              <first-name>Sidas</first-name>   
              <last-name>Plato</last-name>   
          </author>   
          <price>9.99</price>   
      </book>   
  </bookstore>   
   <!-- schema.xsd -->  
    
  <xsd:schema   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
          xmlns="schema.xsd"   
          elementFormDefault="qualified"   
          targetNamespace="schema.xsd">   
    
    <xsd:element   name="bookstore"   type="bookstoreType"/>   
    
    <xsd:complexType   name="bookstoreType">   
      <xsd:sequence   maxOccurs="unbounded">   
        <xsd:element   name="book"     type="bookType"/>   
      </xsd:sequence>   
    </xsd:complexType>   
    
    <xsd:complexType   name="bookType">   
      <xsd:sequence>   
        <xsd:element   name="title"   type="xsd:string"/>   
        <xsd:element   name="author"   type="authorName"/>   
        <xsd:element   name="price"     type="xsd:decimal"/>   
      </xsd:sequence>   
      <xsd:attribute   name="genre"   type="xsd:string"/>   
      <xsd:attribute   name="publicationdate"   type="xsd:string"/>   
      <xsd:attribute   name="ISBN"   type="xsd:string"/>   
    </xsd:complexType>   
    
    <xsd:complexType   name="authorName">   
      <xsd:sequence>   
        <xsd:element   name="first-name"     type="xsd:string"/>   
        <xsd:element   name="last-name"   type="xsd:string"/>   
      </xsd:sequence>   
    </xsd:complexType>   
    
  </xsd:schema>   
  <!-- bookSchemaFail.xml   -->
    
  <?xml   version='1.0'?>   
  <bookstore   xmlns="schema.xsd">   
      <book>   
          <author>   
              <first-name>Benjamin</first-name>   
              <last-name>Franklin</last-name>   
          </author>   
      </book>   
      <book   genre="novel">   
          <title>The   Confidence   Man</title>   
          <author>   
              <first-name>Herman</first-name>   
              <last-name>Melville</last-name>   
          </author>   
          <price>11.99</price>   
      </book>   
      <book   genre="philosophy">   
          <title>The   Gorgias</title>   
          <author>   
              <name>Plato</name>   
          </author>   
          <price>9.99</price>   
      </book>   
  </bookstore> 
    using   System;   
  using   System.Xml;   
  using   System.Xml.Schema;   
  using   System.IO;   
    
  namespace   SchemaData   
  {   
  ///   <summary>   
  ///   Validator   的摘要说明。   
  ///   </summary>   
  public   class   Validator   
  {   
    
  private   const   String   document3   =   "booksSchema.xml";   
  private   const   String   document4   =   "booksSchemaFail.xml";   
  private   const   String   document5   =   "schema.xsd";   
    
  private   XmlValidatingReader   myXmlValidatingReader   =   null;   
  private   XmlTextReader   myXmlTextReader   =   null;   
  private   Boolean   Success   =   true;   
  private   String[]   args   =   {document3,   document4,   document5};   
    
  public   Validator()   
  {   
  //   
  //   TODO:   在此处添加构造函数逻辑   
  //       
  }       
    
  public   void   Run()   
  {   
  try   
  {   
    
  XmlSchemaCollection   myXmlSchemaCollection   =   new   XmlSchemaCollection();   
  myXmlSchemaCollection.Add("schema.xsd"   ,   new   XmlTextReader(args[2]));   
    
  //   用架构验证   XML   文件   
  Success   =   true;   
  Console.WriteLine();   
  Console.WriteLine("正在用架构文件   schema.xsd   验证   XML   文件   booksSchema.xml   ...");   
  myXmlTextReader   =   new   XmlTextReader   (args[0]);   
  myXmlValidatingReader   =   new   XmlValidatingReader(myXmlTextReader);   
  myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);   
  myXmlValidatingReader.ValidationType   =   ValidationType.Schema;   
  Validate();   
    
  //   架构验证失败   
  Success   =   true;   
  Console.WriteLine();   
  Console.WriteLine("正在用架构文件   schema.xsd   验证   XML   文件   booksSchemaFail.xml   ...");   
  myXmlTextReader   =   new   XmlTextReader   (args[1]);   
  myXmlValidatingReader   =   new   XmlValidatingReader(myXmlTextReader);   
  myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);   
  myXmlValidatingReader.ValidationType   =   ValidationType.Schema;   
  Validate();   
  }   
    
  catch   (Exception   e)   
  {   
  Console.WriteLine("异常:"   +   e.ToString());   
  }   
    
  finally   
  {   
  //   通过   XmlTextReader   完成   
  if   (myXmlValidatingReader   !=   null)   
  myXmlValidatingReader.Close();   
  }   
  }   
    
  private   void   Validate()   
  {   
  try   
  {   
  //   设置验证事件处理程序   
  myXmlValidatingReader.ValidationEventHandler   +=   new   ValidationEventHandler   (this.ValidationEventHandle);   
    
  //   读取   XML   数据   
  while   (myXmlValidatingReader.Read()){}   
  Console.WriteLine   ("验证已完成。验证   {0}",   (Success==true   ?   "成功"   :   "失败"));   
  }   
  catch   (XmlException   e)   
  {   
  Console.WriteLine   ("Xml   异常:"   +   e.ToString());   
  }   
    
  catch   (Exception   e)   
  {   
  Console.WriteLine   ("异常:"   +   e.ToString());   
  }   
  }   
    
  public   void   ValidationEventHandle   (object   sender,   ValidationEventArgs   args)   
  {   
  Success   =   false;   
    
  Console.WriteLine("\t验证错误:"   +   args.Message);   
    
  if   (args.Severity   ==   XmlSeverityType.Warning)   
  {   
  Console.WriteLine("未找到要强制验证的架构。");   
  }     
  else   
  if   (args.Severity   ==   XmlSeverityType.Error)   
  {   
  Console.WriteLine("验证实例文档时发生验证错误。");   
  }     
    
  if   (args.Exception   !=   null)   //   XSD   架构验证错误   
  {   
  Console.WriteLine(args.Exception.SourceUri   +   ","   +     args.Exception.LinePosition   +   ","   +     args.Exception.LineNumber);   
  }   
    
  //if   (myXmlValidatingReader.Reader.LineNumber   >   0)   
  //{   
  //         Console.WriteLine("Line:   "+   myXmlValidatingReader.Reader.LineNumber   +   "   Position:   "   +   myXmlValidatingReader.Reader.LinePosition);   
  //}   
  }   
    
  }   
  }

XSD文件详解(二)的更多相关文章

  1. jni.h头文件详解二

    作者:左少华 博客:http://blog.csdn.net/shaohuazuo/article/details/42932813 转载请注明出处:http://blog.csdn.net/shao ...

  2. 【转】 jni.h头文件详解(二)

    原文网址:http://blog.csdn.net/shaohuazuo/article/details/42932813 作者:左少华 博客:http://blog.csdn.net/shaohua ...

  3. XSD文件详解

    XSD (xml Schema Definition) Xml Schema的用途 1.  定义一个Xml文档中都有什么元素 2.  定义一个Xml文档中都会有什么属性 3.  定义某个节点的都有什么 ...

  4. PE文件详解二

    本文转自小甲鱼的PE文件相关教程,原文传送门 咱接着往下讲解IMAGE_OPTIONAL_HEADER32 结构定义即各个属性的作用! 接着我们来谈谈 IMAGE_OPTIONAL_HEADER 结构 ...

  5. MyBatis之Mapper XML 文件详解(二)-sql和入参

    sql 这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中.它可以被静态地(在加载参数) 参数化. 不同的属性值通过包含的实例变化. 比如: <sql id="use ...

  6. [转]文件IO详解(二)---文件描述符(fd)和inode号的关系

    原文:https://www.cnblogs.com/frank-yxs/p/5925563.html 文件IO详解(二)---文件描述符(fd)和inode号的关系 ---------------- ...

  7. Maven pom.xml文件详解

    Maven pom.xml文件详解 一.简介 POM全称是Project Object Model,即项目对象模型. pom.xml是maven的项目描述文件,它类似与antx的project.xml ...

  8. Mybatis SQL映射文件详解

    Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...

  9. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

随机推荐

  1. [置顶] kubernetes资源类型--持久化存储Persistent Volume和Persistent Volume Claim

    概念 存储管理跟计算管理是两个不同的问题.理解每个存储系统是一件复杂的事情,特别是对于普通用户来说,有时并不需要关心各种存储实现,只希望能够安全可靠地存储数据. 为了简化对存储调度,K8S对存储的供应 ...

  2. ol 接入百度地图

    ol5 如何接入百度地图,网上的资料很多,但是大多都有问题,在级别放大时,地图发生扭曲.为此注重研究了下ol5 接入百度地图的方法. 首先明确以下问题: 百度地图的投影是3857. 百度地图的分辨率和 ...

  3. ES的关键端口

    ElasticSearch的集群可自发现,只要配置相同的集群名称,默认为组播发现机制,默认情况下: http 端口:9200 需要打开给调用 数据传输端口:9300 用于集群之间交换数据 组播端口(U ...

  4. Java之JDBC学习

    (一),MySql数据库 1,MySql数据库的数据类型定义 2,完整性约束: 3,索引: 作用:唯一作用就是加快对表查询速度,索引通过快速路径方法访问来快速定位数据,从而减少磁盘的II/O; 缺点: ...

  5. Asp.net Mvc使用PagedList分页

    git:https://github.com/troygoode/PagedList 1. Nuget 安装package watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...

  6. TCP/IP详解 卷一(第十三章 IGMP:Internet组管理协议)

    本章将介绍用于支持主机和路由器进行多播的Internet组管理协议(IGMP) 它让一个物理网络上的所有系统知道主机当前所在的多播组.多播路由器需要这些信息以便知道多播数据报应该向那些接口转发. 跟I ...

  7. 由需求而产生的一款db导出excel的工具

    代码地址如下:http://www.demodashi.com/demo/12062.html 程序员最大的毛病可能就是懒,因为懒所以做出了许许多多提高自己工作效率的工具. 起因于我是商业开发,既然是 ...

  8. 深入理解dp px density

    1 http://blog.csdn.net/lcaihy1314/article/details/8446401 2 待续

  9. 转python调用Go代码

    Go 1.5发布了,其中包含了一个特性:可以编译生成动态链接库,经试验,生成的.so文件可以被python加载并调用.下面举个例子: 先写一个go文件main.go: package main imp ...

  10. rplidar 扫描角度设置

    参考网站::   https://blog.csdn.net/sunyoop/article/details/78302090 https://blog.csdn.net/dzhongjie/arti ...