การตรวจจับข้อผิดพลาดไวยากรณ์ XML ตั้งแต่เนิ่นๆ ป้องกันความล้มเหลวแบบลูกโซ่ในแอปพลิเคชันที่ใช้ข้อมูล XML ตัวตรวจสอบบนเบราว์เซอร์ให้ผลตอบกลับทันทีโดยไม่ต้องติดตั้ง command-line tool หรือกำหนดค่า IDE plugin
⚡
ตรวจจับข้อผิดพลาดได้ทันที
วาง XML แล้วรับผลลัพธ์ผ่านหรือไม่ผ่านพร้อมตำแหน่งข้อผิดพลาดแม่นยำภายในไม่ถึงวินาที ไม่ต้องรอ build pipeline หรือตั้งค่า CLI tool
🔒
ประมวลผลโดยให้ความเป็นส่วนตัวก่อน
การแยกวิเคราะห์และการตรวจสอบทั้งหมดเกิดขึ้นในเบราว์เซอร์โดยใช้ DOMParser API ข้อมูล 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) ซึ่งเป็นเงื่อนไขเบื้องต้นสำหรับอีกสองระดับ
Well-formed
ปฏิบัติตามกฎไวยากรณ์ XML 1.0: root element เดียว tag ที่ตรงกัน attribute ในเครื่องหมายอัญประกาศ และการซ้อนที่ถูกต้อง ทุก XML parser ตรวจสอบสิ่งนี้ก่อน หากเอกสารไม่ใช่ well-formed ก็ไม่ใช่ XML เลย
DTD-valid
สอดคล้องกับ Document Type Definition ที่ระบุว่า element และ attribute ใดได้รับอนุญาต ลำดับและจำนวน DTD ประกาศแบบ inline หรือผ่าน DOCTYPE reference DTD validation พบบ่อยในระบบเก่าและ XHTML
Schema-valid (XSD / Relax NG)
สอดคล้องกับ XML Schema Definition (XSD) หรือ Relax NG schema สคีมาเหล่านี้รองรับชนิดข้อมูล (integer, date, URI) namespace และ content model ที่ซับซ้อน XSD validation เป็นมาตรฐานใน SOAP web service ข้อมูลการแพทย์ HL7 และ enterprise integration
ตัวอย่างโค้ด
ตรวจสอบ XML ด้วยโปรแกรมในภาษาต่างๆ แต่ละตัวอย่างตรวจสอบ well-formedness และคืนค่าข้อความ error หากเอกสาร malformed
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 False
Go
package 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>
}
CLI (xmllint)
# 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
คำถามที่พบบ่อย
ความแตกต่างระหว่าง XML ที่ well-formed และ XML ที่ valid คืออะไร?
XML ที่ well-formed ปฏิบัติตามกฎไวยากรณ์ของข้อกำหนด XML 1.0: root element หนึ่งตัว tag ที่ตรงกันและซ้อนอย่างถูกต้อง attribute ในเครื่องหมายอัญประกาศ XML ที่ valid ไปไกลกว่าโดยยังสอดคล้องกับ DTD หรือ XSD schema ที่กำหนดว่า element, attribute และชนิดข้อมูลใดได้รับอนุญาต เอกสารอาจเป็น well-formed แต่ไม่ valid หากมีไวยากรณ์ที่ถูกต้องแต่ละเมิดข้อจำกัดของสคีมา
เอกสาร XML สามารถ valid แต่ไม่ well-formed ได้หรือไม่?