ข้อจำกัด XSD /แง่มุม
ข้อจำกัดใช้เพื่อกำหนดค่าที่ยอมรับได้สำหรับองค์ประกอบ XML หรือแอตทริบิวต์ ข้อจำกัดในองค์ประกอบ XML เรียกว่า facet
ข้อจำกัดของค่า
ตัวอย่างต่อไปนี้กำหนดองค์ประกอบที่เรียกว่า "อายุ" โดยมีข้อจำกัด ค่าของอายุต้องไม่ต่ำกว่า 0 หรือมากกว่า 120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ข้อจำกัดในชุดของค่า
เพื่อจำกัดเนื้อหาขององค์ประกอบ XML เป็นชุดของค่าที่ยอมรับได้ เราจะใช้ข้อจำกัดการแจงนับ
ตัวอย่างด้านล่างกำหนดองค์ประกอบที่เรียกว่า "รถยนต์" โดยมีข้อจำกัด ค่าที่ยอมรับได้เท่านั้นคือ: Audi, Golf, BMW:
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างข้างต้นสามารถเขียนได้ดังนี้:
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
หมายเหตุ:ในกรณีนี้ องค์ประกอบอื่นสามารถใช้ประเภท "carType" ได้ เนื่องจากไม่ได้เป็นส่วนหนึ่งขององค์ประกอบ "car"
ข้อจำกัดในชุดของค่า
เพื่อจำกัดเนื้อหาขององค์ประกอบ XML เพื่อกำหนดชุดของตัวเลขหรือตัวอักษรที่สามารถใช้ได้ เราจะใช้ข้อจำกัดรูปแบบ
ตัวอย่างด้านล่างกำหนดองค์ประกอบที่เรียกว่า "จดหมาย" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือหนึ่งในตัวอักษร LOWERCASE จาก a ถึง z:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปกำหนดองค์ประกอบที่เรียกว่า "ชื่อย่อ" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือตัวอักษรตัวพิมพ์ใหญ่สามตัวจาก a ถึง z:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปยังกำหนดองค์ประกอบที่เรียกว่า "ชื่อย่อ" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือตัวอักษร LOWERCASE OR UPPERCASE สามตัวจาก a ถึง z:
<xs:element name="initials">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปกำหนดองค์ประกอบที่เรียกว่า "ทางเลือก" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือหนึ่งในตัวอักษรต่อไปนี้: x, y, OR z:
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปกำหนดองค์ประกอบที่เรียกว่า "prodid" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือตัวเลขห้าหลักในลำดับ และแต่ละหลักต้องอยู่ในช่วงตั้งแต่ 0 ถึง 9:
<xs:element name="prodid">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ข้อจำกัดอื่นๆ เกี่ยวกับชุดค่านิยม
ตัวอย่างด้านล่างกำหนดองค์ประกอบที่เรียกว่า "จดหมาย" โดยมีข้อจำกัด ค่าที่ยอมรับได้คือการเกิดตัวอักษรพิมพ์เล็กตั้งแต่ a ถึง z เป็นศูนย์หรือมากกว่า:
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปยังกำหนดองค์ประกอบที่เรียกว่า "จดหมาย" โดยมีข้อจำกัด ค่าที่ยอมรับได้คือตัวอักษรหนึ่งคู่หรือมากกว่า โดยแต่ละคู่ประกอบด้วยอักษรตัวพิมพ์เล็กตามด้วยอักษรตัวพิมพ์ใหญ่ ตัวอย่างเช่น "sToP" จะถูกตรวจสอบโดยรูปแบบนี้ แต่ไม่ใช่ "Stop" หรือ "STOP" หรือ "stop":
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปกำหนดองค์ประกอบที่เรียกว่า "เพศ" โดยมีข้อจำกัด ค่าเดียวที่ยอมรับได้คือชายหรือหญิง:
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างต่อไปกำหนดองค์ประกอบที่เรียกว่า "รหัสผ่าน" โดยมีข้อจำกัด ในแถวต้องมีอักขระแปดตัวพอดี และอักขระเหล่านั้นต้องเป็นตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่ตั้งแต่ a ถึง z หรือตัวเลขตั้งแต่ 0 ถึง 9:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9]{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ข้อจำกัดของอักขระช่องว่าง
ในการระบุวิธีจัดการอักขระช่องว่าง เราจะใช้ข้อจำกัด whiteSpace
ตัวอย่างนี้กำหนดองค์ประกอบที่เรียกว่า "ที่อยู่" โดยมีข้อจำกัด ข้อจำกัด whiteSpace ถูกตั้งค่าเป็น "รักษา" ซึ่งหมายความว่าตัวประมวลผล XML จะไม่ลบอักขระช่องว่างใดๆ:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างนี้ยังกำหนดองค์ประกอบที่เรียกว่า "ที่อยู่" โดยมีข้อจำกัด ข้อจำกัด whiteSpace ถูกตั้งค่าเป็น "replace" ซึ่งหมายความว่าตัวประมวลผล XML จะแทนที่อักขระ white space ทั้งหมด (การป้อนบรรทัด แท็บ ช่องว่าง และการขึ้นบรรทัดใหม่) ด้วยช่องว่าง:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างนี้ยังกำหนดองค์ประกอบที่เรียกว่า "ที่อยู่" โดยมีข้อจำกัด ข้อจำกัด whiteSpace ถูกตั้งค่าเป็น "ยุบ" ซึ่งหมายความว่าตัวประมวลผล XML จะลบอักขระช่องว่างทั้งหมด (การป้อนบรรทัด แท็บ ช่องว่าง การขึ้นบรรทัดใหม่จะถูกแทนที่ด้วยช่องว่าง ช่องว่างนำหน้าและต่อท้ายจะถูกลบออก และลดช่องว่างหลายช่องเป็น ช่องว่างเดียว):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ข้อจำกัดเรื่องความยาว
ในการจำกัดความยาวของค่าในองค์ประกอบ เราจะใช้ข้อจำกัดด้านความยาว maxLength และ minLength
ตัวอย่างนี้กำหนดองค์ประกอบที่เรียกว่า "รหัสผ่าน" โดยมีข้อจำกัด ค่าต้องเป็นอักขระแปดตัวพอดี:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ตัวอย่างนี้กำหนดองค์ประกอบอื่นที่เรียกว่า "รหัสผ่าน" โดยมีข้อจำกัด ค่าต้องมีอักขระอย่างน้อยห้าตัวและสูงสุดแปดอักขระ:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ข้อจำกัดสำหรับประเภทข้อมูล
Constraint | Description |
---|---|
enumeration | Defines a list of acceptable values |
fractionDigits | Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero |
length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero |
maxExclusive | Specifies the upper bounds for numeric values (the value must be less than this value) |
maxInclusive | Specifies the upper bounds for numeric values (the value must be less than or equal to this value) |
maxLength | Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero |
minExclusive | Specifies the lower bounds for numeric values (the value must be greater than this value) |
minInclusive | Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) |
minLength | Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero |
pattern | Defines the exact sequence of characters that are acceptable |
totalDigits | Specifies the exact number of digits allowed. Must be greater than zero |
whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |