Update
Your for
expression is correct, you just need to add an element constructor to get the result you need:
for $animal in $x/Animals/Animal,
$person in $x/People/Person
return element union { $animal, $person }
The element name (union
in the example) can be anything that you want.
These original code samples will generate the result strings from the question.
It's a little hard to read, but this will do what you want:
concat("(",
concat(string-join(
for $animal in /form/Animals/Animal/Name,
$person in /form/People/Person/Name
return concat("[", concat(string-join(($animal, $person), ", "), "]")), ", "), ")"))
If you can use an XPath/XQuery 3.0 processor, you can use the ||
concatenation operator instead of the concat
function calls, which is (somewhat) more readable:
"(" ||
string-join(
for $animal in /form/Animals/Animal/Name,
$person in /form/People/Person/Name
return "[" || string-join(($animal, $person), ", ") || "]"
, ", ")
|| ")"