Search code examples
xpathxquery

How to append a delimiter between multiple values coming from a repeating field in xquery


I have a xml file which has a repeating element generating multiple values. I would like to split all the values generated from that xpath delimited by any delimiter like , |_ I have tried the following which did not work -

tokenize(/*:ShippedUnit/*:Containment/*:ContainerManifest/*:Consignments/*:Consignment/*:ConsignmentHeader/*:ConsignmentRef, '\s')

replace(/*:ShippedUnit/*:Containment/*:ContainerManifest/*:Consignments/*:Consignment/*:ConsignmentHeader/*:ConsignmentRef," ","_")

example :

Now getting - CBR123 CBR678 CBR656

Expecting to get - CBR123|CBR678|CBR656

Note : In some transactions, there can be only one value present for that xpath. And therefore replace doesnot work here


Solution

  • To achieve the expected result assuming the sample source XML added to the comments in the original post, use the fn:string-join() function:

    string-join(
        //ConsignmentRef,
        "|"
    )
    

    This will return:

    CBR00464833N|CBR01264878K
    

    For more on this function, see https://www.w3.org/TR/xpath-functions-31/#func-string-join.