ssb
ssb

Reputation: 209

Add Comma between Multiple return Values

I got an XML which looks like:

<Student>
    <SSN>630-44-3532</SSN>
    <Name>
        <firstName>Ali</firstName>
        <lastName>Daria</lastName>
    </Name>
            ...
    <Email>[email protected]</Email>
    <Email>[email protected]</Email>
</Student>

As we have multiple nodes, when I execute an XQuery to get the email info as a whole, separated by a comma, I don't know what to do.

Here is the expected output:

<email> [email protected], [email protected] </email>

Upvotes: 1

Views: 2434

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Use:

/*/Email/concat(.,
                if(not(position()=last()))
                 then ', '
                 else ()
                )

Upvotes: 2

user357812
user357812

Reputation:

Use XPath 2.0/XQuery 1.0 fn:string-join() function.

Brief from http://www.w3.org/2005/xpath-functions/#string-join

string-join

fn:string-join($arg1 as xs:string*, $arg2 as xs:string) as xs:string

Summary: Returns a xs:string created by concatenating the members of the $arg1 sequence using $arg2 as a separator. If the value of $arg2 is the zero-length string, then the members of $arg1 are concatenated without a separator.

Upvotes: 1

Related Questions