简体   繁体   English

关于项目的XQuery

[英]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()* 我知道如何使用For指令在集合或文档上创建循环,但我在项目上创建循环有麻烦()*

content of item()*: 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. 现在我只需要<blabla>元素。 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 : 但是在item()*上有一个循环,它不能像这样工作:

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. 因为你的循环已经结束迭代Placemark项目,您的解决方案询问Placemark儿童的Placemark ,里面是空的。

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. 如果您将Placemark元素的父元素作为$ content参数的值传递给函数,那么您的代码就可以了。 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. 但看起来好像你用一些像local:fct(doc(...)//Placemark)这样的表达式来调用函数local:fct(doc(...)//Placemark) ,这意味着$ content序列的成员本身就是Placemark元素。 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; wst建议的修订功能是解决问题的好方法; passing in the parents of the Placemark elements as the value of $content would be another fine way. 传递地标元素的父母作为$ content的价值将是另一种好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM