to try and create dtd and xml schema

24
“หหหหหหหหหหหหหหห” (Group 1) Selected Topics in Computer Engineering II DTD & XML Schema To try and create

Upload: aey-unthika

Post on 13-Apr-2017

228 views

Category:

Education


4 download

TRANSCRIPT

Page 1: To try and create DTD and XML Schema

“หนงหนวยกลม ” (Group 1)Selected Topics in Computer Engineering II

DTD & XML Schema

To try and create

Page 2: To try and create DTD and XML Schema

Question Last Week: About the same tag

เนองจาก XML ไมมการกำาหนดชอของ element ทตายตว จงอาจเกดปญหาในการตงชอซำากน

แตวตถประสงคและองคประกอบไมตรงกน ยกตวอยางเชน<table> <tr>     <td>Apples</td>     <td>Bananas</td> </tr></table>

เอกสารจดเกบขอมลแบบตาราง

Page 3: To try and create DTD and XML Schema

Question Last Week: About the same tag

<table> <name>African Coffee Table</name> <width>80</width> <length>120</length></table>

เอกสารจดเกบรายละเอยดขอมลเฟอรนเจอร

เหนวาทง 2 สวนจะม root element เปน table เหมอนกน แต table แรก และ table หลงมขอมลทไมเหมอนกน วธการแกปญหาดงกลาวทำาไดโดยเพม Prefix เขาไปในชอ ดงน

Page 4: To try and create DTD and XML Schema

Question Last Week: About the same tag

<h:table> <h:tr>     <h:td>Apples</h:td>     <h:td>Bananas</h:td> </h:tr></h:table><f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length></f:table>

Page 5: To try and create DTD and XML Schema

Question Last Week: About the same tag

สำาหรบเอกสาร XML จะม attribute เพมเตมอกสวนหนงเพอเปนการประกาศ namespace สำาหรบอางองภายหลง ดงน

<table xmlns="http://www.w3.org/TR/html5/ ">  <tr>     <td>Apples</td>     <td>Bananas</td> </tr></table><table xmlns="http://www.w3schools.com/furniture ">  <name>African Coffee Table</name> <width>80</width> <length>120</length></table>

Page 6: To try and create DTD and XML Schema

Question Last Week: About the same tag

ตวอยางการอางองโดยใช namespace<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform "><xsl:template match="/"><html><body>   <h2>My CD Collection</h2>   <table border="1">    <tr>     <th align="left">Title</th>     <th align="left">Artist</th>    </tr>    <xsl:for-each select="catalog/cd">    <tr>     <td><xsl:value-of select="title"/></td>     <td><xsl:value-of select="artist"/></td>    </tr>    </xsl:for-each>  </table></body></html></xsl:template></xsl:stylesheet>

การใช XSL style sheet สำาหรบแปลงเอกสาร XML ใหอยในรปแบบทกำาหนด จากตวอยาง tag ทขนตนดวย xsl จะเปน tag ทใช namespace ในการอางองรปแบบ ซงประกาศไวท http://www.w3.org/1999/XSL/Transform

Page 7: To try and create DTD and XML Schema

XML Schema

XML Schema ใชสำาหรบการใหรายละเอยดโครงสรางของเอกสาร XML โดยมชอเรยกอกอยา

วา XML Schema Definition (.XSD)

โดยเปาหมายของการทำา XML Schema คอการกำาหนด Block ของ XML ทอานแลวรเร อง

เขาใจงายและนำาไปใชไดอยางถกตองโดยจะกำาหนดสงตอไปน :• elements และ attributes ท สามารถไปอยบนเอกสาร (XML

document)ได• เลขทและอนดบของ child elements• data types สำาหรบ elements และ attributes• คาเรมตน และ คาคงท ของ elements และ attributes

Page 8: To try and create DTD and XML Schema

XML Schema• ตวอยาง XML

ธรรมดา<?xml version="1.0" encoding="UTF-8"?><shiporder orderid="889923"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</orderperson> <shipto>   <name>Ola Nordmann</name>   <address>Langgt 23</address>   <city>4000 Stavanger</city>   <country>Norway</country> </shipto> <item>   <title>Empire Burlesque</title>   <note>Special Edition</note>   <quantity>1</quantity>   <price>10.90</price> </item> <item>   <title>Hide your heart</title>   <quantity>1</quantity>   <price>9.90</price> </item></shiporder>

Page 9: To try and create DTD and XML Schema

XML Schema• เทยบกบ XML

Schema<?xml version="1.0" encoding="UTF-8" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="shiporder"> <xs:complexType>   <xs:sequence>     <xs:element name="orderperson" type="xs:string"/>     <xs:element name="shipto">       <xs:complexType>         <xs:sequence>           <xs:element name="name" type="xs:string"/>           <xs:element name="address" type="xs:string"/>           <xs:element name="city" type="xs:string"/>           <xs:element name="country" type="xs:string"/>         </xs:sequence>       </xs:complexType>     </xs:element>     <xs:element name="item" maxOccurs="unbounded">       <xs:complexType>         <xs:sequence>           <xs:element name="title" type="xs:string"/>           <xs:element name="note" type="xs:string" minOccurs="0"/>           <xs:element name="quantity" type="xs:positiveInteger"/>           <xs:element name="price" type="xs:decimal"/>         </xs:sequence>       </xs:complexType>     </xs:element>   </xs:sequence>   <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType></xs:element></xs:schema>

Page 10: To try and create DTD and XML Schema

XML Schema• Simplified

form<?xml version="1.0" encoding="UTF-8" ?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- definition of simple elements --><xs:element name="orderperson" type="xs:string"/><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/>

<!-- definition of attributes --><xs:attribute name="orderid" type="xs:string"/>

<!-- definition of complex elements --><xs:element name="shipto"> <xs:complexType>   <xs:sequence>     <xs:element ref="name"/>     <xs:element ref="address"/>     <xs:element ref="city"/>     <xs:element ref="country"/>   </xs:sequence> </xs:complexType></xs:element>

<xs:element name="item"> <xs:complexType>   <xs:sequence>     <xs:element ref="title"/>     <xs:element ref="note" minOccurs="0"/>     <xs:element ref="quantity"/>     <xs:element ref="price"/>   </xs:sequence> </xs:complexType></xs:element>

<xs:element name="shiporder"> <xs:complexType>   <xs:sequence>     <xs:element ref="orderperson"/>     <xs:element ref="shipto"/>     <xs:element ref="item" maxOccurs="unbounded"/>   </xs:sequence>   <xs:attribute ref="orderid" use="required"/> </xs:complexType></xs:element>

</xs:schema>

Page 11: To try and create DTD and XML Schema

DTD (Document Type

Declaration)DTD การนยามความหมาย การกำาหนดโครงสรางและ

ขอมลของกฎเกณฑของเอกสาร ทจดเกบโดยเอกสาร XML ใชเปนตวกำาหนดวา Element หนงๆ นนมสวนประกอบอะไรบาง แลวสามารถใช DTD สำาหรบตรวจสอบความถกตองของเอกสาร XML วาสอดคลองกบขอตกลงทกำาหนดไวเปนไวยากรณในไฟล DTD หรอไม ถาไมกจะฟองแสดงขอผดพลาดออกมาโครงสรางของ DTD <!DOCTYPE note [<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>]>

Page 12: To try and create DTD and XML Schema

DTD (Document Type

Declaration)• ใช <!ELEMENT note (to,from,heading,body)> ในการกำาหนดคาตางๆ

ของ ELEMENT ใน (to,from,heading,body) เปนการบอกวาม ELEMENT ใดทอยใน ELEMENT note หากมสญลกษณ

+ ตองมคาอยางนอยหนงคา แตอาจมไดมากกวานน* อาจมคาได N ครง หรออาจไมมคาเลยกได? มคาไดเพยงคาเดยวหรอไมมคาเลย

• สวน (#PCDATA) เปนตวกำาหนดใหภายใน element มขอความอย หรอไมมขอความอย หากม Attribute จะใช  <!ATTLIST elementName attributeName CDATA > ซงสามารถกำาหนดรายละเอยดไดตวอยางเชน

CDATA: คาขอมลของ attribute ตองเปนตวอกษรENTITY, ENTITIES: ชอของเอนตตทประกาศใน DTDID: ขอมลซงมคาไมซำา (unique)

• การประกาศแบบภายใน (Internal)<!DOCTYPE root [

ขอมล]>

• การประกาศแบบภายนอก (External)<!DOCTYPE root SYSTEM "ชอไฟล.dtd">

Page 13: To try and create DTD and XML Schema

DTD (Document Type

Declaration)

<!ELEMENT film (movie+)> <!ELEMENT movie (name,types,stars,director,date)> <!ELEMENT name (#PCDATA)> <!ELEMENT types (type+)> <!ELEMENT type (#PCDATA)> <!ELEMENT stars (name_actor+)> <!ELEMENT name_actor (#PCDATA)> <!ELEMENT director (#PCDATA)><!ELEMENT date (day,month,year)> <!ELEMENT day (#PCDATA)> <!ELEMENT month (#PCDATA)> <!ELEMENT year (#PCDATA)>

Create DTD

Page 14: To try and create DTD and XML Schema

DTD (Document Type

Declaration)

from lxml import etreeparser = etree.XMLParser(dtd_validation=True)tree = etree.parse("MovieAll_SPN.xml", parser)

ใช Python ตรวจสอบ XML โดยใช DTD

Page 15: To try and create DTD and XML Schema

DTD (Document Type

Declaration)XML file ทเพม DTD ตรวจสอบ

<movie> <name>The Lobster</name> <types> <type>Comedy</type> <type>Drama</type> <type>Romance</type> </types> <stars> <name_actor>Colin Farrell</name_actor> </stars> <director>Yorgos Lanthimos</director> <date> <day>15</day> <month>Nov</month> <year>2015</year> </date></movie></film>

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE film SYSTEM "movie_dtd.dtd"><film> <movie> <name>Always</name> <types> <type>Drama</type> <type>Romance</type> </types> <stars> <name_actor>So Ji-seob</name_actor> <name_actor>Han Hyo-joo</name_actor> </stars> <director>Song Il-Gon</director> <date> <day>2</day> <month>Feb</month> <year>2012</year> </date> </movie>

Page 16: To try and create DTD and XML Schema

DTD (Document Type

Declaration)ถาลองแกไฟลใหไมม <name_actor> ผลลพธจะขน error

Page 17: To try and create DTD and XML Schema

Well-Formed XML Document

• ลกษณะWell Formed XML เปนอยางไร

1. XML ตองม root element เชน <book>...</book>2. XML element ตองม tag ปดเสมอ เชน

<title>...</title>3. XML tag เปน case sensitive ( <Letter> ไมเหมอนกบ

<letter> )4. XML ตองสรางใหเหมาะสม เชน การแบงกลมของขอมล การ

จดการ Attribute อนนตองอาศยวธคด5. XML attribute value ตองใส อญญประกาศ (Double

quotes: " ") เชน<book location="GatewayMall">Whiteout</book>

Page 18: To try and create DTD and XML Schema

Well-Formed XML Document

• สรป Well-formXML 1. XML ทม Syntax ถกตองจะเรยกวา Well-formed แตกไมได

รบประกนวาขอมลทกำาลงอธบายม โครงสราง หรอ ประเภท “ ” “ ”ของขอมลวาถกตองตามทผสรางหรอ ผทจะนำาไปใชตองการ

2 .สวน XML ทมโครงสรางและขอมลแตละ Element ถกตองตาม Schema กำาหนดจะเรยกวา Validated

Page 19: To try and create DTD and XML Schema

Well-Formed XML Document

• HTML เปน Well-form ไหม ?

เอกสาร HTML ไมเปนเอกสารในรปแบบ well-formed เนองจากคำาสง หรอ tag ในภาษา HTML มหลาย tag ทมเฉพาะ tag เปด โดยทไมจำาเปนตองใช tag ปดเลย ทำาใหเอกสารทสรางดวย tag HTML จงไมถอเปนเอกสาร well-formed

Page 20: To try and create DTD and XML Schema

Valid XML Document

• ขอกำาหนด

คอ สวนทเพมเตมของขอบงคบในขอกำาหนดรายละเอยด (XML Specification) ทตองใชในการสรางเอกสาร เพราะวาความถกตองสมบรณเปนหลกทางเลอก (Option) สำาหรบ XML การฝาฝน ขอบงคบจะทำาใหเกดขอผดพลาด (Error) ทจะตรงขามกบ (Fatal Error) เมอตวประมวลผล Error จะรายงานปญหา และพยายามแกไขดวยตวเอง ขอบงคบของความถกตองสมบรณ จะประกอบดวยขอบงคบทระบเกยวกบ การสราง Document Type Declaration (DTD) และการสรางเอกสารทตองทำาตามขอกำาหนดใน DTD

• ขอบงคบของความถกตองสมบรณ (Validity Constraints)

1. จะตองเขาเปน Well-formed XML Document กอน2 .มสวน Prolog ของเอกสารจะตองประกอบดวย

Document Type Declaration ทถกตอง และขอมล ภายใน DTD จะเปนตวกำาหนดโครงสรางของเอกสารดงกลาว

3. มสวนตาง ๆ ของเอกสารจะตองถกตองตามโครงสรางทกำาหนดใน DTD

Page 21: To try and create DTD and XML Schema

Valid XML Document

• ประโยชนของการสรางเอกสารทถกตองสมบรณ

1 .สามารถเพม Element และ Attribute ไดตามตองการ2 .สวนประกอบของ DTD นนจะอธบายโครงสรางซงจะอนญาตให

ตวประมวลผล XML (XML Processor) ซงเปนสวนหนงของ Program Browser ตรวจสอบวาเอกสารนนตรงตามโครงสรางหรอไม

3 .สวนอนๆของ DTD จะจดเตรยมแผนมาตรฐานใหแกตวประมวลผล เชน ในการตรวจสอบความถกตอง ถาสวนใด ๆ ของเอกสารไมตรงตามขอกำาหนดรายละเอยด (Specification) ของ DTD ตวประมวลผลจะแสดงขอผดพลาด (Error  Massage) ซงจะสามารถนำาไปแกไข และปฏบตใหถกตองได

Page 22: To try and create DTD and XML Schema

Reference• Valid XML Document

https://th.wikipedia.org/wiki/xmlcode.function.in.th/xml/xml-parser

• Question Last Week: About the same taghttp://www.goragod.com/knowledge/XML

Page 23: To try and create DTD and XML Schema

Members

Group1นาย อภวฒธ วงศโทะ รหสนกศกษา 52-1116-530-2นางสาว พลลภา เขมรงสฤษฏ รหสนกศกษา 56-010126-2008-1นางสาว อญธกา หนองบว รหสนกศกษา 56-010126-3028-1นาย ธรวฒน ผองสกล รหสนกศกษา 56-010126-3015-9นาย ธนดล เตชะวชรกล รหสนกศกษา 56-010126-3009-4นาย ภมมฑล ไชยเชดเกยรต รหสนกศกษา 56-010116-2131-8

Page 24: To try and create DTD and XML Schema