抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

[XML] XMLSchema语法结构 9.29

实验1 P94  三大题上机题第1题、第2题、第3题

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="Orders" type="ord"/>
    <xs:complexType name="ord">
        <xs:sequence>
            <xs:element name="Order" minOccurs="1" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string"/>
                        <xs:element name="number" type="numgs"/>
                        <xs:element name="city" type="xs:string"/>
                        <xs:element name="zip" type="zipgs"/>
                    </xs:sequence>
                    <xs:attribute name="orderID" type="idgs" use="required"/>
                    <xs:attribute name="orderDate" type="xs:date" use="optional"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:simpleType name="zipgs">
        <xs:restriction base="xs:string">
            <xs:length value="6"/>
            <xs:pattern value="\d{6}"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="idgs">
        <xs:restriction base="xs:string">
            <xs:length value="4"/>
            <xs:pattern value="[A]{1}\d{3}"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="numgs">
        <xs:restriction base="xs:positiveInteger">
            <xs:maxExclusive value="99"/>
            <xs:minExclusive value="1"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:attribute name="PRODID" type="pid"/>
    <xsd:simpleType name="pid">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="[P]{1}\d{3}"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="PRODUCTDATA" type="prdata"/>
    <xsd:complexType name="prdata">
        <xsd:sequence>
            <xsd:element name="PRODUCT">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="PRODUCTNAME" type="xsd:string"/>
                        <xsd:element name="DISCRIPTION" type="xsd:string"/>
                        <xsd:element name="PRICE" type="xsd:positiveInteger"/>
                        <xsd:element name="QUANTITY" type="xsd:nonNegativeInteger"/>
                    </xsd:sequence>
                    <xsd:attribute ref="PRODID" use="required"/>
                    <xsd:attribute name="CATEGORY" type="cat" use="required"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:simpleType name="cat">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Toy"/>
            <xsd:enumeration value="Book"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PURCHASEORDER" type="pur"/>
    <xs:complexType name="pur">
        <xs:sequence>
            <xs:element name="Order">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="DATE" type="xs:date"/>
                        <xs:element name="PRODUCTNAME" type="xs:string"/>
                        <xs:element name="SUPPLIERNAME" type="xs:string"/>
                        <xs:element name="ADDRESS" type="xs:string"/>
                        <xs:element name="QUANTITY" type="xs:positiveInteger"/>
                        <xs:element name="PRICEPERQTY" type="xs:positiveInteger"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="ORDERID" type="xs:string" use="required"/>
        <xs:attribute name="PRODID" type="xs:string" use="optional"/>
    </xs:complexType>
</xs:schema>

实验2.  根据书籍XML 文档,建立Schema 文档和DTD文档。

<?xml version="1.0" encoding="gb2312" ?>
<book isbn="0-764-58007-8">
    <title>三国演义</title>
    <author >罗贯中</author>
    <price >80.00 </price>
    <resume>滚滚长江东逝水,浪花淘尽英雄。是非成败转头空。青山依旧在,几度夕阳红。</resume>
    <resume>白发渔樵江渚上,惯看秋月春风。一壶浊酒喜相逢。古今多少事,都付笑谈中。</resume>
</book>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="title" type="xs:string"/>
                <xs:element name="author" type="xs:string"/>
                <xs:element name="price" type="xs:string"/>
                <xs:element name="resume" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="isbn" type="k1" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="k1">
        <xs:restriction base="xs:string">
            <xs:length value="13"/>
            <xs:pattern value="\d{1}-\d{3}-\d{5}-\d{1}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

实验3. 在一个XML 文档中,需要用到一个article 的结点,这个结点结构如下:(选做)

<article>
    <HeadLine section="business">
        <MainHead>Main headline goes here</MainHead>
        <SubHead>A subheadline goes here</SubHead>
    </HeadLine>
    <ByLine>
        <Author>John Doe</Author>
        <title>Reporter</title>
    </ByLine>
    <DateLine>April 30, 2005</Dateline>
    <body>Content of story goes here…</body>
    <stats>
        <submitted>2005-03-02</submitted>
        <wordCount>1523</wordCount>
    </stats>
</article>

请在分别定义 HeadLine、ByLine、DateLine、stats 结点的数据类型基础上,再定义article的复杂数据类型。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="artice" type="x1"/>
    <xs:complexType name="x1">
        <xs:sequence>
            <xs:element name="HeadLine" type="x2"/>
            <xs:element name="ByLine" type="x3"/>
            <xs:element name="DateLine" type="xs:string"/>
            <xs:element name="body" type="xs:string"/>
            <xs:element name="stats" type="x4"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="x2">
        <xs:sequence>
            <xs:element name="MainHead" type="xs:string"/>
            <xs:element name="SubHead" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="section" type="xs:string"/>
    </xs:complexType>
    <xs:complexType name="x3">
        <xs:sequence>
            <xs:element name="Author" type="xs:string"/>
            <xs:element name="title" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="x4">
        <xs:sequence>
            <xs:element name="submitted" type="xs:string"/>
            <xs:element name="wordCount" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

关于所以Schema的语法 去w3school看就好 点我 安静下来,一句一句,10分钟不要,你就会了。

 Anr-C 2016.10.8


0条评论

发表评论