フィルタを使ってみよう
はじめに
xsltを使い始めたが,よく似たxsltをいくつか作らないといけないことに気がついた.例えば,備忘録と雑録はディレクトリのパス等を除いてほぼ共通である.
antのフィルタ機能を使うと,これらの良く似たxsltを一つにできそうだ.
xsltファイルを修正
それぞれのxsltで違うところを“@”で囲んだ文字列に書換える.例えば,備忘録のxsltの場合,以下のようになる.
<!-- 書換える前 -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="framework.xsl"/>
<xsl:template name="link">
<link rel="stylesheet" type="text/css" media="all" href="../css/biboroku_import.css"/>
<link rel="start" href="../index.html"/>
<link rel="contents" href="../index.html"/>
<link rel="prev" href="../hyoryuki/index.html"/>
<link rel="next" href="../zatsuroku/index.html"/>
<link rel="shortcut icon" href="../nemuru-neko.ico"/>
</xsl:template>
<xsl:template name="menu">
<div>
<xsl:comment>ulをdivで囲まないとieで表示がずれる</xsl:comment>
<ul class="ContentsMenu">
<li class="top other"><a href="../index.html">表紙</a></li>
<li class="whatsnew other"><a href="../whatsnew/index.html">新着</a></li>
<li class="saijiki other"><a href="../saijiki/index.html">歳時記</a></li>
<li class="ryokoki other"><a href="../ryokoki/index.html">旅行記</a></li>
<li class="hyoryuki other"><a href="../hyoryuki/index.html">漂流記</a></li>
<li class="biboroku self"><xsl:text>備忘録</xsl:text></li>
<li class="zatsuroku other"><a href="../zatsuroku/index.html">雑録</a></li>
<li class="links other"><a href="../links/index.html">リンク</a></li>
</ul>
</div>
<div class="BreadCrumbs">
<a href="../index.html" title="top">表紙</a><xsl:call-template name="menu_hie"/>
<xsl:call-template name="title"/>
</div>
</xsl:template>
<<省略>>
<!-- 書換えた後 -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="framework.xsl"/>
<xsl:template name="link">
<link rel="stylesheet" type="text/css" media="all" href="@STYLESHEET@"/>
<link rel="start" href="@ROOT_DIR@/index.html"/>
<link rel="contents" href="@PARENT_DIR@/index.html"/>
<link rel="prev" href="@PREV_DIR@/index.html"/>
<link rel="next" href="@NEXT_DIR@/index.html"/>
<link rel="shortcut icon" href="@ROOT_DIR@/nemuru-neko.ico"/>
</xsl:template>
<xsl:template name="menu">
<div>
<xsl:comment>ulをdivで囲まないとieで表示がずれる</xsl:comment>
<ul class="ContentsMenu">
<li class="@TOP_CLASS@"><a href="@ROOT_DIR@/index.html">表紙</a></li>
<li class="@WHATSNEW_CLASS@"><a href="@ROOT_DIR@/whatsnew/index.html">新着</a></li>
<li class="@SAIJIKI_CLASS@"><a href="@ROOT_DIR@/saijiki/index.html">歳時記</a></li>
<li class="@RYOKOKI_CLASS@"><a href="@ROOT_DIR@/ryokoki/index.html">旅行記</a></li>
<li class="@HYORYUKI_CLASS@"><a href="@ROOT_DIR@/hyoryuki/index.html">漂流記</a></li>
<li class="@BIBOROKU_CLASS@"><a href="@ROOT_DIR@/biboroku/index.html">備忘録</a></li>
<li class="@ZATSUROKU_CLASS@"><a href="@ROOT_DIR@/zatsuroku/index.html">雑録</a></li>
<li class="@LINKS_CLASS@"><a href="@ROOT_DIR@/links/index.html">リンク</a></li>
</ul>
</div>
<div class="BreadCrumbs">
<a href="@ROOT_DIR@/index.html" title="top">表紙</a><xsl:call-template name="menu_hie"/>
<xsl:call-template name="title"/>
</div>
</xsl:template>
<<省略>>
プロパティファイルを作る
build.xmlと同じディレクトリに置換前の文字列と置換後の文字列を記述したプロパティファイルを作る.build.xmlの中に書くのでファイル名は何でも良い.今回はfilter.propertiesとした.内容は以下の通り.
STYLESHEET=../css/biboroku_import.css
ROOT_DIR=..
PARENT_DIR=..
PREV_DIR=../hyoryuki
NEXT_DIR=../zatsuroku
TOP_CLASS=top other
WHATSNEW_CLASS=whatsnew other
SAIJIKI_CLASS=saijiki other
RYOKOKI_CLASS=ryokoki other
HYORYUKI_CLASS=hyoryuki other
BIBOROKU_CLASS=biboroku self
ZATSUROKU_CLASS=zatsuroku other
LINKS_CLASS=links other
xsltファイルの中に記述した置換前の文字列“@HOGE_HOGE@”と置換後の文字列を対にして記述する.置換後の文字列に空白があっても問題はない.
マルチバイト文字の扱い
マルチバイト文字(ひらがな・カタカナ・漢字等)を置換後の文字列としてプロパティファイルに記述すると,変換後に文字化けが発生する.事前にnavive2asciiでUnicodeに変換しておく必要がある.
タスクを定義しよう
native2asciiでの変換とフィルタを使った置換を行うantのタスクは以下のようになる.コピー先のファイル“tmp.xsl”が,置換済みのファイルとなる.
<target name="xslt-setup">
<native2ascii encoding="UTF-8" dest="."
includes="filter.properties" ext=".ascii.properties"/>
<copy file="org.xsl" tofile="tmp.xsl" filtering="yes" encoding="utf-8">
<filterset>
<filtersfile file="filter.ascii.properties"/>
</filterset>
</copy>
</target>
参考文献
関口宏司著,Apache Ant,技術評論社