Cryptter
Cryptter

Reputation: 45

XQuery: Item expected, sequence found error

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.

Upvotes: 3

Views: 346

Answers (2)

line-o
line-o

Reputation: 1895

Looking at the comments under the previous answer the query can (and potentially should be) rewritten in one of the below options. Which works for you depends on your XQuery processor and personal preference.

FLWOR

let $cities :=
  for $prov in doc("providers.xml")/data/providers/provider
  where $prov/state > 15
  return $prov/city
return <cities>{string-join($cities, ' , '}</cities>

With a predicate

let $cities := doc("providers.xml")/data/providers/provider[state>15]/city
return <cities>{ string-join($cities, ' , ') }</cities>

Without let or return

<cities>{
  string-join(
    doc("providers.xml")
      /data/providers/provider[state>15]/city, ' , ')
}</cities>

If arrow expressions are supported:

<cities>{
    doc("providers.xml")
      /data/providers/provider[state>15]/city
    => string-join(' , ')
}</cities>

Upvotes: 2

Mads Hansen
Mads Hansen

Reputation: 66723

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>

Upvotes: 3

Related Questions