0

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?

2 Answers2

2

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
};
wst
  • 11,681
  • 1
  • 24
  • 39
  • Thank you, it works properly. The next time I will try harder before ask a simple question... – user2547762 Jul 03 '13 at 19:00
  • @user2547762 No worries. One thing that's helpful to me is always strictly typing function arguments, like above. It makes it easier to remember what all of the variables contain when you write a query. – wst Jul 03 '13 at 19:11
0

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.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65