2. Question Last Week:
About the same tag
เนื่องจาก XML ไม่มีการกาหนดชื่อของ element ที่ตายตัว จึงอาจเกิดปัญหาในการตั้งชื่อ้้ากัน
แต่วัตถุประสงค์และองค์ประกอบไม่ตรงกัน ยกตัวอย่างเช่น
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
เอกสารจัดเก็บข้อมูลแบบตาราง
3. 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 เข้าไปในชื่อ ดังนี้
4. 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>
5. 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>
6. 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
7. 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
13. 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
14. DTD
(Document Type
Declaration)
from lxml import etree
parser = etree.XMLParser(dtd_validation=True)
tree = etree.parse("MovieAll_SPN.xml", parser)
ใช้ Python ตรวจสอบ XML โดยใช้ DTD
17. 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>
18. Well-Formed
XML Document
• สรุป Well-formXML
1. XML ที่มี Syntax ถูกต้องจะเรียกว่า Well-formedแต่ก็ไม่ได้รับประกันว่าข้อมูลที่กาลังอธิบาย
มี “โครงสร้าง” หรือ “ประเภท” ของข้อมูลว่าถูกต้องตามที่ผู้สร้างหรือ ผู้ที่จะนาไปใช้ต้องการ
2. ส่วน XML ที่มีโครงสร้างและข้อมูลแต่ละ Element ถูกต้องตาม Schema กาหนดจะเรียกว่า
Validated
19. Well-Formed
XML Document
• HTML เป็ น Well-form ไหม ?
เอกสาร HTML ไม่เป็นเอกสารในรูปแบบ well-formed เนื่องจากคาสั่ง หรือ tag ในภาษา
HTML มีหลาย tag ที่มีเฉพาะ tag เปิด โดยที่ไม่จาเป็นต้องใช้tag ปิดเลย ทาให้เอกสารที่สร้าง
ด้วย tag HTML จึงไม่ถือเป็นเอกสาร well-formed
22. Reference
• Valid XML Document
https://th.wikipedia.org/wiki/xml
code.function.in.th/xml/xml-parser
• Question Last Week: About the same tag
http://www.goragod.com/knowledge/XML