2

このようなトップレベルの要素を持つ 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>

それは正しく動作します。そのため、変数を直接参照に置き換えました。なぜこれが違いを生むのか理解できません。何が原因でしょうか?

4

2 に答える 2

1

機能しないケースと機能するケースは、実際には大きく異なります。

  1. 動作していません: ではpreceding-sibling::*[$layout = 'one']、 ステートメントで最初に設定されたとき$layout と常に同じ値です。one<xsl:variable name="layout" select="@template"/>

  2. Working : ではpreceding-sibling::*[@template = 'one']、 さまざまな先行兄弟コンテキスト ノード@templateの属性値ごとに異なります。@template

于 2013-10-02T14:04:25.870 に答える
0
*[(@template = 'one')]

nodes上記の意味: attributetemplateが text と等しい場所をすべてカウントしますone

*[($layout = 'one')]

nodes上記の意味: variablelayoutが text と等しい場所をすべてカウントしますone。あなたが提起した質問$layoutはテキストで満たされていないと思いますoneが、xsl:call-template. たぶん、ここで何かがうまくいかないのでしょうか?

それに加えて、ノードnodesだけを除いてすべてを数えたくない場合。chapterこれを行う:

chapter[($layout = 'one')]
chapter[(@template = 'one')]
于 2013-10-02T13:29:34.470 に答える