質問

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?

役に立ちましたか?

解決

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
};

他のヒント

If you were passing the parent of the Placemark elements to the function as the value of the $content parameter, your code would be fine. But it looks as if you're calling the function with some expression like local:fct(doc(...)//Placemark), which means the members of the $content sequence are themselves the Placemark elements. Your function then asks for their Placemark children; there are none, so the empty sequence is what you get.

The revised function suggested by wst is a fine way to fix the problem; passing in the parents of the Placemark elements as the value of $content would be another fine way.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top