schema约束理解

来源:互联网 发布:苹果自动关机软件哪个 编辑:程序博客网 时间:2024/06/05 18:11

schema 的简单认识

简单重点:只要认识        (增加子元素)

   <xs:complexType>

      <xs:sequence>

可以嵌套元素就可以了!



(增加属性)

<xsd:element name="media">
<xsd:complexType>
<xsd:attribute name="mediaid" type="xsd:integer" />
<xsd:attribute name="status" type="mediaType" />



</xsd:complexType>
</xsd:element> 


在<xsd:complexType>           </xsd:complexType> 中间增加


小例子:

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

<xs:element name="note">
<xs:complexType>

<xs:sequence>
<xs:element name="to" >
<xs:complexType>
<xs:attribute name="mm" type="xs:string"/>                (增加元素)
<xs:attribute name="gg" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>                                                                      (父元素就一定要在sequence后,在complexType 结束前)
<xs:attribute name="mediaid" type="xs:integer" />
<xs:attribute name="status" type="xs:string" />
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>



简单例子:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"     <!--规定的命名空间-->

targetNamespace="http://www.w3school.com.cn"                              <!--自定义的命名空间,自己挂上去-->

xmlns="http://www.w3school.com.cn"

elementFormDefault="qualified">

 

<xs:element name="note">

    <xs:complexType>

      <xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="heading" type="xs:string"/>

<xs:element name="body" type="xs:string"/>

      </xs:sequence>

    </xs:complexType>

</xs:element>

 

</xs:schema>





<?xml version="1.0"?>

<note

xmlns="http://www.w3school.com.cn"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.w3school.com.cn note.xsd">            <!-自定义的命名空间 ,文档-->

 

<to>George</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don't forget the meeting!</body>

</note>





还有不理解的:  (看官方文档)

http://www.ibm.com/developerworks/cn/education/xml/x-valid/section5.html