XSLTでScriptを使う1

<xsl:attribute>を使いHTMLタグにイベントハンドラ属性を生成する。その値としてScriptコードを記述する。

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

XMLソース

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

<center><H4>ScriptのイベントハンドラをHTMLタグの属性として使う</H4></center>
<xsl:apply-templates select="root/data"/>
<p id="info" style="width:80%;text-align:center;color:navy;border:1px solid green"> (ここに情報を表示)</p>
</body>
</xsl:template>

<xsl:template match="data">
<p>
<xsl:attribute name="onmouseover">this.style.color="red";</xsl:attribute>
<xsl:attribute name="onmouseout">this.style.color="";</xsl:attribute>

この上にマウスをのせると文字色が変わります
</p>
<button>
<xsl:attribute name="onclick">info.innerText='<xsl:value-of select="kabu[position()=1]/name"/>の 終値は:<xsl:value-of select="kabu[position()=1]/end"/>円、 出来高は:<xsl:value-of select="kabu[position()=1]/total"/>(千株)'</xsl:attribute>
銘柄[松下]の情報
</button>
<button>
<xsl:attribute name="onclick">info.innerText='<xsl:value-of select="kabu[position()=3]/name"/>の 終値は:<xsl:value-of select="kabu[position()=3]/end"/>円、 出来高は:<xsl:value-of select="kabu[position()=3]/total"/>(千株)'</xsl:attribute>
銘柄[ソニー]の情報
</button>
<button>
<xsl:attribute name="onclick">info.innerText='<xsl:value-of select="kabu[position()=8]/name"/>の 終値は:<xsl:value-of select="kabu[position()=8]/end"/>円、 出来高は:<xsl:value-of select="kabu[position()=8]/total"/>(千株)'</xsl:attribute>
銘柄[富士通]の情報
</button>
</xsl:template>
</xsl:stylesheet>

end(04/6/5)