Home > other >  XQuery: Item expected, sequence found error
XQuery: Item expected, sequence found error

Time:08-20

XML:

<data>
    <providers>
        <provider num="v1">
            <name>Smith</name>
            <state>20</state>
            <city>London</city>
        </provider>
        <provider num="v2">
            <name>Jones</name>
            <state>10</state>
            <city>Paris</city>
        </provider>
        <provider num="v3">
            <name>Adams</name>
            <state>30</state>
            <city>Athens</city>
        </provider>
    </providers>
 </data>

XQuery:

let $p := doc("providers.xml")/data/providers/provider
where $p/state > 15
return <cities>{concat($p/city/text(), ' , ')}</cities>

I want the output to be the name of the cities, one after the other separated by comma, but I get the error: item expected, sequence found.

CodePudding user response:

It is complaining about the concat() function where the first parameter is a sequence of strings. It expects each item you want to concatenate to be specified as separate parameters to that function (it doesn't spread the sequence to params).

You should use string-join() instead, to produce a comma separated string with each of the cities:

<cities>{string-join($p/city/text(), ' , ')}</cities>
  • Related