首页 > 解决方案 > XSLT 2.0:检查节点集中的字符串是否包含在另一个字符串中

问题描述

我有一个要求,其中接收到的输入 XML 对相同的错误代码具有不同的错误描述。我需要比较部分文本是否包含在错误描述中,以便进行一些过滤。下面是我正在尝试做的片段。

创建了一个变量来存储要在错误描述中检查的所有部分文本的列表。

<xsl:variable name="partialTextList">
    <errorDesc value="insufficient funds" />
    <errorDesc value="amount cannot exceed" />
</xsl:variable>

创建了访问变量的密钥

<xsl:key name="kErrorDesc" match="errorDesc" use="@value" />

此 XSL 的输入 XML 将具有类似

<Error>
    <Code>123</Code>
    <Desc>Transaction cannot be processed as account has insufficient funds.</Desc>
</Error>

或者

<Error>
    <Code>123</Code>
    <Desc>The withdrawal amount cannot exceed account balance.</Desc>
</Error>

是否可以使用contains函数来检查是否<Desc>具有来自 的值之一partialTextList

我试图为这种比较查找解决方案,但找不到。大多数解决方案是检查<Desc>列表中是否存在值,反之亦然。

任何帮助表示赞赏。

标签: xslt-2.0xslkey

解决方案


在 eg 的上下文中,xsl:template match="Error"您当然可以根据需要检查$partialTextList/errorDesc/@value[contains(current()/Desc, .)]或将其移动到模式中xsl:template match="Error[$partialTextList/errorDesc/@value[contains(current()/Desc, .)]]"


推荐阅读