Understanding and Using the contains Function in XSLT: Common Pitfalls and Solutions

Dec 05, 2025 · Programming · 10 views · 7.8

Keywords: XSLT | contains function | string processing

Abstract: This technical article provides an in-depth exploration of the contains function in XSLT, examining its core syntax and practical applications. Through comparative analysis of common erroneous patterns versus correct implementations, it systematically explains the logical structure for string containment checking. Starting from fundamental function definitions, the article progressively addresses key technical aspects including variable referencing and Boolean logic combination, supplemented by practical code examples to help developers avoid typical syntax errors.

Fundamental Syntax of the XSLT contains Function

In XSLT transformation processes, string manipulation is a frequent requirement, with the need to check whether a string contains a specific substring being particularly important. XSLT 1.0 and subsequent versions provide the built-in contains() function for this purpose. The basic syntax structure is: contains(stringToSearchWithin, stringToSearchFor), where the first parameter is the source string to be searched, and the second parameter is the target substring to find.

The function returns a Boolean value: true if the source string contains the target substring, otherwise false. This return value can be directly used in XSLT conditional test expressions, such as in <xsl:if> or <xsl:choose> statements.

Analysis of Common Error Patterns

Many XSLT beginners make syntax errors when using the contains function, especially when combining conditional logic. A typical erroneous pattern is: test="hhref not contains '1234'". This formulation has two main issues: first, the XSLT expression language does not support combined keywords like not contains; second, the variable reference is incorrect, as hhref as a variable name requires the $ prefix.

The correct logical negation should use the not() function to wrap the entire contains expression, forming the structure not(contains($hhref, '1234')). This formulation clearly expresses the conditional logic of "when $hhref does not contain '1234'", conforming to XSLT's function call specifications.

Correct Implementation Example and Detailed Explanation

The following is a complete correct implementation example, demonstrating how to combine variable definitions and conditional checks within a <xsl:for-each> loop:

<xsl:for-each select="item">
  <xsl:variable name="hhref" select="link" />
  <xsl:variable name="pdate" select="pubDate" />
  <xsl:if test="not(contains($hhref, '1234'))">
    <li>
      <a href="{$hhref}" title="{$pdate}">
        <xsl:value-of select="title"/>
      </a>
    </li>
  </xsl:if>
</xsl:for-each>

In this example, two variables $hhref and $pdate are first defined via <xsl:variable>, storing the values of the link and pubDate child nodes of the current item element, respectively. Subsequently, in the test attribute of <xsl:if>, the expression not(contains($hhref, '1234')) is used for conditional evaluation: only when the value of the $hhref variable does not contain the string 1234 will the internal list item generation logic be executed.

It is important to note that when referencing variables in attribute value templates (such as href="{$hhref}"), curly braces {} must be used to enclose the variable name, which is the standard syntax for outputting variable values in XSLT. In XPath expressions (such as the test attribute), variables are referenced directly using the form $variableName.

Advanced Applications and Best Practices

Beyond basic string containment checking, the contains() function can be combined with other string functions to implement more complex text processing logic. For example:

In practical development, it is recommended to always use explicit function call syntax and avoid relying on implicit type conversions. For string evaluation logic that needs to be used frequently, consider defining it as named templates or functions to enhance code reusability and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.