如何在XSLT中使用contains()和一组字符串

时间:2013-10-01 14:31:43

标签: xslt

我有以下XML代码段:

<figure customer="ABC DEF">
    <image customer="ABC"/>
    <image customer="XYZ"/>
</figure>

我想检查figure元素的customer属性是否包含图像元素的customer属性。

<xsl:if test="contains(@customer, image/@customer)">
    ...
</xsl:if>

我收到错误说:

  

不允许包含多个项目的序列作为contains

的第二个参数

重要的是要注意我无法预先告知客户属性的值,因此使用xsl:choose不是一个选项。

是否可以在不使用xsl:for-each?

的情况下解决这个问题

1 个答案:

答案 0 :(得分:2)

在XSLT 2.0中,您可以使用:

test="image/@customer/contains(../../@customer, .) = true()"

如果其中任何一个为真,您将获得true()结果。实际上,这导致我建议:

test="some $cust in image/@customer satisfies contains(@customer, $cust)"

但这不会解决客户字符串是另一个客户字符串的子集的情况。

因此,也许这是最好的:

test="tokenize(@customer,'\s+') = image/@customer"

...因为它会进行逐字符串比较,如果figure属性的任何标记化值等于其中一个图像属性,则会给你true()