string-join()函数抛出XPath无效表达式错误,寻求解决方法
string-join() in Your Expression Let's break down why you're hitting that "Invalid expression" error with string-join(/form/Data/MProcess/Process/Id/string(), ',')—it all comes down to how string-join() expects its arguments to be structured.
The Root Cause
string-join() requires two core parameters:
- A sequence of nodes or strings (the items you want to join together)
- The delimiter string (in your case,
',')
Your current code tries to call string() directly on each Id node within the path (/Id/string()), which isn't valid XPath syntax here. XPath doesn't allow chaining the string() function onto a node selection like that when passing it as the first argument to string-join().
Working Solutions
Solution 1: Let string-join() Handle Implicit String Conversion
Most modern XPath 2.0+ implementations automatically convert nodes to their string values when passed to string-join(). Just pass the node sequence directly:
string-join(/form/Data/MProcess/Process/Id, ',')
This works because each Id node's string value is its text content (like your example aee865d1-7253-489c-8d53-2a0d580639d0). string-join() will iterate through all matching Id nodes, convert them to strings implicitly, and join them with commas.
Solution 2: Explicitly Convert Nodes with a for Expression
If you're working with an older XPath version or need explicit control over string conversion, use a for loop to generate a sequence of strings:
string-join(for $id in /form/Data/MProcess/Process/Id return string($id), ',')
This loops through each Id node, converts it to a string explicitly, then passes the resulting string sequence to string-join().
Quick Check to Rule Out Other Issues
Before testing the fixes, double-check your XPath path:
- Make sure node names match exactly (most XPath parsers are case-sensitive)
- Confirm that
Idnodes actually exist under theProcesspath in your XML structure
A misspelled node name or incorrect hierarchy could still cause errors even after fixing the string-join() syntax.
内容的提问来源于stack exchange,提问作者Poyraz Kürkçoğlu

