DTD & XML Schema Group 3 Seriousl
Well-formed and Valid ที่มา : http://www.ict.up.ac.th/sathienh/DTD%20and%20XML%20Schema.pdf
Well-formed XML Document -
ตองมี root element
-
ตองมี tags เปด - ปด
-
Tags ตองเปน case sensitive
-
Attribute ตองมี quoted(“ ”)เสมอ
Valid XML Document -
เอกสาร Valid ตองเปนเอกสาร Well-formed
-
ตองใช tag ที่กําหนดเฉพาะใน Schema ที่ตกลงกันไวเทานั้น ปจจุบันมี Schema ที่นิยม 3 ตัว คือ DTD , XML Schema และ RELAX NG
What is DTD ? ที่มา http://www.w3schools.com/xml/xml_dtd_intro.asp
DTD DTD ยอมาจาก Document Type Definition โดย เปนตัวกําหนด structure elements และ attributes ของ ไฟล XML DTD จะทําหนาที่ตรวจสอบความถูกตองของเอกสาร XML วาเปนไปตามขอตกลงที่กําหนดไวในไฟล DTD หรือไม ถาไมก็จะฟองแสดงขอผิดพลาดออกมา
XML document with an internal DTD ]>
Tove Jani Reminder Don't forget me this weekend
การเขียน DTD ภายในไฟล XML จะเขียนตอ จาก XML Declaration และ ถูกครอบดวย
XML document with an internal DTD ]>
เราสามารถ ตีความหมายของ DTD ไดดังนี้ - !DOCTYPE note กําหนด root element ของไฟลนี้คือ note - !ELEMENT note กําหนดวา element ของไฟล จะมี elements ตามนี้ : "to,from,heading,body" - !ELEMENT to กําหนด element to เปนประเภท "#PCDATA" - !ELEMENT from กําหนด element from เปนประเภท "#PCDATA" - !ELEMENT heading กําหนด element heading เปนประเภท "#PCDATA" - !ELEMENT body กําหนด element body เปนประเภท "#PCDATA"
An External DTD Declaration
Tove Jani Reminder Don't forget me this weekend!
ถาทําการประกาศ DTD ไวภายนอกไฟล XML จะตองอางอิง(reference)ไป ยังไฟล DTD ดวย
An External DTD Declaration
รูปแบบไฟล "note.dtd" ทีมี DTD ประกาศไว
The Building Blocks of XML Documents สวนประกอบของไฟล XML มีดังนี้ ● ● ● ● ●
Elements Attributes Entities PCDATA CDATA
The Building Blocks of XML Documents - Elements เปนสวนหลักของการสรางไฟล XML ภายใน Element จะบรรจุ text เอาไวดานใน Example some text
some text
The Building Blocks of XML Documents - Attributes เปนสวนเพิ่มเติมขอมูลใหกับ Element และจะถูกใสไวใน tag เปดของ Element Example
"img" คือ element name "src" คือ attribute name "computer.gif" คือคาของ attribute ถา Element วางเปลาจะถูกปดดวย /
The Building Blocks of XML Documents - Entities ขอความพิเศษบางตัวใน XML ไมสามารถใชงานไดโดย ตรงอยางเชน < จึงตองใชตัวอักษรอื่นมาเทียบแทน Example
Entity References
Character
<
<
>
>
&
&
"
"
'
‘
The Building Blocks of XML Documents - PCDATA(parsed character data) เปนขอความที่จะถูก parsed โดย parser ก็คือ เครื่องหมาย < จะบอกวาเปน tag เปดถาตองการให < มีความ หมายวา นอยกวา ตองใช Entity References เขามาเสริม
- CDATA(character data) เปนขอความที่จะไมถูก parsed โดย parser ก็คือ เครื่องหมาย < จะไมไดหมายความวาเปน tag เปด แตจะเปนตัว อักษรปกติทั่วไป
Declaring Elements การกําหนดคา Element ตาม syntax ดังนี้
Or
Empty Elements
Example
XML Example
Elements with Parsed Character Data
Example
Elements with any Contents
Example
Elements with Children (sequences)
Or
Example
Declaring Only One Occurrence of an Element
Example
Declaring Minimum One Occurrence of an Element
Example
เครื่องหมาย + เปนตัวบอกวา ภายใน Element note จะ มี child ที่ชื่อ message ได 1 หรือมากกวานั้น
Declaring Zero or More Occurrences of an Element
Example
เครื่องหมาย * เปนตัวบอกวา ภายใน Element note จะ มี child ที่ชื่อ message ได 0 หรือมากกวานั้น
Declaring Zero or One Occurrences of an Element
Example
เครื่องหมาย ? เปนตัวบอกวา ภายใน Element note จะ มี child ที่ชื่อ message ได 0 หรือ 1 ตัวเทานั้น
Declaring either/or Content
เปนตัวบอกวาใน note element จะมี to element , from element , header element และ message หรือ body element
Declaring Mixed Content
เปนการกําหนดวา ใน note element จะมี Zero or More Occurrences และเปนแบบ parsed character data โดยขางใน จะมี to,from,header หรือ message อยู
Declaring Attributes การกําหนด Attributes syntax ดังนี้
DTD Example
XML Example
Declaring Attributes Attribute-type สามารถใสไดตามนี้ Type
Description
CDATA
The value is character data
(en1|en2|..)
The value must be one from an enumerated list
ID
The value is a unique id
IDREF
The value is the id of another element
IDREFS
‘The value is a list of other ids
NMTOKEN
The value is a valid XML name
Declaring Attributes
Type
Description
NMTOKENS
The value is a list of valid XML names
ENTITY
The value is an entity
ENTITIES
The value is a list of entities
NOTATION
The value is a name of a notation
xml:
The value is a predefined xml value
Declaring Attributes Attribute-value สามารถใสไดตามนี้
Value
Description
value
The default value of the attribute
#REQUIRED
The attribute is required
#IMPLIED
The attribute is optional
#FIXED value
The attribute value is fixed
Use of Elements vs. Attributes
Anna Smith female Anna Smith
จะเห็นไดวา ในตัวอยางการใช Attributes กับไมใชใหขอมูลที่เหมือนกัน
Avoid using attributes เราควรหลีกเลี่ยงการใช Attributes เพราะบางทีอาจทําใหเกิดปญหาได เชน ● ● ● ● ●
attributes ไมสามารถเก็บคาหลายคาได (child elements สามารถทําได) attributes นั้นขยายไดยาก (สําหรับเปลี่ยนแปลงขอมูลในอนาคต) attributes ไมสามารถอธิบายโครงสรางได (child elements สามารถทําได) attributes นั้นยากตอการจัดการดวย code attribute values ทดสอบกับ DTD ไดยาก
ถาหากใช Attributes ในการเก็บขอมูลจะทําใหอานไดยาก ใหใช Element เปนตัวอธิบายขอมูลและใช Attributes ในการใหขอมูลที่ไมไดเกี่ยวของกับขอมูล หลักโดยตรงจะดีที่สุด
Example TV Schedule DTD
Example TV Schedule DTD
]>
Movies DTD
]>
Movies DTD
]>
ทดลองการใช DTD ที่ผิด
Output
ที่มา http://www.herongyang.com/XML/DTD-Validation-of-XML-with-DTD-Using-DOM.html
What is XML Schema ? ที่มา : http://www.w3schools.com/xml/schema_howto.asp
XML Schema XML Schema จะใชอธิบายโครงสรางของ XML Document ภาษา XML Schema อางอิงจาก XML Schema Definition (XSD) ซึ่ง จะใชการเขียนรูปแบบเดียวกับภาษา XML โดย XML Schema กําหนด มาตรฐานโดย W3C
The Purpose of XML Schema -
มี Element และ Attribute อยูภายใน XML Document มีลําดับของ Child Element มีประเภทของขอมูลใน Element และ Attribute กําหนดคา default และ fixed ของขอมูลใน Element และ Attribute
XML Schema Example
XML Document reference to XML Schema
XML Schema Example Define XML Schema ( note.xsd)
Structure of XML Schema
Indicate element and data type used in schema
targetNamespace="http://www.w3schools.com"
*Indicate that the element defined by this schema
xmlns="http://www.w3schools.com"
*Default namespaces
elementFormDefault="qualified">
*Any element in schema must be namespace qualified
Simple Type VS Complex Type Simple Type :
Complex Type :
Peter
Peter
Parker
Parker
28
28
Most Common Data Type ●
xs:string
●
xs:decimal
●
xs:integer
●
xs:boolean
●
xs:date
●
xs:time
Define Simple Element and Attribute Syntax :
Peter Parker 28
Define Complex Element
Peter Parker 28
Schema of Movies Project Movies
film 1
film 2
...
film 3
film n
title
title
title
title
year
year
year
year
types
types
types
types
type 1
type 1
type 1
type 1
type 2
type 2
type 2
type 2
type 3
type 3
type 3
type 3
time
time
time
time
director
director
director
director
Movie XSD
Movie XSD
ทดลองการใช XSD ที่ผิด
Output
DOM parser in Java ตัวอยาง วิธีจัดการกับ tree เมื่อชื่อ element เหมือนกัน ที่ parent Node ตางกัน
title
year
types
film 61
Movies
time
director
screenplay
type 1
p1
type 2
p2
story p1
type 3
film 62 title
year
types
time
director
screenplay
type 1
p1
type 2
p2
type 3
story p1
DOM parser in Java Insert Data เพิ่ม element screenplay และ story
film 61
film 62
สรางฟงกชัน searchMovie() private static void searchMovie(NodeList nList, String search){ System.out.println("\n\nsearch about : " + search); System.out.println("----------------------------"); for (int i = 0; i < nList.getLength(); i++) { //all film Element film = (Element) nList.item(i); NodeList childfilm = film.getChildNodes(); for (int j= 0; j < childfilm.getLength(); j++){ //all node in film Node temp = childfilm.item(j); NodeList childElement = temp.getChildNodes(); if (childElement.getLength() > 1){ for (int l=1; l
เรียกใชฟงกชันคนหา searchMovie() public class MovieST { public static void main(String[] args) { try { File inputFile = new File(".../parseMovie.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); //defines tage list film NodeList nList = doc.getElementsByTagName("film");
[...] searchMovie(nList,"Ted Elliott"); [...] }
DOM parser in Java ผลลัพธที่ไดเมื่อเรียกฟงกชัน searchMovie(nList,"Ted Elliott") NodeId = 61 parent Node = screenplay child Node = p1
NodeId = 61 parent Node = story child Node = p1
NodeId = 62 parent Node = story child Node = p2
Summary ที่มา : http://www.w3schools.com/xml/schema_howto.asp
When to Use a DTD/Schema?
-
ทําใหกลุมคนตางๆ สามารถตกลงใชรูปแบบที่เปน มาตรฐานเพื่องายตอการแลกเปลี่ยนขอมูลกัน
-
สามารถตรวจสอบไดวา ขอมูลที่รับมานั้นมีความถูกตอง (Valid)
-
นอกจากนั้นยังสามารถใช DTD เพื่อตรวจสอบขอมูลของ ตนเอง
When to NOT to Use a DTD/Schema? -
XML ไมจําเปนตองใช DTD/Schema.
-
เมื่อตองการทดลอง XML หรือทํางานเกี่ยวกับไฟล XML ที่มีขนาดเล็ก การทํา DTD อาจจะทําใหเสียเวลา
-
ถาหากทําการพัฒนา applications ใหรอจนกระทั่ง โปรแกรมมีความเสถียรกอนจึงคอยทํา DTD ไมเชนนั้น Software อาจจะหยุดทํางานเนื่องจากการผิดพลาดใน การตรวจสอบ