変数の活用

スタイルシートの文字色をグローバル変数に格納し、出来高によって文字色を変える。

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

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="var03.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>以上です(2003/11/23)</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:variable name="red" select="'color:red'" />
<xsl:variable name="blue" select="'color:blue'"/>
<xsl:variable name="green" select="'color:green'"/>
<xsl:variable name="magenta" select="'color:magenta'"/>
<xsl:variable name="black" select="'color:black'"/>

<xsl:template match="/">
<center><H3><xsl:value-of select="root/title"/></H3></center>
<xsl:apply-templates select="root/data/kabu"/>
</xsl:template>
<xsl:template match="kabu">
<div>
<xsl:attribute name="style">
<xsl:choose>
<xsl:when test="total[.&gt;'20000']">
<xsl:value-of select="$red"/>
</xsl:when>
<xsl:when test="total[.&gt; 15000]">
<xsl:value-of select="$blue"/>
</xsl:when>
<xsl:when test="total[.&gt; 10000]">
<xsl:value-of select="$green"/>
</xsl:when>
<xsl:when test="total[.&gt; 5000]">
<xsl:value-of select="$magenta"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$black"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="name"/>:(終値)<xsl:value-of select="end"/>
(出来高)<xsl:value-of select="total"/>
</div>
</xsl:template>
</xsl:stylesheet>

end(03/11/23)