Advanced Techniques for Concatenating Multiple Node Values in XPath: Combining string-join and concat Functions

Dec 06, 2025 · Programming · 10 views · 7.8

Keywords: XPath | XML processing | node concatenation

Abstract: This paper explores complex scenarios of concatenating multiple node values in XML processing using XPath. Through a detailed case study, it demonstrates how to leverage the combination of string-join and concat functions to achieve precise concatenation of specific element values in nested structures. The article explains the limitations of traditional concat functions and provides solutions based on XPath 2.0, supplemented with alternative methods in XSLT and Spring Expression Language. With code examples and step-by-step analysis, it helps readers master core techniques for handling similar problems across different technology stacks.

Problem Background and Challenges

In XML data processing, it is often necessary to extract and concatenate text values from multiple nodes in nested structures. For example, given the following XML structure:

<element1>
    <element2>
        <element3>    
            <element4>Hello</element4>
            <element5>World</element5>
        </element3>
        <element3>    
            <element4>Hello2</element4>
            <element5>World2</element5>
        </element3>
        <element3>    
            <element4>Hello3</element4>
            <element5>World3</element5>
        </element3>
    </element2>
</element1>

The goal is to concatenate the text values of <element4> and <element5> within each <element3>, separated by a dot, and output as multiple lines:

Hello.World
Hello2.World2
Hello3.World3

Beginners often attempt to use XPath's concat function, such as:

concat(/element1/element2/element3/element4/text(),".", /element1/element2/element3/element5/text())

This only returns the first pair "Hello.World", because in XPath 1.0, the concat function takes only the first node when processing node-sets.

Core Solution: Combining string-join and concat

The best answer utilizes XPath 2.0's string-join function combined with concat for precise concatenation:

string-join(//element3/(concat(element4/text(), '.', element5/text())), "&#10;")

The core logic of this expression is as follows:

  1. Internal concat operation: For each <element3> node, use relative paths element4/text() and element5/text() to get the text values of its child elements, concatenated with a dot. This avoids the flattening of node-sets caused by absolute paths.
  2. External string-join operation: Join the sequence generated by internal concat (i.e., "Hello.World", "Hello2.World2", "Hello3.World3") with a newline character &#10; to form multi-line output.

The key is using //element3/ as the context to ensure the concat function executes independently on each <element3> node, preserving the grouped structure.

Common Error Analysis

The user's attempted string-join version:

string-join((/element1/element2/element3/element4/text(), /element1/element2/element3/element5/text()),".")

Produces "Hello.Hello2.Hello3.World.World2.World3", because the two path expressions return sequences of all <element4> and <element5> text nodes, and string-join concatenates them after merging into a single sequence, disrupting the original grouping.

Supplementary Technical Solutions

Beyond XPath, other technology stacks offer solutions:

Practical Recommendations and Summary

When handling multi-node concatenation, consider:

  1. Confirm XPath version: XPath 1.0 does not support string-join; upgrade to 2.0 or use processor extensions.
  2. Use relative paths to maintain context and avoid node-set mixing.
  3. Test expression compatibility in target environments like Camel integration frameworks.

By combining string-join and concat, efficient solutions for concatenation needs in XML data extraction can be achieved, enhancing data processing automation.

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.