表:行・列の連結

●列の連結にはHTMLのcolspan属性を利用。<xsl:attribute>を使い<td><th>タグにこのcolspan属性を生成します。連結数は数値を直接指定してもよいが、サンプルでは<team>の子要素の数から自動的に取得している。

●行の連結にはHTMLのrowspan属性を利用。<xsl:attribute>を使い<td><th>タグにこのrowspan属性を生成します。連結数は数値を直接指定してもよいが、サンプルでは<team>要素の数から自動的に取得している。

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

XMLソース

<?xml version="1.0" encoding="Shift_JIS" ?>
<?xml-stylesheet type="text/xsl" href="table02.xsl"?>
<root>
<title>パリーグ勝敗表(2000/10/15現在)</title>
<team>
<順位>1位</順位>
<球団名>ダイエー</球団名>
<勝数>73</勝数>
<負数>60</負数>
<勝率>.549</勝率>
</team>
<team>
<順位>2位</順位>
<球団名>西武</球団名>
<勝数>69</勝数>
<負数>61</負数>
<勝率>.531</勝率>
</team>
<team>
<順位>3位</順位>
<球団名>日本ハム</球団名>
<勝数>69</勝数>
<負数>65</負数>
<勝率>.515</勝率>
</team>
<team>
<順位>4位</順位>
<球団名>オリックス</球団名>
<勝数>64</勝数>
<負数>66</負数>
<勝率>.492</勝率>
</team>
<team>
<順位>5位</順位>
<球団名>ロッテ</球団名>
<勝数>61</勝数>
<負数>67</負数>
<勝率>.477</勝率>
</team>
<team>
<順位>6位</順位>
<球団名>近鉄</球団名>
<勝数>58</勝数>
<負数>75</負数>
<勝率>.436</勝率>
</team>
<end>以上です(04/4/7)</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:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<H4><xsl:value-of select="root/title"/></H4>
<xsl:apply-templates select="root"/>
</xsl:template>
<xsl:template match="root">
<table border="1">
<tr><th>
<xsl:attribute name="colspan"><xsl:value-of select="count(team[1]/*)"/></xsl:attribute>
<p style="text-align:center;color:red">列の連結</p>
</th></tr>
<tr>
<!--表の列名は、local-name()関数を使い要素名から取得している-->
<xsl:for-each select="team[1]/*">
<th><xsl:value-of select="local-name()"/></th>
</xsl:for-each>
</tr>
<tr><td>・・・・</td><td>
<xsl:attribute name="colspan"><xsl:value-of select="count(team[1]/*)-2"/></xsl:attribute>
<p style="text-align:center;color:green">(2000/10/15現在)</p>
</td><td>・・・・</td></tr>
<xsl:for-each select="team">
<tr>
<td><xsl:value-of select="順位"/></td>
<td><xsl:value-of select="球団名"/></td>
<td><xsl:value-of select="勝数"/></td>
<td><xsl:value-of select="負数"/></td>
<td><xsl:value-of select="勝率"/></td>
</tr>
</xsl:for-each>
</table>
<hr/>
<table border="1">
<tr>
<th style="width:20px;text-align:center;color:red">
<xsl:attribute name="rowspan"><xsl:value-of select="count(team)+1"/></xsl:attribute>
<p>行の連結</p>
</th>
<xsl:for-each select="team[1]/*">
<th><xsl:value-of select="local-name()"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="team">
<tr>
<td><xsl:value-of select="順位"/></td>
<td><xsl:value-of select="球団名"/></td>
<td><xsl:value-of select="勝数"/></td>
<td><xsl:value-of select="負数"/></td>
<td><xsl:value-of select="勝率"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

end(04/4/7)