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 อาจจะหยุดทํางานเนื่องจากการผิดพลาดใน การตรวจสอบ

DTD & XML Schema.pdf

Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. DTD & XML Schema.pdf. DTD & XML Schema.pdf. Open. Extract.

881KB Sizes 3 Downloads 224 Views

Recommend Documents

No documents