Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 节点集测试中的不一致性?_Xslt_Xpath - Fatal编程技术网

Xslt 节点集测试中的不一致性?

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

我有一个包含顶级元素的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,并从那里开始递增。它似乎统计了所有顶级元素,而不仅仅是名为“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='one'],
    @template
    根据前面不同同级上下文节点的
    @template
    属性值而变化


  • 啊,我现在明白了。我假设
    $layout
    将扩展到
    @template
    ,但在声明变量时,
    $layout
    将填充当前节点的
    @template
    值。