---
# System prepended metadata

title: XML

---

# XML
## DTD-1
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mp3 [
    <!ELEMENT mp3 (item_no, model, price?,memory*)>
    <!ELEMENT item_no (#PCDATA)>
    <!ELEMENT model (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
    <!ELEMENT memory (#PCDATA)>
]>
<mp3>
    <item_no>
        aaa
    </item_no>
    <model>
        aaaa
    </model>
    <price>
        aaaa
    </price>
    <memory>
        aaaa
    </memory>
</mp3>
```
## DTD-2
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE author [
    <!ELEMENT author (phone*)>
    <!ATTLIST author firstname CDATA #IMPLIED>
    <!ATTLIST author middlename CDATA #IMPLIED>
    <!ATTLIST author lastname CDATA #IMPLIED>
    <!ELEMENT phone (#PCDATA)>
]>
<author firstname="a" middlename="a" lastname="aa">
    <phone>
        aaaa
    </phone>
</author>
```
## DTD-3
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE employees [
    <!ELEMENT employees (employee*)>
    <!ELEMENT employee (name+,age?,sex,address)>
    <!ATTLIST employee id CDATA #REQUIRED>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT age (#PCDATA)>
    <!ELEMENT sex (#PCDATA)>
    <!ELEMENT address (#PCDATA)>
]>
<employees>
    <employee id= "1">
        <name>張三</name>
        <age>32</age>
        <sex>男</sex>
        <address>台中</address>
    </employee>
    <employee id= "2">
        <name>李四</name>
        <age>22</age>
        <sex>男</sex>
        <address>新竹</address>
    </employee>
</employees> 
```
## XML 1-1
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<media xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml1.xsd">
    <paper>
        aaaaa
    </paper>
</media>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<media xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml1.xsd">
    <paper>
        aaaaa
    </paper>
</media>
```
## XML 1-2
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="color_type">
        <xs:restriction base="xs:string">
            <xs:enumeration value="white"></xs:enumeration>
            <xs:enumeration value="red"></xs:enumeration>
            <xs:enumeration value="green"></xs:enumeration>
            <xs:enumeration value="blue"></xs:enumeration>
            <xs:enumeration value="black"></xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="tests">
        <xs:sequence>
            <xs:element name="test" type="color_type"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="t"></xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml2.xsd">>
    <test>
        <white>
            aaaaa
        </white>
    </test>
</t> 

```
## XML 1-3
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:group name="mp3_group">
        <xs:sequence>
            <xs:element name="model" type="xs:string" />
            <xs:element name="price" type="xs:string" />
            <xs:element name="memory" type="xs:string" />
        </xs:sequence>
    </xs:group>
    <xs:element name="mp3">
        <xs:complexType>
            <xs:sequence>
                <xs:group ref="mp3_group" minOccurs="1" maxOccurs="1" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<mp3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml3.xsd">
    <model>
        aaa
    </model>
    <price>
        aaa
    </price>
    <memory>
        aaaa
    </memory>
</mp3>
```
## XML 2-1
### DTD
```
<?xml version="1.0" encoding="Big5"?>
<!DOCTYPE BookCatalogue [
    <!ELEMENT BookCatalogue (Book*)>
    <!ELEMENT Book (Title, Author+, ReleaseDate, ISBN?, Publisher)>
    <!ELEMENT Title (#PCDATA)>
    <!ELEMENT Author (#PCDATA)>
    <!ELEMENT ReleaseDate (#PCDATA)>
    <!ELEMENT ISBN (#PCDATA)>
    <!ELEMENT Publisher (Country, Company)>
    <!ELEMENT Country (#PCDATA)>
    <!ELEMENT Company (#PCDATA)>
    <!ATTLIST Book category CDATA #REQUIRED>
    <!ATTLIST Book page CDATA #REQUIRED>
    <!ATTLIST Author sex (男|女|male|female) "男">
    <!ATTLIST Author mail CDATA #IMPLIED>
    <!ATTLIST Publisher city NMTOKEN "Taipei">
    <!ATTLIST Publisher address NMTOKENS #REQUIRED>
]>
<BookCatalogue>
    <Book category="aaa" page="55">
        <Title>
            ssss
        </Title>
        <Author sex="男" mail="ssss">
            aaaa
        </Author>
        <ReleaseDate>
            ssss
        </ReleaseDate>
        <ISBN>
            sssss
        </ISBN>
        <Publisher address="SSSS">
            <Country>SSSSS</Country>
            <Company>SSSS</Company>
        </Publisher>
    </Book>
</BookCatalogue>
```
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="BookCatalogue">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Title" type="xs:string"></xs:element>
                            <xs:element name="Author" type="Authortype" minOccurs="0" maxOccurs="1"></xs:element>
                            <xs:element name="ReleaseDate" type="xs:string"></xs:element>
                            <xs:element name="ISBN" type="xs:string" maxOccurs="1" minOccurs="0"></xs:element>
                            <xs:element name="Publisher">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="Country" type="xs:string"></xs:element>
                                        <xs:element name="Company" type="xs:string"></xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="city" default="Taipei" type="xs:NMTOKEN"></xs:attribute>
                                    <xs:attribute name="address" use="required" type="xs:NMTOKENS"></xs:attribute>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="category" type="xs:string" use="required"></xs:attribute>
                        <xs:attribute name="page" type="xs:string" use="required"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="Authortype">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="sex" default="男">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="男"></xs:enumeration>
                            <xs:enumeration value="女"></xs:enumeration>
                            <xs:enumeration value="male"></xs:enumeration>
                            <xs:enumeration value="female"></xs:enumeration>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
                <xs:attribute name="mail" type="xs:string" use="optional"></xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="Big5"?>
<BookCatalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="xml2-1.xsd">
    <book category="aaa" page="55">
        <Title>
            ssss
        </Title>
        <Author sex="女" mail="ssss">
            aaaa
        </Author>
        <ReleaseDate>
            ssss
        </ReleaseDate>
        <ISBN>
            sssss
        </ISBN>
        <Publisher address="SSSS">
            <Country>SSSSS</Country>
            <Company>SSSS</Company>
        </Publisher>
    </book>
</BookCatalogue>
```
## XML 2-2
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="employees">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="employee" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" minOccurs="1" maxOccurs="1"  type="xs:string"></xs:element>
                            <xs:element name="age" minOccurs="1" maxOccurs="1"  type="xs:string"></xs:element>
                            <xs:element name="sex" type="xs:string" maxOccurs="1" minOccurs="0"></xs:element>
                            <xs:element name="address" type="xs:string" maxOccurs="unbounded"></xs:element>
                        </xs:sequence>
                        <xs:attribute name="pid" type="xs:string" use="required"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="xml2-2.xsd">
 <employee pid="B123456789">
     <name>張三</name>
     <age>32</age>
     <sex>男</sex>
     <address>台中</address>
 </employee>
 <employee pid="P234567891">
     <name>李四</name>
     <age>22</age>
     <sex>男</sex>
     <address>新竹</address>
 </employee>
 </employees>
```
## XML 2-3
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Orders">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Order" maxOccurs="unbounded" minOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"></xs:element>
                            <xs:element name="number">
                                <xs:simpleType>
                                    <xs:restriction base="xs:int">
                                        <xs:minExclusive value="149"></xs:minExclusive>
                                        <xs:maxExclusive value="451"></xs:maxExclusive>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="city" type="xs:string"></xs:element>
                            <xs:element name="zip">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="\d{3}"></xs:pattern>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="orderID" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="[T]\d{2}-[P]-\d{3}"></xs:pattern>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                        <xs:attribute name="orderDate" type="xs:string" use="optional"></xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml2-3.xsd">
    <Order orderID="T01-P-011" orderDate="2016-5-19">
        <name>Toy</name>
        <number>400</number>
        <city>Hsinchu</city>
        <zip>301</zip>
    </Order>
    <Order orderID="T02-P-012" orderDate="2016-5-20">
        <name>ToyBar</name>
        <number>168</number>
        <city>Taichung</city>
        <zip>401</zip>
    </Order>
</Orders>
```
## XML 2-4
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="inventory">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"></xs:element>
                            <xs:element name="price"  type="xs:string"></xs:element>
                            <xs:element name="quantity" type="xs:string" minOccurs="0"></xs:element>
                            <xs:element name="available" type="xs:string" minOccurs="0"></xs:element>
                        </xs:sequence>
                        <xs:attribute name="isbn" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="\d{1}-\d{2}-\d{6}-\d{1}"></xs:pattern>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                        <xs:attribute name="inStock" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="yes"></xs:enumeration>
                                    <xs:enumeration value="no"></xs:enumeration>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<!-- File name : inventory.xml -->
<inventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml2-4.xsd">
    <book isbn="0-12-012507-8" inStock="yes">
        <name> How to program in Java 3/e</name>
        <price>USD88</price>
        <quantity>300</quantity>
    </book>
    <book isbn="0-12-028418-5" inStock="no">
        <name>XML and Web Service</name>
        <price>USD68 </price>
        <available>2015-12-15</available>
    </book>
</inventory>
```
## XML 2-5
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="booklist">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="bookTitle" minOccurs="1" maxOccurs="1"  type="xs:string"></xs:element>
                            <xs:element name="pubDate" minOccurs="1" maxOccurs="1"  type="xs:string"></xs:element>
                            <xs:element name="authorlist" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="author" type="xs:string"></xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:string"></xs:element>
                        </xs:sequence>
                        <xs:attribute name="ISBN" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="\d{3}-\d{3}-\d{3}-\d{3}-\d{1}"></xs:pattern>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml2-5.xsd">
    <book ISBN="966-788-611-147-9">
        <bookTitle>DBMS Using SQL Server 2015</bookTitle>
        <pubDate>2015-08-30</pubDate>
        <authorlist>
            <author>潔西卡</author>
        </authorlist>
        <price>NT780</price>
    </book>
    <book ISBN="966-788-622-146-5">
        <bookTitle>Java 8 語言入門</bookTitle>
        <pubDate>2015-07-12</pubDate>
        <authorlist>
            <author>威爾遜</author>
        </authorlist>
        <price> NT 700</price>
    </book>
    <book ISBN="966-788-244-127-2">
        <bookTitle>Java 8 語言指引</bookTitle>
        <pubDate>2015-11-25</pubDate>
        <authorlist>
            <author>尼爾森</author>
        </authorlist>
        <price> NT 750</price>
    </book>
</booklist>
```
## XML 2-6
### .xsd
```
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PRODUCTDATA" type="prdata" />
    <xs:complexType name="prdata">
        <xs:sequence>
            <xs:element name="PRODUCT" type="prdt" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="prdt">
        <xs:sequence>
            <xs:element name="PRODUCTNAME" type="xs:string" />
            <xs:element name="DESCRIPTION" type="xs:string" />
            <xs:element name="PRICE" type="xs:decimal" minOccurs="3" maxOccurs="5"></xs:element>
            <xs:element name="QUANTITY" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>
```
### .xml
```
<?xml version="1.0" encoding="UTF-8"?>
<PRODUCTDATA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="xml2-6.xsd">
    <PRODUCT>
        <PRODUCTNAME>sss</PRODUCTNAME>
        <DESCRIPTION>ssss</DESCRIPTION>
        <PRICE>555</PRICE>
        <PRICE>555</PRICE>
        <PRICE>555</PRICE>
        <QUANTITY>222</QUANTITY>
    </PRODUCT>
</PRODUCTDATA>
```