XSLでHTMLタグやCSSを使う

<xsl:template>〜</xsl:template>内のテンプレート内容には、元のXML文書に無い文字列や要素を記述できる。文字列はそのままコピー出力される。XSLT命令要素以外の要素もそのままコピー出力される。

コピー出力された要素がHTMLタグの場合、HTMLタグとして認識され出力文書に反映する。よってXSLT内でHTMLタグとCSSのstyle=""属性を使い書式設定できる。
<ABC>〜</ABC>のようにHTMLタグでない場合は、無意味。

(注意)XSL文書もXML文書の規則に従わなければならない。よって整形式の条件に合致しない単独タグ(終了タグを必要としない<HR>、<BR>など)はそのままでは使えなず、エラーになる。空要素として<HR/>、<BR/>のように記述すればよい。

[サンプルXML文書を開く](下のフレームに)

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="xsl02.xsl"?>
<root>
<title>2003年11月05日の東証株式第一部情報</title>
<info><name>銘柄</name><end>終値(円)</end><total>出来高(千株)</total></info>
<data>
<name code="6752">松下<end>1529</end><total>12302</total></name>
<name code="6753">シャープ<end>1786</end><total>3964</total></name>
<name code="6758">ソニー<end>4030</end><total>8220</total></name>
<name code="6764">三洋<end>516</end><total>7551</total></name>
<name code="6501">日立<end>692</end><total>21896</total></name>
<name code="6502">東芝<end>440</end><total>17832</total></name>
<name code="6701">NEC<end>1027</end><total>19229</total></name>
<name code="6702">富士通<end>708</end><total>16634</total></name>
</data>
<end>以上です(2003/11/07)</end>
</root>

XSLソース

<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="Shift_JIS"/>
<xsl:template match="/">
<H3 style="color:blue">=== <xsl:value-of select="root/title"/> ===</H3>
<HR style="margin:2mm 2cm;color:green"/>
<DIV style="margin-left:3cm">表示する情報は<BR/>[
<span style="color:red;font-size:12px">
  <xsl:value-of select="root/info/name"/>,
  <xsl:value-of select="root/info/end"/>,
  <xsl:value-of select="root/info/total"/>
</span> ]<BR/>
です
</DIV><HR/>
<ABC><i>(2003/11/07)</i></ABC>
</xsl:template>
</xsl:stylesheet>

end(03/11/7)