DTDの作成
xmllintのインストール
まずはIgor's WebhomeのLibxml Binariesからlibxmlとiconvをダウンロードします.2003.2.6時点での最新バージョンは2.5.1と1.8なので,ダウンロードするファイルはlibxml2-2.5.1.win32.zipとiconv-1.8.win32.zipです.
ダウンロードしたlibxml2-2.5.1-win32.zipの中からxmllint.exeとlibxml2.dll,iconv-1.8.win32.zipの中からiconv.dllを適当なディレクトリにコピーします.libxml2.dllとiconv.dllはパスが通っているディレクトリであればどこでもかまいません.今回は3ファイル全てをc:\program files\xmllintにコピーしました.
正しくインストールができていれば,コマンドプロンプトから以下のように打ち込むと,このような結果を得ることかできます.
$ xmllint --version c:\Program Files\xmllint\xmllint.exe: using libxml version 20501 compiled with: FTP HTTP HTML C14N Catalog DocBook XPath XPointer XInclude Iconv Unicode Regexps Automata Schemas
整形式のXML
例として,トップページのXMLを検証するDTDを作成します.トップページのXMLは整形式のXMLで,2003.2.2時点で以下のようになっています.#ちょくちょく変わります.
---index.xml--- <?xml version="1.0" encoding="utf-8"?> <root title="眠る猫の頁"> <img width="333" height="250" src="cat.jpg" alt=""/> <content href="whatsnew/index.html" name="新着"> 2003.2.2 <ul> <li><a href="saijiki/index.html">歳時記</a>に2003.1.26から2003.2.1までを追加.</li> <li><a href="biboroku/index.html">備忘録</a>のUNIXに「文字列を数える」を追加.</li> </ul> </content> <content href="saijiki/index.html" name="歳時記"> 日々の出来事など. </content> <content href="ryokoki/index.html" name="旅行記"> 旅先での写真や駄文を. </content> <content href="hyoryuki/index.html" name="漂流記"> システム開発への私見です. </content> <content href="biboroku/index.html" name="備忘録"> 教えてもらったことやできたことを忘れないうちに. </content> <content href="zatsuroku/index.html" name="雑録"> その他雑多なものが色々. </content> <content href="links/index.html" name="リンク"> 友人・知合い,便利なソフトウェア,参考になるサイト等. </content> <p>「眠る猫の頁」にリンクを張りたい方は<a href="about.html">リンクについて</a>へ.</p> <ancher href="http://www.nakka.com/wwwc/"> <banner src="http://www.nakka.com/wwwc/wwwc_meta.png" alt="WWWC META Check"/> </ancher> <ancher href="http://validator.w3.org/check?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/valid-xhtml11.png" alt="Valid XHTML 1.1!"/> </ancher> <ancher href="http://jigsaw.w3.org/css-validator/validator?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/vcss.png" alt="Valid CSS!" /> </ancher> </root> ---end of index.xml---
内部サブセット
index.xmlの内部にDTDを記述します.<!DOCTYPE
から]>
までがDTDです.
---index.xml--- <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE root [ <!ELEMENT root (img,content+,p,ancher*)> <!ATTLIST root title CDATA #REQUIRED> <!ELEMENT img EMPTY> <!ATTLIST img width CDATA #REQUIRED height CDATA #REQUIRED src CDATA #REQUIRED alt CDATA #REQUIRED> <!ELEMENT content (#PCDATA|ul)*> <!ATTLIST content href CDATA #REQUIRED name CDATA #REQUIRED> <!ELEMENT ul (li+)> <!ELEMENT li (#PCDATA|a)*> <!ELEMENT a (#PCDATA)> <!ATTLIST a href CDATA #REQUIRED> <!ELEMENT p (#PCDATA|a)*> <!ELEMENT ancher (banner)> <!ATTLIST ancher href CDATA #REQUIRED> <!ELEMENT banner EMPTY> <!ATTLIST banner src CDATA #REQUIRED alt CDATA #REQUIRED> ]> <root title="眠る猫の頁"> <img width="333" height="250" src="cat.jpg" alt=""/> <content href="whatsnew/index.html" name="新着"> 2003.2.2 <ul> <li><a href="saijiki/index.html">歳時記</a>に2003.1.26から2003.2.1までを追加.</li> <li><a href="biboroku/index.html">備忘録</a>のUNIXに「文字列を数える」を追加.</li> </ul> </content> <content href="saijiki/index.html" name="歳時記"> 日々の出来事など. </content> <content href="ryokoki/index.html" name="旅行記"> 旅先での写真や駄文を. </content> <content href="hyoryuki/index.html" name="漂流記"> システム開発への私見です. </content> <content href="biboroku/index.html" name="備忘録"> 教えてもらったことやできたことを忘れないうちに. </content> <content href="zatsuroku/index.html" name="雑録"> その他雑多なものが色々. </content> <content href="links/index.html" name="リンク"> 友人・知合い,便利なソフトウェア,参考になるサイト等. </content> <p>「眠る猫の頁」にリンクを張りたい方は<a href="about.html">リンクについて</a>へ.</p> <ancher href="http://www.nakka.com/wwwc/"> <banner src="http://www.nakka.com/wwwc/wwwc_meta.png" alt="WWWC META Check"/> </ancher> <ancher href="http://validator.w3.org/check?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/valid-xhtml11.png" alt="Valid XHTML 1.1!"/> </ancher> <ancher href="http://jigsaw.w3.org/css-validator/validator?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/vcss.png" alt="Valid CSS!" /> </ancher> </root> ---end of index.xml---
xmllintで検証するには以下のようにコマンドを打ち込みます.
$ xmllint --noout --valid index.xml
画面に何も出なければ検証完了です.エラーがあった場合は,例えばこんな表示になります.widthをwdthと書き間違えてるため,wdth属性がDTD未定義となっています.
$ xmllint --noout --valid index.xml index.xml:33: validity error: No declaration for attribute wdth of element img <img wdth="333" height="250" src="cat.jpg" alt=""/> ^ index.xml:33: validity error: Element img does not carry attribute width <img wdth="333" height="250" src="cat.jpg" alt=""/> ^
外部サブセット
DTDを別ファイルにします.
---index.dtd--- <!ELEMENT root (img,content+,p,ancher*)> <!ATTLIST root title CDATA #REQUIRED> <!ELEMENT img EMPTY> <!ATTLIST img width CDATA #REQUIRED height CDATA #REQUIRED src CDATA #REQUIRED alt CDATA #REQUIRED> <!ELEMENT content (#PCDATA|ul)*> <!ATTLIST content href CDATA #REQUIRED name CDATA #REQUIRED> <!ELEMENT ul (li+)> <!ELEMENT li (#PCDATA|a)*> <!ELEMENT a (#PCDATA)> <!ATTLIST a href CDATA #REQUIRED> <!ELEMENT p (#PCDATA|a)*> <!ELEMENT ancher (banner)> <!ATTLIST ancher href CDATA #REQUIRED> <!ELEMENT banner EMPTY> <!ATTLIST banner src CDATA #REQUIRED alt CDATA #REQUIRED> ---end of index.dtd---
---index.xml--- <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE root SYSTEM "index.dtd"> <!-- 外部サブセットの指定 --> <root title="眠る猫の頁"> <img width="333" height="250" src="cat.jpg" alt=""/> <content href="whatsnew/index.html" name="新着"> 2003.2.2 <ul> <li><a href="saijiki/index.html">歳時記</a>に2003.1.26から2003.2.1までを追加.</li> <li><a href="biboroku/index.html">備忘録</a>のUNIXに「文字列を数える」を追加.</li> </ul> </content> <content href="saijiki/index.html" name="歳時記"> 日々の出来事など. </content> <content href="ryokoki/index.html" name="旅行記"> 旅先での写真や駄文を. </content> <content href="hyoryuki/index.html" name="漂流記"> システム開発への私見です. </content> <content href="biboroku/index.html" name="備忘録"> 教えてもらったことやできたことを忘れないうちに. </content> <content href="zatsuroku/index.html" name="雑録"> その他雑多なものが色々. </content> <content href="links/index.html" name="リンク"> 友人・知合い,便利なソフトウェア,参考になるサイト等. </content> <p>「眠る猫の頁」にリンクを張りたい方は<a href="about.html">リンクについて</a>へ.</p> <ancher href="http://www.nakka.com/wwwc/"> <banner src="http://www.nakka.com/wwwc/wwwc_meta.png" alt="WWWC META Check"/> </ancher> <ancher href="http://validator.w3.org/check?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/valid-xhtml11.png" alt="Valid XHTML 1.1!"/> </ancher> <ancher href="http://jigsaw.w3.org/css-validator/validator?uri=http://www.dab.hi-ho.ne.jp/sasa/"> <banner src="links/banner/vcss.png" alt="Valid CSS!" /> </ancher> </root> ---end of index.xml---
検証方法は内部サブセットと同じです.
$ xmllint --noout --valid index.xml
画面に何も出なければ検証完了です.
参考サイト
参考文献
- 株式会社日本ユニテック,改訂版 標準XML完全解説(上),技術評論社