我需要检查节点列表中是否存在特定值。
目前我正在使用 for-each,我认为这效率不高。
<xsl:for-each select="$ChildList">
<i><xsl:value-of select="current()"></xsl:value-of> and <xsl:value-of select="$thisProduct"></xsl:value-of></i><br/>
<xsl:if test="string(current())=string($thisProduct)">
<!--<xsl:variable name="flag" select="1"/> -->
<p><b>Found!</b>
</p>
</xsl:if>
</xsl:for-each>
我想一次性搞定。我该怎么办?
最佳答案
按照您的使用方式使用时,current()
与 .
相同(参见 12.4 部分)。但是,current
的目的(广义上讲)是能够从谓词中获取整个表达式的上下文节点(其中 .
表示谓词的上下文) .
我想这种区别的微妙之处可能引起了一些混淆。
只有当整个表达式的上下文节点的字符串值与 $thisProduct 相同时,这个 XPath 表达式才会成功。这显然不是你想要的:
$ChildList[string(current())=string($thisProduct)]
如果 $ChildList
中有一个节点与 $thisProduct 具有相同的字符串值,则此表达式将成功。
$ChildList[string(.)=string($thisProduct)]
即它通过 $ChildList
查找表达式 string(.)=string($thisProduct)
为真的节点。
关于xml - XSLT:检查是否在节点列表中找到节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8615830/