ตัวจัดรูปแบบ XML
จัดรูปแบบและทำให้ XML สวยงามพร้อมการเน้นไวยากรณ์
ป้อนข้อมูล XML
XML ที่จัดรูปแบบแล้ว
XML Formatting คืออะไร?
XML formatting (หรือที่เรียกว่า XML pretty-printing หรือ XML beautification) คือกระบวนการเพิ่มการย่อหน้าและการขึ้นบรรทัดใหม่ที่สม่ำเสมอให้กับเอกสาร XML เพื่อให้โครงสร้างแบบลำดับชั้นมองเห็นได้ชัดเจน XML ดิบจาก API ตัวสร้าง config หรือ serializer มักถูกส่งมาในบรรทัดเดียวโดยไม่มีช่องว่างระหว่าง tag ตัวจัดรูปแบบ XML จะแยกวิเคราะห์เอกสารนั้นเป็น tree แล้ว serialize ใหม่พร้อมระยะห่างที่คาดเดาได้
ข้อกำหนด XML 1.0 (W3C Recommendation ฉบับที่ห้า) กำหนด grammar ที่เข้มงวด โดย opening tag ทุกตัวต้องมี closing tag ที่ตรงกันหรือเป็น self-closing, attribute ต้องอยู่ในเครื่องหมายอัญประกาศ และอักขระห้าตัว (<, >, &, ", ') ต้องใช้ entity escaping ตัวจัดรูปแบบต้องปฏิบัติตามกฎเหล่านี้ขณะแทรกเฉพาะช่องว่างที่ไม่มีนัยสำคัญซึ่งไม่เปลี่ยนแปลง information set (infoset) ของเอกสาร
XML ที่จัดรูปแบบแล้วอ่านง่ายขึ้นระหว่าง code review, diff ง่ายขึ้นใน version control และ debug ได้ง่ายขึ้นเมื่อ service ส่งข้อมูลที่ไม่คาดคิด การดำเนินการจัดรูปแบบนั้น lossless: เนื้อหาเชิงตรรกะของเอกสารยังคงเหมือนเดิม เปลี่ยนเพียงการนำเสนอ เครื่องมืออย่าง git diff และแพลตฟอร์ม pull-request review จะแสดงเฉพาะบรรทัดที่แก้ไข — XML ที่ย่อหน้าสม่ำเสมอทำให้ diff เหล่านั้นสะท้อนการเปลี่ยนแปลงจริง ไม่ใช่แค่การจัดรูปแบบใหม่
<catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date></book></catalog>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>ทำไมต้องใช้ตัวจัดรูปแบบ XML ออนไลน์?
การจัดรูปแบบ XML ด้วยตนเองเกิดข้อผิดพลาดได้ง่ายและใช้เวลานาน โดยเฉพาะเอกสารที่มีการซ้อนลึกหรือ namespace ผสมกัน ตัวจัดรูปแบบบนเบราว์เซอร์ให้ผลลัพธ์ที่จัดรูปแบบแล้วในไม่กี่วินาที ไม่ว่าไฟล์จะมีขนาดเท่าใด
กรณีการใช้งานตัวจัดรูปแบบ XML
ตารางอ้างอิง Entity ที่กำหนดไว้ล่วงหน้าใน XML
XML สงวนอักขระห้าตัวไว้สำหรับไวยากรณ์ของตน เมื่ออักขระเหล่านี้ปรากฏเป็นเนื้อหาข้อความหรือค่า attribute ต้องแทนที่ด้วย entity reference ที่กำหนดไว้ล่วงหน้า ตัวจัดรูปแบบที่ถูกต้องจะรักษา entity เหล่านี้ระหว่าง pretty-printing
| อักขระ | บทบาทใน XML | Entity |
|---|---|---|
| < | Start of tag | < |
| > | End of tag | > |
| & | Start of entity | & |
| " | Attribute delimiter | " |
| ' | Attribute delimiter | ' |
การเปรียบเทียบรูปแบบการย่อหน้าใน XML
ไม่มีมาตรฐานเดียวสำหรับการย่อหน้า XML การเลือกขึ้นอยู่กับข้อตกลงของทีมและเครื่องมือใน pipeline นี่คือสามรูปแบบที่พบบ่อยที่สุด
ตัวอย่างโค้ด: จัดรูปแบบ XML ด้วยโปรแกรม
เมื่อต้องการจัดรูปแบบ XML ภายใน script หรือกระบวนการ build ทุกภาษาหลักมีตัวเลือกในตัวหรือ standard library ด้านล่างคือตัวอย่างที่ใช้งานได้ซึ่งคุณสามารถคัดลอกไปใช้ได้โดยตรง
const raw = '<root><item>hello</item></root>'
const parser = new DOMParser()
const doc = parser.parseFromString(raw, 'application/xml')
const serializer = new XMLSerializer()
const xml = serializer.serializeToString(doc)
// Indent with XSLT (browser-native approach)
const xslt = new DOMParser().parseFromString(`
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>`, 'application/xml')
const proc = new XSLTProcessor()
proc.importStylesheet(xslt)
const formatted = proc.transformToDocument(doc)
console.log(new XMLSerializer().serializeToString(formatted))
// → <root>\n <item>hello</item>\n</root>import xml.dom.minidom raw = '<root><item>hello</item><item>world</item></root>' dom = xml.dom.minidom.parseString(raw) print(dom.toprettyxml(indent=' ')) # → <?xml version="1.0" ?> # → <root> # → <item>hello</item> # → <item>world</item> # → </root> # With lxml (handles namespaces, XSD, large files) from lxml import etree tree = etree.fromstring(raw.encode()) print(etree.tostring(tree, pretty_print=True).decode())
package main
import (
"encoding/xml"
"fmt"
"strings"
)
func formatXML(raw string) (string, error) {
decoder := xml.NewDecoder(strings.NewReader(raw))
var out strings.Builder
encoder := xml.NewEncoder(&out)
encoder.Indent("", " ")
for {
tok, err := decoder.Token()
if err != nil {
break
}
encoder.EncodeToken(tok)
}
encoder.Flush()
return out.String(), nil
}
// formatXML("<a><b>1</b></a>") → "<a>\n <b>1</b>\n</a>"# Format XML file with xmllint (part of libxml2, pre-installed on macOS/Linux) xmllint --format input.xml > formatted.xml # Format from stdin echo '<a><b>1</b></a>' | xmllint --format - # → <?xml version="1.0"?> # → <a> # → <b>1</b> # → </a> # Validate and format at the same time xmllint --format --schema schema.xsd input.xml