Deep Analysis of String Concatenation and Attribute Value Templates in XSLT

Nov 22, 2025 · Programming · 11 views · 7.8

Keywords: XSLT | String Concatenation | concat Function | Attribute Value Templates | XPath

Abstract: This article provides an in-depth exploration of the concat() function in XSLT, detailing how to concatenate strings within xsl:value-of elements and introducing the simplified syntax of attribute value templates. Through practical code examples, it demonstrates how to combine static text with dynamic XPath expression results for applications such as href attribute construction. The article also analyzes the parameter processing mechanism of the concat() function and various application patterns, offering comprehensive guidance on string operations for XSLT developers.

Fundamentals of String Concatenation in XSLT

During XSLT development, there is often a need to combine multiple string fragments into complete text content. This requirement is particularly common when constructing HTML attribute values. This article will use the scenario from the Q&A data as an example to conduct an in-depth analysis of the technical implementation of string concatenation.

Core Usage of the concat() Function

The XPath standard library provides the concat() function to handle string concatenation operations. This function accepts two or more parameters and connects all parameters in sequence to form a single string. Any parameters that are not of string type are automatically converted to strings, following the same rules as the string() function.

In the original problem scenario, the developer needed to concatenate static text with dynamically obtained attribute values within the select attribute of the xsl:value-of element:

<a>
    <xsl:attribute name="href">
        <xsl:value-of select="concat('myText:', /*/properties/property[@name='report']/@value)" />
    </xsl:attribute>
</a>

This code demonstrates the basic application pattern of the concat() function: the first parameter is the string literal 'myText:', and the second parameter is a dynamic value obtained through an XPath expression. After the function executes, the two parts are seamlessly connected to form the complete href attribute value.

Simplified Syntax with Attribute Value Templates

XSLT provides a more concise attribute value template syntax, using curly braces {} to directly embed XPath expressions within attributes:

<a href="{concat('myText:', /*/properties/property[@name='report']/@value)}"></a>

This syntax not only makes the code more concise but also improves execution efficiency. The expression within the curly braces is evaluated directly when the XSLT processor parses the template, avoiding the additional creation process of xsl:attribute elements.

Advanced Applications of the concat() Function

The concat() function supports any number of parameters, providing great flexibility for complex string construction. The example from the reference article demonstrates how to build filenames based on multiple data sources:

<xsl:value-of select="concat('album', position(), '.jpg')"/>

In this example, the function combines the static text 'album', the dynamic position index position(), and the file extension '.jpg' to generate a formatted filename.

Parameter Type Conversion Mechanism

When the concat() function receives non-string parameters, it automatically performs type conversion. Numbers, Boolean values, etc., are all processed according to XPath's string conversion rules. For example, the number 42 is converted to the string "42", and the Boolean value true() is converted to "true".

Analysis of Practical Application Scenarios

In web development, string concatenation is commonly used for constructing URLs, generating file paths, creating message templates, and other scenarios. By properly using the concat() function, developers can dynamically generate text content that meets business requirements while maintaining code maintainability and readability.

Developers should choose between the traditional method using xsl:attribute combined with xsl:value-of and the simplified syntax of attribute value templates based on specific needs. In scenarios with high performance requirements, attribute value templates are usually the better choice.

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.