mod演算子

mod演算子・・・剰余、左辺を右辺の値で除算した余りを返す。

(応用)「数値 mod 2 = 1」で奇数を、「数値 mod 2 = 0」で偶数を得られる。

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

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="mod01.xsl"?>
<root>
<title>2004年8月20日の東証株式第一部情報</title>
<info><name>銘柄</name><end>終値(円)</end><total>出来高(千株)</total></info>
<data>
<kabu><name code="6752">松下電器産業</name><end>1436</end><total>3362</total></kabu>
<kabu><name code="6753">シャープ</name><end>1516</end><total>9077</total></kabu>
<kabu><name code="6758">ソニー</name><end>3700</end><total>3840</total></kabu>
<kabu><name code="6764">三洋電機</name><end>386</end><total>9712</total></kabu>
<kabu><name code="6501">日立製作所</name><end>667</end><total>8939</total></kabu>
<kabu><name code="6502">東芝</name><end>406</end><total>16041</total></kabu>
<kabu><name code="6701">NEC</name><end>702</end><total>8493</total></kabu>
<kabu><name code="6702">富士通</name><end>671</end><total>3737</total></kabu>
</data>
<end>以上です(2004/9/2)</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="/">
<center><H4><xsl:value-of select="root/title"/></H4></center>
<xsl:apply-templates select="root/data"/>
</xsl:template>

<xsl:template match="data">
<table border="1">
<tr>
<xsl:for-each select="../info/*">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="kabu">
<tr>
<xsl:attribute name="style">background-color:
<xsl:choose>
<xsl:when test="position() mod 3 = 0">beige</xsl:when>
<xsl:when test="position() mod 3 = 1">lightskyblue</xsl:when>
<xsl:otherwise>silver</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:for-each select="*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

end(新規:04/12/3) [Home]