xmllintのxpathを使うと文字エンコードがバグる件

2020年3月10日

xmllint テクノロジー トラブル プログラミング

eyecatch 文字エンコードと掛けまして、 自家用車と、ときます。 そのココロは・・・ シフト(shift-jis)はもう古い。 ナンチャッテ・エンジニアのユゲタです。 先日、xmllintが便利だというブログを書いたんですが、使っていてどう考えてもバグを発見したので、お伝えします。 xmllint学習 #1 サーバーコマンドでxmlをパースできるxmllintが便利っぽい

事象

xmllintは、--encodeオプションで、文字エンコードを指定することができるんですが、 --xpathオプションを使うと、encodeが機能しないという事が判明しました。 サンプルとして、以下のxmlを読み込んでみました。 $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 - <?xml version="1.0" encoding="utf-8"?> <rss xmlns:itunes="http://www.itunes.com/DTDs/Podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" version="2.0"> <channel> <title>9chat-e ナンチャッテ・ラジオ</title> <link>http://9chat-e.mynt.work/</link> <description>プログラミング学習に役立つ情報を、ナンチャッテ・エンジニアがお伝えするラジオ番組</description> <language>ja</language> <docs>http://9chat-e.mynt.work//?f=rss_radio</docs> <itunes:author>9chat-e</itunes:author> <itunes:image href="http://9chat-e.mynt.work//page/radio/img/banner/podcast_banner.jpg"/> <itunes:owner> <itunes:email>9chat-e@mynt.work</itunes:email> <itunes:name>9chat-e</itunes:name> </itunes:owner> <itunes:summary>プログラミング学習に役立つ情報を、ナンチャッテ・エンジニアがお伝えするラジオ番組</itunes:summary> <itunes:category text="Technology"> <itunes:category text="Education"/> </itunes:category> <itunes:explicit>yes</itunes:explicit> <googleplay:author>9chat-e</googleplay:author> <googleplay:image href="http://9chat-e.mynt.work//page/radio/img/banner/podcast_banner.jpg"/> <googleplay:owner> <googleplay:email>9chat-e@mynt.work</googleplay:email> <googleplay:name>9chat-e</googleplay:name> </googleplay:owner> <googleplay:summary>プログラミング学習に役立つ情報を、ナンチャッテ・エンジニアがお伝えするラジオ番組</googleplay:summary> <googleplay:category text="Technology"> <googleplay:category text="Education"/> </googleplay:category> <googleplay:explicit>yes</googleplay:explicit> <item> <title>#004 初心者向けプログラミング言語について</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=4</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/4.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 24 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>プログラミング学習に役立つ情報をお伝えするラジオ番組 - chapter-1 ワンポイントアドバイスのコーナー 「プログラム言語についてのお話」 - chapter-2 質問のコーナー 「iOSのwebviewにて位置情報の確認ダイアログが何度も出る...」 - chapter-3 トライアルのコーナー 「環境構築その2」</description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/4.mp3</guid> </item> <item> <title>#003 フローチャートについて</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=3</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/3.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 17 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>プログラミング学習に役立つ情報をお伝えするラジオ番組 chapter-1 番組開始の挨拶 chapter-2 QAのコーナー chapter-3 トライアルのコーナー </description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/3.mp3</guid> </item> <item> <title>#002 プログラムは数学ではなく国語で考えろ!</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=2</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/2.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 10 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>chapter-1 プログラミング学習アドバイス chapter-2 QAのコーナー capter-3 トライアルのコーナー</description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/2.mp3</guid> </item> <item> <title>#001 ナンチャッテ・ラジオ 始まるよ!</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=1</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/1.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 03 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>プログラミング学習に役立つ情報をお伝えするラジオ番組 chapter-1 番組開始の挨拶 chapter-2 QAのコーナー chapter-3 トライアルのコーナー </description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/1.mp3</guid> </item> </channel> </rss> この中のitemタグを絞り込もうとxpathを使うと・・・ $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 --xpath "//*[local-name()='item']" - $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 --xpath "//*[local-name()='item']" -<item> <title>#004 &#x521D;&#x5FC3;&#x8005;&#x5411;&#x3051;&#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30DF;&#x30F3;&#x30B0;&#x8A00;&#x8A9E;&#x306B;&#x3064;&#x3044;&#x3066;</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=4</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/4.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 24 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>&#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30DF;&#x30F3;&#x30B0;&#x5B66;&#x7FD2;&#x306B;&#x5F79;&#x7ACB;&#x3064;&#x60C5;&#x5831;&#x3092;&#x304A;&#x4F1D;&#x3048;&#x3059;&#x308B;&#x30E9;&#x30B8;&#x30AA;&#x756A;&#x7D44; - chapter-1 &#x30EF;&#x30F3;&#x30DD;&#x30A4;&#x30F3;&#x30C8;&#x30A2;&#x30C9;&#x30D0;&#x30A4;&#x30B9;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; &#x300C;&#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30E0;&#x8A00;&#x8A9E;&#x306B;&#x3064;&#x3044;&#x3066;&#x306E;&#x304A;&#x8A71;&#x300D; - chapter-2 &#x8CEA;&#x554F;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; &#x300C;iOS&#x306E;webview&#x306B;&#x3066;&#x4F4D;&#x7F6E;&#x60C5;&#x5831;&#x306E;&#x78BA;&#x8A8D;&#x30C0;&#x30A4;&#x30A2;&#x30ED;&#x30B0;&#x304C;&#x4F55;&#x5EA6;&#x3082;&#x51FA;&#x308B;...&#x300D; - chapter-3 &#x30C8;&#x30E9;&#x30A4;&#x30A2;&#x30EB;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; &#x300C;&#x74B0;&#x5883;&#x69CB;&#x7BC9;&#x305D;&#x306E;&#xFF12;&#x300D;</description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/4.mp3</guid> </item><item> <title>#003 &#x30D5;&#x30ED;&#x30FC;&#x30C1;&#x30E3;&#x30FC;&#x30C8;&#x306B;&#x3064;&#x3044;&#x3066;</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=3</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/3.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 17 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>&#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30DF;&#x30F3;&#x30B0;&#x5B66;&#x7FD2;&#x306B;&#x5F79;&#x7ACB;&#x3064;&#x60C5;&#x5831;&#x3092;&#x304A;&#x4F1D;&#x3048;&#x3059;&#x308B;&#x30E9;&#x30B8;&#x30AA;&#x756A;&#x7D44; chapter-1 &#x756A;&#x7D44;&#x958B;&#x59CB;&#x306E;&#x6328;&#x62F6; chapter-2 QA&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; chapter-3 &#x30C8;&#x30E9;&#x30A4;&#x30A2;&#x30EB;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; </description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/3.mp3</guid> </item><item> <title>#002 &#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30E0;&#x306F;&#x6570;&#x5B66;&#x3067;&#x306F;&#x306A;&#x304F;&#x56FD;&#x8A9E;&#x3067;&#x8003;&#x3048;&#x308D;&#xFF01;</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=2</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/2.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 10 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>chapter-1 &#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30DF;&#x30F3;&#x30B0;&#x5B66;&#x7FD2;&#x30A2;&#x30C9;&#x30D0;&#x30A4;&#x30B9; chapter-2 QA&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; capter-3 &#x30C8;&#x30E9;&#x30A4;&#x30A2;&#x30EB;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC;</description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/2.mp3</guid> </item><item> <title>#001 &#x30CA;&#x30F3;&#x30C1;&#x30E3;&#x30C3;&#x30C6;&#x30FB;&#x30E9;&#x30B8;&#x30AA;&#x3000;&#x59CB;&#x307E;&#x308B;&#x3088;&#xFF01;</title> <link>http://9chat-e.mynt.work/?c=radio/contents&amp;id=1</link> <enclosure url="http://9chat-e.mynt.work//data/9chat_e/radio/1.mp3" length="" type="audio/mpeg"/> <pubDate>Mon, 03 Feb 2020 08:00:00 +0900</pubDate> <itunes:author>9chat-e</itunes:author> <description>&#x30D7;&#x30ED;&#x30B0;&#x30E9;&#x30DF;&#x30F3;&#x30B0;&#x5B66;&#x7FD2;&#x306B;&#x5F79;&#x7ACB;&#x3064;&#x60C5;&#x5831;&#x3092;&#x304A;&#x4F1D;&#x3048;&#x3059;&#x308B;&#x30E9;&#x30B8;&#x30AA;&#x756A;&#x7D44; chapter-1 &#x756A;&#x7D44;&#x958B;&#x59CB;&#x306E;&#x6328;&#x62F6; chapter-2 QA&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; chapter-3 &#x30C8;&#x30E9;&#x30A4;&#x30A2;&#x30EB;&#x306E;&#x30B3;&#x30FC;&#x30CA;&#x30FC; </description> <guid>http://9chat-e.mynt.work//data/9chat_e/radio/1.mp3</guid> </item> 文字コード化したデータをブラウザで表示すると、ブラウザのオートエスケープ機能が働いて、正常に見えるんですが、コマンドを叩いたら、上記のようになるので、非常に困惑します。

解決への道

ひとまず、nkfコマンドで、文字エンコード状態を調べてみましょう。 $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 -| nkf -g > UTF-8 $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 --xpath "//*[local-name()='item']" -| nkf -g > ASCII 見事に、UTF-8からASCIIに切り替わっていることが分かります。 でも、 $ curl http://9chat-e.mynt.work/?f=rss_radio | xmllint --encode utf-8 - | nkf -W8 | nkf -g > UTF-8 無事に変換できたかと思っても、コード表示は変わっていません。 ファイルのエンコードが変わっただけのようです。 コマンド操作だけでは、これが限界なのか? この先は、phpなどで変換したほうがいいかもですね。

人気の投稿

このブログを検索

ごあいさつ

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

ブログ アーカイブ