復習と勉強がてらにXMLファイルをコピーするという単純な処理を様々な言語でどのように実現できるかやってみました.
[MusicLibrary.xml]
<musicLibrary>
<cd id="1">
<title>Parallel Lines</title>
<year>2001</year>
<artist>Blondie</artist>
<genre>New Wave</genre>
</cd>
<cd id="2">
<title>Bat Out of Hell</title>
<year>2001</year>
<artist>Meatloaf</artist>
<genre>Rock</genre>
</cd>
<cd id="3">
<title>Abbey Road</title>
<year>1987</year>
<artist>The Beatles</artist>
<genre>Rock</genre>
</cd>
<cd id="4">
<title>The Dark Side of the Moon</title>
<year>1994</year>
<artist>Pink Floyd</artist>
<genre>Rock</genre>
</cd>
<cd id="5">
<title>Thriller</title>
<year>2001</year>
<artist>Michael Jackson</artist>
<genre>Pop</genre>
</cd>
</musicLibrary>
※ なんとPink FloydのThe Dark Side of the Moonが入っています.なかなか作者の趣味を感じさせられます.
[style.xsl]
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" encoding="UTF-8" byte-order-mark="no" omit-xml-declaration="no"/>
<!-- すべての要素にマッチするテンプレート-->
<xsl:template match="*">
<!--自分をコピーし-->
<xsl:copy>
<!--属性もコピーする-->
<xsl:copy-of select="@*"/>
<!--下位ノードを処理する-->
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
すると次のようにXML宣言の箇所だけ改行の有無が違ったファイルが出力されます.
あとの部分はまったく同じです.
このスタイルシートは実に簡単ですが、ちゃんとXMLファイルのすべての要素、属性、テキストを全部なめて処理します.要素は<xsl:copy>でコピーされ、属性は<xsl:copy-of select="@*"/>でコピーされます.テキストノードはデフォルトテンプレートが適用され自動的に出力にコピーされます.たった一つのテンプレートにもかかわらずなかなか奥深いです.