| position() 関数 | |
| 現在のコンテキストからのコンテキストの位置に等しい数値を返します。 | |
| 入力 | |
|
なし。 |
|
| 出力 | |
|
評価コンテキストの現在のノードの場所に等しい数値。 |
|
| 定義先 | |
|
XPath 4.1 節「ノードセット関数」 |
|
| 例 | |
|
この例では、position() 関数を使用して、テーブルの行の背景色を指定します。背景色は、white、darkgray、および lightgreen に順次変わります。使用する XML ドキュメントは次のとおりです。 <?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list> このスタイルシートを使用して HTML ドキュメントを生成します。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="/list/title"/>
</title>
</head>
<body>
<h1>
<xsl:value-of select="/list/title"/>
</h1>
<table border="1">
<xsl:for-each select="/list/listitem">
<xsl:variable name="background-color">
<xsl:choose>
<xsl:when test="position() mod 3 = 1">white</xsl:when>
<xsl:when test="position() mod 3 = 2">darkgray</xsl:when>
<xsl:otherwise>lightgreen</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr bgcolor="{$background-color}">
<td>
<b><xsl:value-of select="."/></b>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
スタイルシートからは、次のような結果が生成されます。 <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>A few of my favorite albums</title> </head> <body> <h1>A few of my favorite albums</h1> <table border="1"> <tr bgcolor="white"> <td><b>A Love Supreme</b></td> </tr> <tr bgcolor="darkgray"> <td><b>Beat Crazy</b></td> </tr> <tr bgcolor="lightgreen"> <td><b>Here Come the Warm Jets</b></td> </tr> <tr bgcolor="white"> <td><b>Kind of Blue</b></td> </tr> <tr bgcolor="darkgray"> <td><b>London Calling</b></td> </tr> <tr bgcolor="lightgreen"> <td><b>Remain in Light</b></td> </tr> <tr bgcolor="white"> <td><b>The Joshua Tree</b></td> </tr> <tr bgcolor="darkgray"> <td><b>The Indestructible Beat of Soweto</b></td> </tr> </table> </body> </html> この HTML ファイルの表示を図 C-8 に示します。 背景色が異なる項目を表示した HTML ファイル |
|