Phương pháp thứ nhất : Cấu trúc lồng
Viết XLM schema từ trên xuống dưới, nghÄ©a là viết đến element nà o thì định nghÄ©a element đó. Như váºy, đối vá»›i và dụ trên, chúng ta sẽ bắt đầu vá»›i việc Ä‘inh nghÄ©a transcript
- bên trong transcript ta sẽ tiếp tục đinh nghĩa student và unit,
- bên trong student lại định nghĩa studentID,name và degree
- bên trong unit thì định nghĩa code,unitNam và grade
Nói tóm lại, XML scheam sẽ có cấu trúc lồng và o nhau, duới đây là lược đồ của các elements trong XLM schema:
Code cá»§a schame1.xsd:
|
|
<?xml version="1.0" encoding="utf-8" ?>
<!--
XML Schema 1
-->
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="transcript">
<xs:complexType>
<xs:sequence>
<!-- Start element Student information -->
<xs:element name="student" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"
maxOccurs="1" minOccurs="1" />
<xs:element name="degree" maxOccurs="5"
minOccurs="1" type="xs:string" />
</xs:sequence>
<xs:attribute use="required" name="id">
<xs:simpleType>
<xs:restriction base="xs:long">
<xs:maxInclusive value="999999" />
<xs:minInclusive value="100000" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<!-- End element Student information -->
<!-- Start element Unit information -->
<xs:element name="unit" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="unitName" maxOccurs="1"
minOccurs="1" type="xs:string" />
<xs:element name="grade" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="High Distinction" />
<xs:enumeration value="Distinction" />
<xs:enumeration value="Credit" />
<xs:enumeration value="Pass" />
<xs:enumeration value="Fail" />
<xs:enumeration value="Incomplete" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="code" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="7" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<!-- Start element Unit information -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
|
Nhược điểm của phương pháp lồng:
- Khi viết code theo kiểu lồng và o nhau thì XML schema sẽ có code rất phức tạp. Như các bạn đã thấy, chỉ với một xml đơn giản mà code của XML cũng đã khá rối rắm, nếu xml phức tạp hơn một chút thì sẽ cà ng khó cho việc debug hơn.
- Việc viết code theo kiểu từ trên xuống không sỠdụng lại các element đã định nghĩa.
Phương pháp 2: Cấu trúc riêng lẻ
Äây là cách viết có phần “hướng đổi tượngâ€, chúng ta sẽ định nghÄ©a má»—i element riêng biệt, rồi sá» dụng các element nà y như là kiểu dữ liệu đối vá»›i element khác.Nhu váºy, vá»›i và dụ trên, chúng ta tiến hà nh như sau:
- đinh nghĩa transcript
- định nghĩa student
- đinh nghĩa unit
- định nghĩa studentID
- định nghia unitCode
- định nghĩa grade
Trong đó:
- transcrip sỠdụng student và unit như là kiểu dữ liệu con
- student sỠdụng studentID và grade như kiểu dữ liệu con
- unit sỠdụng unitCode như kiểu dữ liệu con
Lược đồ của XML schema :
Code cá»§a chema2.xsd:
|
|
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- annotation: header comments-->
<xs:annotation>
<xs:documentation xml:lang="en">
XML Schema 2
</xs:documentation>
</xs:annotation>
<!-- transcript element with type of transcriptType-->
<xs:element name="transcript" type="transcriptType" />
<!-- transcriptType: complextype -->
<xs:complexType name="transcriptType">
<xs:sequence>
<xs:element name="student" type="studentType"
maxOccurs="1" />
<xs:element name="unit" type="unitType"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<!-- studentType: complextype -->
<xs:complexType name="studentType">
<xs:sequence>
<xs:element name="name" type="xs:string"
maxOccurs="1" />
<xs:element name="degree" type="xs:string"
minOccurs="1" maxOccurs="5" />
</xs:sequence>
<xs:attribute name="id" type="studentID" use="required" />
</xs:complexType>
<!-- unitType: complextype -->
<xs:complexType name="unitType">
<xs:sequence>
<xs:element name="unitName" type="xs:string"
minOccurs="1" maxOccurs="1" />
<xs:element name="grade" type="gradeType"
maxOccurs="1" />
</xs:sequence>
<xs:attribute name="code" type="codeType" />
</xs:complexType>
<!-- StudentID: simpletype -->
<xs:simpleType name="studentID">
<xs:restriction base="xs:long">
<xs:minInclusive value="100000" />
<xs:maxInclusive value="999999" />
</xs:restriction>
</xs:simpleType>
<!-- codeType: simpletype -->
<xs:simpleType name="codeType">
<xs:restriction base="xs:string">
<xs:length value="7" />
</xs:restriction>
</xs:simpleType>
<!-- gradeType: simpletype -->
<xs:simpleType name="gradeType">
<xs:restriction base="xs:string">
<xs:enumeration value="High Distinction" />
<xs:enumeration value="Distinction" />
<xs:enumeration value="Credit" />
<xs:enumeration value="Pass" />
<xs:enumeration value="Fail" />
<xs:enumeration value="Incomplete" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
|
|
|
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- annotation: header comments-->
<xs:annotation>
<xs:documentation xml:lang="en">
XML Schema 2
</xs:documentation>
</xs:annotation>
<!-- transcript element with type of transcriptType-->
<xs:element name="transcript" type="transcriptType" />
<!-- transcriptType: complextype -->
<xs:complexType name="transcriptType">
<xs:sequence>
<xs:element name="student" type="studentType"
maxOccurs="1" />
<xs:element name="unit" type="unitType"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<!-- studentType: complextype -->
<xs:complexType name="studentType">
<xs:sequence>
<xs:element name="name" type="xs:string"
maxOccurs="1" />
<xs:element name="degree" type="xs:string"
minOccurs="1" maxOccurs="5" />
</xs:sequence>
<xs:attribute name="id" type="studentID" use="required" />
</xs:complexType>
<!-- unitType: complextype -->
<xs:complexType name="unitType">
<xs:sequence>
<xs:element name="unitName" type="xs:string"
minOccurs="1" maxOccurs="1" />
<xs:element name="grade" type="gradeType"
maxOccurs="1" />
</xs:sequence>
<xs:attribute name="code" type="codeType" />
</xs:complexType>
<!-- StudentID: simpletype -->
<xs:simpleType name="studentID">
<xs:restriction base="xs:long">
<xs:minInclusive value="100000" />
<xs:maxInclusive value="999999" />
</xs:restriction>
</xs:simpleType>
<!-- codeType: simpletype -->
<xs:simpleType name="codeType">
<xs:restriction base="xs:string">
<xs:length value="7" />
</xs:restriction>
</xs:simpleType>
<!-- gradeType: simpletype -->
<xs:simpleType name="gradeType">
<xs:restriction base="xs:string">
<xs:enumeration value="High Distinction" />
<xs:enumeration value="Distinction" />
<xs:enumeration value="Credit" />
<xs:enumeration value="Pass" />
<xs:enumeration value="Fail" />
<xs:enumeration value="Incomplete" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
|
Vá»›i cách viết code cho từng element riêng lẻ, chúng ta rất dẽ dà ng cho việc debug và sá» dụng lại các element trong trưá»ng hợp cần thiết, tránh việc dư thừa và code rác.
[right][size=1][url=http://bachkhoaaptech.com/forum/showthread.php?p=1949]Copyright © Diá»…n Äà n BachKhoa-Aptech - Posted by NgocToan[/url][/size][/right]