用于检查变量是否属于元素集的 XSLT 表达式 [英] XSLT expression to check if variable belongs to set of elements
本文介绍了用于检查变量是否属于元素集的 XSLT 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
<xsl:if test="$k='7' or $k = '8' or $k = '9'">
有什么办法可以把这个表达式放在一个表单中,比如 SQL
Is there any way to put this expression in a form, like, for instance SQL
k IN (7, 8, 9)
你:)
推荐答案
XSLT/XPath 1.0:
XSLT / XPath 1.0:
<!-- a space-separated list of valid values -->
<xsl:variable name="list" select="'7 8 9'" />
<xsl:if test="
contains(
concat(' ', $list, ' '),
concat(' ', $k, ' ')
)
">
<xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
</xsl:if>
如果需要,您可以使用其他分隔符.
You can use other separators if needed.
在 XSLT/XPath 2.0 中,您可以执行以下操作:
In XSLT / XPath 2.0 you could do something like:
<xsl:variable name="list" select="fn:tokenize('7 8 9', '\s+')" />
<xsl:if test="fn:index-of($list, $k)">
<xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
</xsl:if>
如果你可以使用文档结构来定义你的列表,你可以这样做:
If you can use document structure to define your list, you could do:
<!-- a node-set defining the list of currently valid items -->
<xsl:variable name="list" select="/some/items[1]/item" />
<xsl:template match="/">
<xsl:variable name="k" select="'7'" />
<!-- test if item $k is in the list of valid items -->
<xsl:if test="count($list[@id = $k])">
<xsl:value-of select="concat('Item ', $k, ' is in the list.')" />
</xsl:if>
</xsl:template>
这篇关于用于检查变量是否属于元素集的 XSLT 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文