xml-schema仅含有文本及案例

<!--Schema-仅含有文本-->

案例

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 

targetNamespace="http://www.example.org/text" 

xmlns:tns="http://www.example.org/text" 

elementFormDefault="qualified">

<!-- xmlschema仅含有文本 -->

<xs:element name="books">

<xs:complexType>

<xs:sequence>

<xs:element name="book">

<xs:complexType>

<xs:sequence>

<xs:element name="name">

<!-- 简单的类型即可 -->

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:pattern value="[a-z]{9}"></xs:pattern>

</xs:restriction>

</xs:simpleType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

被约束文件

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/text text.xsd">

<book>

<name>aaaaaaaaa</name>

</book>

</books>

案例2

<!--混合类型-无序->

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 

targetNamespace="http://www.example.org/text" 

xmlns:tns="http://www.example.org/text" 

elementFormDefault="qualified">

<!-- xmlschema仅含有文本 -->

<xs:element name="books">

<xs:complexType>

<xs:sequence>

<xs:element name="book">

<!-- mixed=true代表是混合类型的-->

<xs:complexType mixed="true">

<!--代表无序-->

<xs:all>

<xs:element name="name" maxOccurs="1">

<!-- 简单的类型即可 -->

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:pattern value="[a-z]{9}"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name="price" type="xs:double"/>

</xs:all>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

无序

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/text text1.xsd">

<book>

<!-- all代表的是无序的 -->

<price>12.9</price>

<name>xxxxxxxxx</name>

</book>

</books>

案例3

<!--互斥-->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 

targetNamespace="http://www.example.org/text" 

xmlns:tns="http://www.example.org/text" 

elementFormDefault="qualified">

<!-- xmlschema仅含有文本 -->

<xs:element name="books">

<xs:complexType>

<xs:sequence>

<xs:element name="book">

<!-- mixed=true代表是混合类型的-->

<xs:complexType mixed="true">

<!-- choice代表的是互斥的 -->

<xs:choice>

<xs:element name="name" maxOccurs="1">

<!-- 简单的类型即可 -->

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:pattern value="[a-z]{9}"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name="price" type="xs:double"/>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

被约束文件

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/text text3.xsd">

<book>

<!-- choice代表的是互斥的 -->

<price>12.9</price>

</book>

</books>

案例4

<!--schema--->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 

targetNamespace="http://www.example.org/text" 

xmlns:tns="http://www.example.org/text" 

elementFormDefault="qualified">

<!-- xmlschema仅含有文本 -->

<xs:element name="books">

<xs:complexType>

<xs:sequence>

<xs:element name="book">

<!-- mixed=true代表是混合类型的-->

<xs:complexType mixed="true">

<!-- sequence代表的是有序的 -->

<xs:sequence>

<xs:element name="name" maxOccurs="5">

<!-- 简单的类型即可 -->

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:pattern value="[a-z]{9}"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name="price" type="xs:double" minOccurs="2" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

被约束文件

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/text text4.xsd">

<book>

<!-- sequence代表的是有序的 -->

<name>xxxxxxxxx</name>

<name>xxxxxxxxx</name>

<name>xxxxxxxxx</name>

<name>xxxxxxxxx</name>

<name>xxxxxxxxx</name>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

<price>12.9</price>

</book>

</books>

案例5

<!--定义组-有序单个->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 

targetNamespace="http://www.example.org/text" 

xmlns:tns="http://www.example.org/text" 

xmlns="http://www.example.org/text" 

elementFormDefault="qualified">

<!-- 定义组 -->

<xs:group name="bookGroup">

  <xs:sequence>

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

    <xs:element name="price" type="xs:double"/>

    <xs:element name="cdata" type="xs:date"/>

  </xs:sequence>

</xs:group>

<xs:element name="books">

<xs:complexType>

<xs:sequence>

<xs:element name="book">

<xs:complexType>

<xs:sequence>

<!-- 引用组 -->

<xs:group ref="bookGroup"/>

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

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

被约束文件

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.example.org/text text5.xsd">

<book>

<!-- group代表的是有序的 -->

<name>xxxxxxxxx</name>

<price>12.9</price>

<cdata>2010-10-10</cdata>

<author>chenhj</author>

</book>

</books>

案例6

<!--定义组-无序、多个->

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.example.org/text" xmlns:tns="http://www.example.org/text"

xmlns="http://www.example.org/text" elementFormDefault="qualified">

<!-- 定义组 -->

<xs:group name="bookGroup">

<xs:choice>

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

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

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

</xs:choice>

</xs:group>

<xs:element name="books">

<xs:complexType>

<xs:all>

<xs:element name="book">

<xs:complexType>

<xs:sequence>

<!-- 引用组 -->

<xs:group ref="bookGroup" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:all>

</xs:complexType>

</xs:element>

</xs:schema>

被约束文件

<?xml version="1.0" encoding="UTF-8"?>

<books xmlns="http://www.example.org/text" 

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

xsi:schemaLocation="http://www.example.org/text text6.xsd">

<book>

<!-- group代表的是有序的 -->

<!-- 无序,多次 -->

<name>xxxxxxxxx</name>

<cdata>2</cdata>

<price>2</price>

<price>2</price>

<price>2</price>

<price>2</price>

<price>2</price>

<price>2</price>

<price>2</price>

</book>

</books>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值