Search code examples
xslt-2.0xslkey

XSLT 2.0: Check if string within a node-set is contained in another string


I have a requirement in which the input XML that is received has different error description for the same error code. I need to compare whether a part of the text is contained within the error description in order to do some filtering. Below is the snippet of what I am trying to do.

Created a variable to store a list of all the partial text to be checked within the error description.

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

Created a key to access the variable

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

The input XML to this XSL will have something like

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

OR

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

Is it possible to use contains function to check whether <Desc> has one of the values from partialTextList?

I tried to look up a solution for this comparison but was not able to find one. Most of the solutions are to check whether <Desc> value is present in the list but not vice-versa.

Any help is appreciated.


Solution

  • In the context of e.g. xsl:template match="Error" you can certainly check $partialTextList/errorDesc/@value[contains(current()/Desc, .)] or move it to the pattern xsl:template match="Error[$partialTextList/errorDesc/@value[contains(current()/Desc, .)]]" if you like.