XMLSchema.xsd 文件约束xml文档格式内容

来源:互联网 发布:win10优化驱动器第8遍 编辑:程序博客网 时间:2024/05/16 10:14
XMLSchema.xsd 文件,用来约束xml内容格式的。
<?xml version="1.0" encoding="utf-8"?><xs:schema id="XMLSchemaLettle"    targetNamespace="http://www.pukuimin.com/XMLSchemaLettle.xsd"    elementFormDefault="qualified"    xmlns="http://www.pukuimin.com/XMLSchemaLettle.xsd"    xmlns:mstns="http://www.pukuimin.com/XMLSchemaLettle.xsd"    xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xs:element name="note">    <xs:complexType>      <xs:sequence>        <xs:element name="heading" type="xs:string" minOccurs="0" maxOccurs="1" default="headstring">        </xs:element>        <xs:element name="body" type="xs:string" minOccurs="0" maxOccurs="1" default="content"/>        <xs:element name="aletter" minOccurs="0" maxOccurs="1" default="a">          <!--只能输入一个字符,且是大小写字母中的一个-->          <xs:simpleType>            <xs:restriction base="xs:string">              <xs:pattern value="[a-zA-Z]"/>            </xs:restriction>          </xs:simpleType>        </xs:element>        <xs:element name="age" minOccurs="0" maxOccurs="1">          <!--只能指定一个范围内的值-->          <xs:simpleType>            <xs:restriction base="xs:integer">              <xs:minInclusive value="1"/>              <xs:maxInclusive value="120"/>            </xs:restriction>          </xs:simpleType>        </xs:element>        <xs:element name="gender" minOccurs="0" maxOccurs="1" default="male">          <!--正则表达示限制输入规则,这样能验证,但不自动提示-->          <xs:simpleType>            <xs:restriction base="xs:string">              <xs:whiteSpace value="replace"/>              <!--移除所有空白字符(换行、回车、空格以及制表符)-->              <xs:pattern value="male|female"/>              <xs:minLength value="4"/>              <!--最小字符数-->              <xs:maxLength value="6"/>              <!--最大字符数-->            </xs:restriction>          </xs:simpleType>        </xs:element>        <xs:element name="password" minOccurs="0" maxOccurs="1" default="12345678">          <xs:simpleType>            <xs:restriction base="xs:string">              <xs:length value="8"/>              <!--固定长度-->            </xs:restriction>          </xs:simpleType>        </xs:element>        <xs:element name="from" type="xs:string" minOccurs="0" maxOccurs="1" default="pukuimin"/>        <xs:element name="to" minOccurs="0" maxOccurs="unbounded">          <xs:complexType>            <xs:sequence minOccurs="1" maxOccurs="1">              <!--一个元素下的子元素-->              <xs:element name="full_name" minOccurs="1" maxOccurs="1">                <!--只能指定一个固定的值,vs里按Ctrl+J 就能显示可以指定的值-->                <xs:simpleType>                  <xs:restriction base="xs:string">                    <xs:enumeration value="pu"/>                    <xs:enumeration value="shu"/>                    <xs:enumeration value="liu"/>                  </xs:restriction>                </xs:simpleType>              </xs:element>              <xs:element name="child_name" type="xs:string" minOccurs="1" maxOccurs="1"/>            </xs:sequence>            <xs:anyAttribute />            <!--允许指定自定义属性-->          </xs:complexType>        </xs:element>        <xs:element name="to2">          <xs:complexType>            <xs:attributeGroup ref="persongroup"/>          </xs:complexType>        </xs:element>      </xs:sequence>    </xs:complexType>  </xs:element>  <xs:attributeGroup name="persongroup">    <!--一个元素中的很多属性-->    <xs:attribute name="firstname" default="pu">      <xs:simpleType>        <xs:restriction base="xs:string">          <xs:enumeration value="pu"/>          <xs:enumeration value="shu"/>          <xs:enumeration value="liu"/>        </xs:restriction>      </xs:simpleType>    </xs:attribute>    <xs:attribute name="lastname" type="xs:string" default="km"/>    <xs:attribute name="birthday" type="xs:date" default="2012-08-27"/>  </xs:attributeGroup></xs:schema>


在vs2010中:

 

生成的示例xml文件内容:

<?xml version="1.0" encoding="utf-8"?><note xmlns="http://www.pukuimin.com/XMLSchemaLettle.xsd">  <heading>headstring</heading>  <body>content</body>  <aletter>a</aletter>  <age>1</age>  <gender>male</gender>  <password>12345678</password>  <from>pukuimin</from>  <to>    <!-- Attribute Wild card could not be matched. Generated XML may not be valid. -->    <full_name>pu</full_name>    <child_name>child_name1</child_name>  </to>  <to>    <!-- Attribute Wild card could not be matched. Generated XML may not be valid. -->    <full_name>shu</full_name>    <child_name>child_name2</child_name>  </to>  <to>    <!-- Attribute Wild card could not be matched. Generated XML may not be valid. -->    <full_name>liu</full_name>    <child_name>child_name3</child_name>  </to>  <to2 firstname="pu" lastname="km" birthday="2012-08-27" /></note>


如果不符合约束要求,文档就会 显示警告。

 

 

原创粉丝点击