xmllint学習 #6 helpを日本語化-4日目

2019年7月10日

xmllint テクノロジー プログラミング 特集

XMLって何の略かというと「Extensible Markup Language」の略です。 ん??? ・・・ eml??? 日本人はピンと来ない人も多いかもしれませんが、exはxとするのが英語圏の常識なのですね・・・ なので頭文字の「XML」なんですね。 wikipediaからの説明を引用すると、下記のように書かれていました。
基本的な構文規則を共通とすることで、任意の用途向けの言語に拡張することを容易としたことが特徴のマークアップ言語の総称である。 一般的にXML(エックスエムエル)と略称で呼ばれる。 JISによる訳語は「拡張可能なマーク付け言語」と定義している。
https://ja.wikipedia.org/wiki/Extensible_Markup_Language そんなxmlを扱う便利コマンド「xmllint」のヘルプ調査も4日目に突入です。

--maxmem nbbytes

limits memory allocation to nbbytes bytes メモリ割り当てをnbbytesバイトに制限する $ xmllint sample.xml --maxmem 1024 Ran out of memory needs > 1024 bytes parser error : Memory allocation failed : cannot initialize parser context parser error : Memory allocation failed : cannot allocate parser context Memory tag error occurs :0x55a8d4570098 bye xmlMemFree(55A8D45700C0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d45700b8 bye xmlMemFree(55A8D45700E0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d4570038 bye xmlMemFree(55A8D4570060) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d4570058 bye xmlMemFree(55A8D4570080) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456ffd8 bye xmlMemFree(55A8D4570000) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fff8 bye xmlMemFree(55A8D4570020) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456ff78 bye xmlMemFree(55A8D456FFA0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456ff98 bye xmlMemFree(55A8D456FFC0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456ff18 bye xmlMemFree(55A8D456FF40) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456ff38 bye xmlMemFree(55A8D456FF60) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456feb8 bye xmlMemFree(55A8D456FEE0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fed8 bye xmlMemFree(55A8D456FF00) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fe58 bye xmlMemFree(55A8D456FE80) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fe78 bye xmlMemFree(55A8D456FEA0) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fdf8 bye xmlMemFree(55A8D456FE20) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fe18 bye xmlMemFree(55A8D456FE40) error xmlMallocBreakpoint reached on block 0 Memory tag error occurs :0x55a8d456fc58 bye xmlMemFree(55A8D456FC80) error xmlMallocBreakpoint reached on block 0 出力制限か読み込み制限をしてくれるのかと思いきや、よくわからないエラーの山で意気消沈です・・・orz MallocBreakPointと怒られているので、メモリポイントをちゃんとしないといけないようです。

--nowarning

do not emit warnings from parser/validator パーサ/バリデータから警告を出さない $ xmllint sample.xml --nowarning <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> 警告を出さないということで、通常表示以外に何も表示されないことが正解なのかも・・・

--noblanks

drop (ignorable?) blanks spaces スペースを削除する(無視できる?) $ xmllint sample.xml --noblanks <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge><hage name="aaa" value="12345" a="1">DOG</hage><hage name="bbb" value="56789" b="2">CAT</hage></hoge> trimのような機能です。 不要なスペースやタブ、解消などを取り除いてくれるようです。 これは結構使うかも。

--nocdata

replace cdata section with text nodes cdataセクションをテキストノードに置き換えます $ xmllint sample_cdata.xml <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> <![CDATA[ <?xml version="1.0"?> <document> Kitty on your lap & Tokyo mew mew </document> ]]> </hoge> $ xmllint sample_cdata.xml --nocdata <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> &lt;?xml version="1.0"?&gt; &lt;document&gt; Kitty on your lap &amp; Tokyo mew mew &lt;/document&gt; </hoge> cdataセクションの文字列をurlエスケープしてくれるようですね。

--format

reformat/reindent the output 出力を再フォーマット/再インデント $ xmllint sample.xml <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> $ xmllint sample.xml --format <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> この違い分かります? タブ数が半角スペース2個から4個に変わってます。 短いデータなのでこれぐらいでしたが、キレイなフォーマットに変換したい場合に便利そうです。

--encode encoding

output in the given encoding 与えられたエンコーディングで出力する $ xmllint sample_encode.xml --encode utf-8 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">犬</hage> <hage name="bbb" value="56789" b="2">猿</hage> <hage name="ccc" value="00000" b="2">キジ</hage> </hoge> $ xmllint sample_encode.xml --encode shift-jis <?xml version="1.0" encoding="shift-jis" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">??</hage> <hage name="bbb" value="56789" b="2">??</hage> <hage name="ccc" value="00000" b="2">?L?W</hage> </hoge> $ xmllint sample_encode.xml --encode euc-jp <?xml version="1.0" encoding="euc-jp" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">??</hage> <hage name="bbb" value="56789" b="2">??</hage> <hage name="ccc" value="00000" b="2">????</hage> </hoge> その環境にあったエンコードに切り替えてくれるので便利に使えそうですが、元データはutf-8である必要があるようです。

--dropdtd

remove the DOCTYPE of the input docs 入力ドキュメントのDOCTYPEを削除します $ xmllint --html http://myntinc.com --dropdtd <html lang="ja"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <title>Mynt,Inc.</title> <meta name="description" content="IT,WEB,&#12503;&#12525;&#12464;&#12521;&#12512;,&#12469;&#12540;&#12499;&#12473;,&#38651;&#23376;&#24037;&#20316;,IoT,DIY,&#12507;&#12540;&#12512;&#12506;&#12540;&#12472;&#21046;&#20316;"> <meta name="author" content="MYNT,Inc."> <meta http-equiv="Expires" content="0"> <link rel="icon" type="image/png" href="favicon.png" sizes="96x96"> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-105017486-1', 'auto'); ga('send', 'pageview'); </script> <link rel="stylesheet" href="css/common.css"> <!-- <link href="https://fonts.googleapis.com/css?family=Sawarabi+Gothic" rel="stylesheet"> --> <script type="text/javascript" src="js/ef.js"></script> <script src="js/ajax.js"></script> <script src="js/common.js"></script> </head> <body> <div class="banner"> <ul class="base"> <li class="ef" data-link="index"><div class="title">MYNT,Inc.</div></li> <li class="ef" data-link="about"><div class="title">About</div></li> <li class="ef" data-link="service"><div class="title">Service</div></li> <li class="ef" data-link="work"><div class="title">Work</div></li> <li class="ef" data-link="contact"><div class="title">Contact</div></li> </ul> </div> <div id="contents"></div> <div id="footer"><script>var ajax = new $$MYNT_AJAX;ajax.loadHTML("html/footer.html","#footer")</script></div> </body> </html> 1行目にあるはずのDOCTYPEが消えています。 データのやり取りで不要な場合に使えそうです。

--pretty STYLE

pretty-print in a particular style 0 Do not pretty print 1 Format the XML content, as --format 2 Add whitespace inside tags, preserving content 特定のスタイルできれいに印刷 0 きれいに印刷しない 1 XMLコンテンツを--formatとしてフォーマットします。 2 コンテンツを維持しながら、タグ内に空白を追加する $ xmllint sample.xml <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> $ xmllint sample.xml --pretty 0 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> $ xmllint sample.xml --pretty 1 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge> <hage name="aaa" value="12345" a="1">DOG</hage> <hage name="bbb" value="56789" b="2">CAT</hage> </hoge> $ xmllint sample.xml --pretty 2 <?xml version="1.0" encoding="utf-8" standalone="yes"?> <hoge > <hage name="aaa" value="12345" a="1" >DOG</hage > <hage name="bbb" value="56789" b="2" >CAT</hage > </hoge > 2番・・・・使うことあるかな・・・・???

--c14n

save in W3C canonical format v1.0 (with comments) W3C標準フォーマットv1.0で保存(コメント付き) $ xmllint sample.xml --c14n <hoge> <hage a="1" name="aaa" value="12345">DOG</hage> <hage b="2" name="bbb" value="56789">CAT</hage> </hoge> ヘッダ情報のxmlタグは消えるんですね。

--c14n11

save in W3C canonical format v1.1 (with comments) W3C標準フォーマットv1.1で保存(コメント付き) $ xmllint sample.xml --c14n11 <hoge> <hage a="1" name="aaa" value="12345">DOG</hage> <hage b="2" name="bbb" value="56789">CAT</hage> </hoge> 1.0と1.1の違いはそんなに大きくないですが、コンバート処理として使えそうです。

--exc-c14n

save in W3C exclusive canonical format (with comments) W3C独自の正規形式で保存する(コメント付き) $ xmllint sample.xml --exc-c14n <hoge> <hage a="1" name="aaa" value="12345">DOG</hage> <hage b="2" name="bbb" value="56789">CAT</hage> </hoge> 何が独自なのかよくわからないので、使えなそうです・・・

後半突入

ようやく終りが見えてきました。 あと2回ぐらいで、全てのオプション調査が完了します。 もうしばらくお付き合いください。

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。