BLOGTIMES
2011/12/14

コマンドラインから xsl を使う

  xml  cli 
このエントリーをはてなブックマークに追加

手軽に XML ファイルに XSLT を適用するツールがないかなと思って探してみたら、 libxslt に付属の xsltproc というものが見つかったのでメモ。

The xsltproc tool

This program is the simplest way to use libxslt: from the command line. It is also used for doing the regression tests of the library.
It takes as first argument the path or URL to an XSLT stylesheet, the next arguments are filenames or URIs of the inputs to be processed. The output of the processing is redirected on the standard output.

使い方としては xsltproc (xslt file) (xml file) という感じで使えるようです。
それぞれのファイル指定はURLでもいいみたいなので、ネットワーク越しに xsl や xml を取り寄せて変換することができます。

以下、作業メモ。

動作サンプル

今回は rss を html に変換する超簡単な xsl を使いました。

rss2html.xsl

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <xsl:apply-templates select="/rss/channel"/> </xsl:template> <xsl:template match="/rss/channel"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body> <xsl:apply-templates select="item"/> </body> </html> </xsl:template> <xsl:template match="/rss/channel/item"> <a href="{link}"><xsl:value-of select="title"/></a> </xsl:template> </xsl:stylesheet>

このファイルを下記のように実行することができます。

$ xsltproc rss2html.xsl http://blog.cles.jp/xml-rss2.php <?xml version="1.0"?> <html> <head> <title>cles::blog</title> </head> <body> <a href="/item/4643">バルス!が Twitter 最高記録を更新</a> <a href="/item/4642">phantomjs コマンドラインから実行できる Webkit ベースのブラウザ</a> <a href="/item/4640">svn, git, hg の proxy 設定</a> <a href="/item/4639">ThinkPad が Made in Japan になる?</a> <a href="/item/4638">セブンスポットから amazon や楽天にアクセスできないらしい</a> <a href="/item/4637">バルス砲の威力はどうだったのか</a> <a href="/item/4636">妙義山で皆既月食観察</a> <a href="/item/4635">きこり</a> <a href="/item/4634">山手線唯一の踏切</a> <a href="/item/4633">ホワイトヒマラヤ</a> </body> </html>

    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/4641
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    コメントは承認後の表示となります。
    OpenIDでログインすると、即時に公開されます。

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン