<xsl:for-each> | |
XSLT の反復演算子として機能します。この要素の select 属性を使用して、現在のコンテキストからノードを選択できます。 | |
カテゴリ | |
命令 |
|
必須の属性 | |
|
|
省略可能な属性 | |
なし。 |
|
コンテンツ | |
<xsl:for-each> には、選択した各ノードを評価したテンプレートが含まれます。<xsl:for-each> 要素に 1 つまたは複数の <xsl:sort> 要素を含めて、処理を行う前に選択したノードを並べ替えることができます。<xsl:sort> 要素は、必ずテンプレートの開始前に最初に指定する必要があります。 |
|
指定先 | |
<xsl:for-each> テンプレート内に指定します。 |
|
定義先 | |
XSLT 8 節「Repetition」 |
|
例 | |
次のスタイルシートに <xsl:for-each> 要素を示します。 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:variable name="complicatedVariable"> <xsl:choose> <xsl:when test="count(//listitem) > 10"> <xsl:text>really long list</xsl:text> </xsl:when> <xsl:when test="count(//listitem) > 5"> <xsl:text>moderately long list</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>fairly short list</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$newline"/> <xsl:text>Here is a </xsl:text> <xsl:value-of select="$complicatedVariable"/> <xsl:text>:</xsl:text> <xsl:value-of select="$newline"/> <xsl:variable name="listitems" select="list/listitem"/> <xsl:call-template name="processListitems"> <xsl:with-param name="items" select="$listitems"/> </xsl:call-template> </xsl:template> <xsl:template name="processListitems"> <xsl:param name="items"/> <xsl:for-each select="$items"> <xsl:value-of select="position()"/> <xsl:text>. </xsl:text> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> このスタイルシートでは、items という名前の <xsl:param> の値を使用して <xsl:for-each> 要素を示します。items パラメータには、XML ソースドキュメントの <listitem> 要素が複数含まれます。<xsl:for-each> 要素により、これらのすべての要素が繰り返し処理されます。次の 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> 変換を実行した場合の結果は次のとおりです。 Here is a moderately long list: 1. A Love Supreme 2. Beat Crazy 3. Here Come the Warm Jets 4. Kind of Blue 5. London Calling 6. Remain in Light 7. The Joshua Tree 8. The Indestructible Beat of Soweto <xsl:for-each> 要素により、XML ソースドキュメントのすべての <listitem> 要素が繰り返し処理されます。 |