DTD练习和schema练习

(1)dtd的配置
<?xml version="1.0" encoding="utf-8"?>

        <!ELEMENT NEWSPAPER (ARTICLE+)>
        <!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
        <!ELEMENT HEADLINE (#PCDATA)>
        <!ELEMENT BYLINE (#PCDATA)>
        <!ELEMENT LEAD (#PCDATA)>
        <!ELEMENT BODY (#PCDATA)>
        <!ELEMENT NOTES (#PCDATA)>
        <!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>
        <!ATTLIST ARTICLE EDITOR CDATA #IMPLIED>
        <!ATTLIST ARTICLE DATE CDATA #IMPLIED>
        <!ATTLIST ARTICLE EDITION CDATA #IMPLIED>
------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE NEWSPAPER SYSTEM "A.dtd">
<NEWSPAPER>
    <ARTICLE AUTHOR="aaa"  EDITOR="bbb"  DATE="2019-1-1" EDITION="ccc">
        <HEADLINE>aa</HEADLINE>
        <BYLINE>bb</BYLINE>
        <LEAD>cc</LEAD>
        <BODY>dd</BODY>
        <NOTES>ee</NOTES>
    </ARTICLE>
</NEWSPAPER>
==========================================================================
(2)schema配置1
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema
        targetNamespace="http://www.cskaoyan.com"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified">
    <xs:element name="shiporder">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="orderperson" type="xs:string"/>
                <xs:element name="shipto">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="address" type="xs:string"/>
                            <xs:element name="city" type="xs:string"/>
                            <xs:element name="country" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="item" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="note" type="xs:string" minOccurs="0"/>
                            <xs:element name="quantity" type="xs:positiveInteger"/>
                            <xs:element name="price" type="xs:decimal"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="orderid" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>
------------------------------------------------------------------------------
使用默认的名字空间
<?xml version="1.0" encoding="UTF-8" ?>
<shiporder xmlns="http://www.cskaoyan.com"//约束文件的总的地址
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/*这是一个官方的名字空间,必须要加上*/
           xsi:schemaLocation="http://www.cskaoyan.com E.xsd"/*总的地址下面约束文件的地址,注意,如果地址是http://www.cskaoyan.com E.xsd,那么要写成 xsi:schemaLocation="http://www.cskaoyan.com http://www.cskaoyan.com E.xsd"(没写多)*/
           orderid="01">
    <orderperson>sss</orderperson>
    <shipto>
        <name>lzh</name>
        <address>wd</address>
        <city>wuhan</city>
        <country>china</country>
    </shipto>
    <item>
        <title>lzh</title>
        <note>lzh</note>
        <quantity>123</quantity>
        <price>111.11</price>
    </item>
</shiporder>
使用名字空间p:
<?xml version="1.0" encoding="UTF-8" ?>
<p:shiporder
        xmlns:p="http://www.cskaoyan.com"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.cskaoyan.com E.xsd"
           orderid="01">
    <p:orderperson>sss</p:orderperson>
    <p:shipto>
        <p:name>lzh</p:name>
        <p:address>wd</p:address>
        <p:city>wuhan</p:city>
        <p:country>china</p:country>
    </p:shipto>
    <p:item>
        <p:title>lzh</p:title>
        <p:note>lzh</p:note>
        <p:quantity>123</p:quantity>
        <p:price>111.11</p:price>
    </p:item>
</p:shiporder>
==============================================================================
(3)schema配置2
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3schools.com"
           xmlns="http://www.w3schools.com"
           elementFormDefault="qualified">
    <xs:element name="school"  >
        <xs:complexType>
            <xs:sequence>
                <xs:element name="students"    >
                    <xs:complexType>
                        <xs:sequence minOccurs="1" maxOccurs="10"><!--表明元素可以按任意属性出现,次数-->
                            <xs:element name="student" >
                                <xs:complexType>
                                    <xs:sequence  >
                                        <xs:element name="id" type="xs:integer" />
                                        <xs:element name="name" type="xs:string" />
                                        <xs:element name="age"   >
                                            <xs:simpleType><!--定义年龄,只能为20-30的整数-->
                                                <xs:restriction base="xs:integer" >
                                                    <xs:minInclusive value="20" />
                                                    <xs:maxInclusive value="30" />
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="sex"  >
                                            <xs:simpleType ><!--定义性别,只能为男或者女-->
                                                <xs:restriction base="xs:string" >
                                                    <xs:pattern value="男|女"></xs:pattern>
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                        <xs:element name="class" >
                                            <xs:simpleType><!--定义课程,只能为选定值-->
                                                <xs:restriction base="xs:string">
                                                    <xs:enumeration value="语文" />
                                                    <xs:enumeration value="数学" />
                                                    <xs:enumeration value="政治" />
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="address" type="xs:string"></xs:attribute><!--定义属性-->
        </xs:complexType>
    </xs:element>

</xs:schema>
------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<school xmlns="http://www.w3schools.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.w3schools.com D.xsd"
        address="我们的家" >
    <students >
        <student>
            <id>1</id>
            <name>刘正辉</name>
            <age>23</age>
            <sex>男</sex>
            <class>数学</class>
        </student>
        <student >
            <id>2</id>
            <name>何道凤</name>
            <age>22</age>
            <sex>女</sex>
            <class>语文</class>
        </student>
    </students>
</school>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值