Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XQuery: Item expected, sequence found error

Tags:

xml

xquery

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.

like image 631
Cryptter Avatar asked Apr 23 '26 17:04

Cryptter


2 Answers

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>
like image 58
Mads Hansen Avatar answered Apr 27 '26 06:04

Mads Hansen


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>
like image 26
line-o Avatar answered Apr 27 '26 07:04

line-o



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!