このようなトップレベルの要素を持つ XML があります。
<chapter template="one"/>
<chapter template="two"/>
<chapter template="one"/>
<chapter template="one"/>
<chapter template="two"/>
<chapter template="one"/>
これらの要素を、choose ステートメントでループして処理しています。
<xsl:variable name="layout" select="@template"/>
<xsl:choose>
<xsl:when test="contains($layout, 'one')">
<xsl:call-template name="processChapterOne"/>
</xsl:when>
<xsl:when test="contains($layout, 'two')">
<xsl:call-template name="processChaptertwo"/>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
これは正しく動作します。しかし、今は条件付き処理をしようとしているので、リストの最初の章を見つけようとしています:
<xsl:when test="count(preceding-sibling::*[($layout = 'one')]) = '0'">
<xsl:call-template name="processChapterOne"/>
</xsl:when>
ここで、物事が奇妙になります。私のテストは決して真になりません: count(...) の値は、リストの最初の章では 4 であり、そこから増加します。「チャプター」という名前の要素だけでなく、すべてのトップレベルの要素をカウントしているようです。コードを次のように変更すると:
<xsl:when test="count(preceding-sibling::*[(@template = 'one')]) = '0'">
<xsl:call-template name="processChapterOne"/>
</xsl:when>
それは正しく動作します。そのため、変数を直接参照に置き換えました。なぜこれが違いを生むのか理解できません。何が原因でしょうか?