ตัวตรวจสอบ XML
ตรวจสอบไวยากรณ์ XML และข้อผิดพลาด well-formedness
ป้อนข้อมูล XML
XML Validation คืออะไร?
ตัวตรวจสอบ XML ตรวจสอบว่าเอกสาร XML ปฏิบัติตามกฎโครงสร้างที่กำหนดโดยข้อกำหนด XML 1.0 (W3C Recommendation ฉบับที่ห้า) หรือไม่ อย่างน้อยที่สุด ตัวตรวจสอบนี้ยืนยันว่าเอกสารเป็น well-formed: มี root element เพียงหนึ่งตัว ทุก tag ซ้อนกันและปิดอย่างถูกต้อง ค่า attribute อยู่ในเครื่องหมายอัญประกาศ และอักขระสงวนใช้ entity ที่กำหนดไว้ล่วงหน้า เอกสารที่ล้มเหลวในการตรวจสอบเหล่านี้จะทำให้ XML parser เกิดข้อผิดพลาดแทนที่จะสร้างผลลัพธ์ที่ผิดพลาดอย่างเงียบๆ
ความแตกต่างระหว่าง well-formedness และ validity มีความสำคัญ เอกสาร XML ที่ well-formed ปฏิบัติตามกฎไวยากรณ์ของข้อกำหนด XML เอง เอกสาร XML ที่ valid ไปไกลกว่านั้น: ยังสอดคล้องกับข้อจำกัดที่กำหนดในสคีมาภายนอก เช่น DTD (Document Type Definition), XSD (XML Schema Definition) หรือ Relax NG schema เครื่องมือนี้ตรวจสอบ well-formedness ซึ่งเป็นขั้นตอนการตรวจสอบขั้นแรกและพบบ่อยที่สุด หากขาดขั้นตอนนี้ ทุกอย่างในกระบวนการต่อไปจะดำเนินการไม่ได้
XML validation ตรวจจับข้อผิดพลาดก่อนที่จะถึง production แท็กที่ยังไม่ปิด ชื่อ element ที่ไม่ตรงกัน ampersand ที่ไม่ได้ escape และ attribute ที่ซ้ำกันเป็นข้อผิดพลาดที่พบบ่อยที่สุดใน XML ที่แก้ไขด้วยมือ Parser ในภาษาต่างๆ จัดการข้อผิดพลาดเหล่านี้แตกต่างกัน: บางตัวล้มเหลวอย่างเงียบๆ บางตัวคืนค่าผลลัพธ์บางส่วน และบางตัวโยน exception การรัน XML ผ่านตัวตรวจสอบก่อนขจัดความกำกวมนี้และให้คำตอบที่ชัดเจนว่าผ่านหรือไม่ผ่านพร้อมรายละเอียดตำแหน่งข้อผิดพลาด
ทำไมต้องใช้ตัวตรวจสอบ XML ออนไลน์?
การตรวจจับข้อผิดพลาดไวยากรณ์ XML ตั้งแต่เนิ่นๆ ป้องกันความล้มเหลวแบบลูกโซ่ในแอปพลิเคชันที่ใช้ข้อมูล XML ตัวตรวจสอบบนเบราว์เซอร์ให้ผลตอบกลับทันทีโดยไม่ต้องติดตั้ง command-line tool หรือกำหนดค่า IDE plugin
กรณีการใช้งานตัวตรวจสอบ XML
กฎ Well-Formedness ของ XML
ข้อกำหนด XML 1.0 กำหนดชุดกฎไวยากรณ์ที่เข้มงวด เอกสารต้องปฏิบัติตามกฎทั้งหมดจึงจะถือว่าเป็น well-formed ตารางด้านล่างแสดงแต่ละกฎ สิ่งที่กำหนด และตัวอย่างที่ถูกต้อง ข้อผิดพลาดในการตรวจสอบส่วนใหญ่เกิดจากกฎเหล่านี้
| กฎ | ข้อกำหนด | ตัวอย่างที่ถูกต้อง |
|---|---|---|
| Single root element | Document must have exactly one root | <root>...</root> |
| Matched tags | Every opening tag needs a closing tag | <p>text</p> |
| Proper nesting | Tags must close in reverse order of opening | <a><b>...</b></a> |
| Quoted attributes | Attribute values must be in single or double quotes | <el attr="val"/> |
| Entity escaping | Reserved characters must use predefined entities | < > & " ' |
| Case sensitivity | Tag names are case-sensitive: <A> is not </a> | <Book>...</Book> |
| No duplicate attributes | Each attribute name must appear once per element | <el a="1" b="2"/> |
| Valid XML declaration | If present, must be the very first line | <?xml version="1.0"?> |
Well-Formed vs. DTD-Valid vs. Schema-Valid
XML validation มีสามระดับ เครื่องมือนี้ตรวจสอบระดับแรก (well-formedness) ซึ่งเป็นเงื่อนไขเบื้องต้นสำหรับอีกสองระดับ
ตัวอย่างโค้ด
ตรวจสอบ XML ด้วยโปรแกรมในภาษาต่างๆ แต่ละตัวอย่างตรวจสอบ well-formedness และคืนค่าข้อความ error หากเอกสาร malformed
function validateXML(xmlString) {
const parser = new DOMParser()
const doc = parser.parseFromString(xmlString, 'application/xml')
const error = doc.querySelector('parsererror')
if (error) {
return { valid: false, message: error.textContent }
}
return { valid: true, message: 'Well-formed XML' }
}
validateXML('<root><item>hello</item></root>')
// → { valid: true, message: "Well-formed XML" }
validateXML('<root><item>hello</root>')
// → { valid: false, message: "Opening and ending tag mismatch..." }import xml.etree.ElementTree as ET
def validate_xml(xml_string):
try:
ET.fromstring(xml_string)
return True, "Well-formed XML"
except ET.ParseError as e:
return False, str(e)
valid, msg = validate_xml('<root><item>hello</item></root>')
# → (True, "Well-formed XML")
valid, msg = validate_xml('<root><item>hello</root>')
# → (False, "mismatched tag: line 1, column 27")
# With lxml — also supports XSD schema validation
from lxml import etree
schema = etree.XMLSchema(etree.parse('schema.xsd'))
doc = etree.fromstring(b'<root><item>hello</item></root>')
schema.validate(doc) # → True or Falsepackage main
import (
"encoding/xml"
"fmt"
"strings"
)
func validateXML(raw string) error {
decoder := xml.NewDecoder(strings.NewReader(raw))
for {
_, err := decoder.Token()
if err != nil {
if err.Error() == "EOF" {
return nil
}
return err
}
}
}
func main() {
err := validateXML("<root><item>hello</item></root>")
fmt.Println(err) // → <nil>
err = validateXML("<root><item>hello</root>")
fmt.Println(err) // → XML syntax error: unexpected end element </root>
}# Check well-formedness (part of libxml2, pre-installed on macOS/Linux) xmllint --noout document.xml # Exit code 0 = valid, non-zero = errors printed to stderr # Validate against an XSD schema xmllint --noout --schema schema.xsd document.xml # Validate against a DTD xmllint --noout --dtdvalid schema.dtd document.xml # Validate from stdin echo '<root><unclosed>' | xmllint --noout - # → :1: parser error : Premature end of data in tag unclosed line 1