Xslt 节点集测试中的不一致性?
我有一个包含顶级元素的XML:Xslt 节点集测试中的不一致性?,xslt,xpath,Xslt,Xpath,我有一个包含顶级元素的XML: <chapter template="one"/> <chapter template="two"/> <chapter template="one"/> <chapter template="one"/> <chapter template="two"/> <chapter template="one"/> 我通过一个choose语句循环处理这些元素: <xsl:variable
<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,并从那里开始递增。它似乎统计了所有顶级元素,而不仅仅是名为“chapter”的元素。
当我将代码更改为以下内容时:
<xsl:when test="count(preceding-sibling::*[(@template = 'one')]) = '0'">
<xsl:call-template name="processChapterOne"/>
</xsl:when>
它工作正常。所以我用直接引用替换了一个变量。我不明白为什么这会有不同。这是什么原因造成的
*[(@template = 'one')]
上面的意思是:计算所有节点
,其中属性模板
等于文本一个
*[($layout = 'one')]
上面的意思是:计算所有节点
,其中变量布局
等于文本一个
。
我认为,对于您提出的问题,$layout
没有填充文本one
,但它有一个xsl:call模板
。也许这里出了什么问题
除此之外,如果您不想计算所有节点
,而只计算章节
节点。这样做:
chapter[($layout = 'one')]
chapter[(@template = 'one')]
不工作和工作的情况实际上非常不同:
前面的同级:*[$layout='one']
中,$layout
的值始终与最初在
语句中设置的值相同@template
根据前面不同同级上下文节点的@template
属性值而变化啊,我现在明白了。我假设
$layout
将扩展到@template
,但在声明变量时,$layout
将填充当前节点的@template
值。