邦友antijava的XML系列,看了之後,興起了想要實驗的念頭.
最近裝了有名的libxml2,裏面有附小工具,xmllint.
$ xmllint --valid insideDTD.xml
<?xml version="1.0"?>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
Don't forget me this weekend
</note>
以上是格式良好(well form)的情況.
$ xmllint --valid insideDTD.xml
insideDTD.xml:15: element note: <strong>validity error</strong> : Element note content does not follow the DTD, expecting (to , from , heading , body , comment), <strong>got (to from heading body )</strong>
</note>
^
<?xml version="1.0"?>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT <strong>comment</strong> (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
Don't forget me this weekend
</note>
粗體字的部分,因為多定義了一個欄位,導致valid(有效)檢查ERROR.
小結:emacs可編xml文件.<>不對稱時,會秀出紅色.
xmllint (line tool)還支援shell互動模式,可惜筆者,搞參數
(OPTION)就搞不出名堂.網上大多是libxml2程式庫的使用,對這個工具xmllint著墨很少.有想到一些實驗,實作不出來.