Search code examples
xmlfor-loopxqueryexist-dbxquery-3.0

XQuery for on items


I know how to use the For instruction to create a loop on a collection or on a document but I have troubles to create a loop on an item()*

content of item()*:

<Placemark>
    <blabla id="1">
        <value>abcd</value>
    </blabla>
    <blabla id="2">
        <value>abcd</value>
    </blabla>
    ...
</Placemark>
<Placemark>
    ...
</Placemark>

Now I need for example the <blabla> elements only. With a classic loop on a document, I access like this :

for $x in doc("/db/data.xml")/Placemark
return $x

but with a loop on a item()*, it doesn't work like this :

declare function local:fct($content as item()*) as item()* {
    for $x in $content/Placemark
    return $x
};

I have no error, just a blank result. Someone know why it doesn't work?


Solution

  • Because your loop is already iterating over Placemark items, your solution asks for Placemark children of Placemark, which is empty.

    declare function local:fct(
      $content as element(Placemark)*
    ) as element(blabla)* {
        for $x in $content
        return $x/blabla
    };