เอกสาร WSDL ที่ใช้อธิบายการบริการของ web service ประกอบไปด้วย element หลักดังนี้
โครงสร้างหลักของเอกสาร WSDL มีลักษณะดังนี้
<definitions> <types> การนิยามชนิดข้อมูลต่าง ๆ ..... </types> <message> การนิยาม message ต่าง ๆ .... </message> <portType> การนิยาม port ในการเชื่อมต่อและ operations ต่าง ๆ ... </portType> <binding> การนิยาม binding ..... </binding> </definitions>
ซึ่ง element definitions เป็น root elements ของเอกสาร WSDL นอกจากนี้เอกสาร WSDL ยังสามารถประกอบไปด้วย element อื่น ๆ เช่น extension elements และ service element ซึ่งมันจะทำการจัดกลุ่ม web service ต่าง ๆ (ในกรณีที่มี service จำนวนมากมาย) ภายในเอกสาร WSDL เอกสารเดียว
<portType> element มีความสำคัญที่สุดในเอกสาร WSDL ซึ่งมันจะทำการอธิบาย web service ว่ามี operations (บริการ) อะไรบ้าง และ messages (หรือ parameter ในความหมายเชิงโปรแกรม) ที่ต้องใช้ในแต่ละ operations โดยที่ <portType> element เปรียบเสมือน function library (หรือ module หรือ class) ในเชิงโปรแกรมนั่นเอง
<message> element เป็นการนิยาม data element (หรือชุดพารามิเตอร์ที่ต้องใช้) ของ operation ต่าง ๆซึ่งแต่ละ message สามารถมีได้มากกว่า 1 parts โดยที่แต่ละ parts element นั้นเปรียบเสมือนพารามิเตอร์แต่ละตัวที่ส่งค่าไป เมื่อเรียกใช้ฟังก์ชันนั้น ๆ
<types> element เป็นการนิยามชนิดข้อมูลที่ใช้ภายใน web service ซึ่ง element นี้นั้น WSDL จะใช้ XML Schema syntax ในการนิยามชนิดข้อมูล โดยผมจะเขียนบทความเกี่ยวกับ XML Schema ในเวลาต่อไป
<binding> element เป็นการนิยามรูปแบบ message และรายละเอียด protocl แต่ละ port
ตัวอย่างของเอกสาร WSDL เบื้องต้นเพื่อให้ผู้อ่านทำความเข้าใจในขั้นแรก
<message name="getTermRequest"> <part name="term" type="xs:string" /> </message> <message name="getTermResponse"> <part name="value" type="xs:string" /> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest" /> <output message="getTermResponse" /> </operation> </portType>
ในตัวอย่างนี้ <portType> element ถูกนิยามให้ชื่อ port นี้เป็น "glossaryTerms" และบริการหรือ operation ที่มีใน web service นี้ก็คือ "getTerm"
ซึ่ง operation "getTerm" นั้นมี input message ชื่อว่า "getTermRequest" และ output message ที่ชื่อว่า "getTermReponse" ซึ่งโดยปกติแล้ว message ในการร้องขอข้อมูล (หรือ message ที่เป็น input) จะใช้ Request ตามหลัง และ message ในการตอบสนองหรือส่งค่ากลับมา (หรือ output message) จะใช้ Response ตามหลัง ดังตัวอย่าง
<message> element เป็นการนิยาม parts (พารามิเตอร์) ของแต่ละ message รวมไปถึงกำหนดชนิดข้อมูลให้แต่ละ parts ด้วย
ถ้ามองในเชิงโปรแกรม glossaryTerms ก็คือ function library (หรือ class) ส่วน getTerm ก็คือ function ซึ่งภายในฟังก์ชันนี้ต้องการ input parameter ก็คือ getTermRequest และฟังก์ชันนี้จะส่งค่ากลับมา (output parameter) ก็คือ getTermResponse
ผมจะยกตัวอย่างภาษา PHP ตามโครงสร้างเอกสาร WSDL นี้ให้ผู้อ่านได้เห็นภาพดังนี้
class glossaryTerm {
public function getTerm($term) {
return $value;
}
}
ซึ่ง input parameter ก็คือ part ที่ชื่อว่า term ในส่วนของ message "getTermRequest" โดยที่เวลาใช้งานจริง ๆ จะต้องระบุชื่อ input message ให้ถูกต้องรวมไปถึงชนิดของตัวแปรด้วย
output parameter ก็คือ part ที่ชื่อว่า value ในส่วนของ message "getTermResponse" โดยที่เวลาใช้งานจริง ๆ web service จะทำการส่งข้อมูลกลับไปเป็นชนิด string ตามที่ได้ระบุไว้เช่นกัน
Login
Search