gpt4 book ai didi

xml - XQuery 嵌套返回

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:13 31 4
gpt4 key购买 nike

我不太明白如何使用嵌套在返回中的 for。例如,给定

<book year="1994">
<title>TCP/IP Illustrated</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>

<book year="1992">
<title>Advanced Programming in the Unix environment</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>

我要返回以下内容:

<price group = "65.95">
<book year = "1994">
<book year = "1992">
</price>

但是在我编写的代码中,出现了错误 undefined variable $b。我想知道是否有人可以告诉我哪里出错了。谢谢!这是我的代码:

    for $p in distinct-values(//price)
return<price group ="{$p}">
(
for $b in //book[price = $p]
return <book year = "{$b/@year}"></book>
)
</price>

最佳答案

问题是 <price group="... 里面的所有东西元素被解释为 XML。如果您希望它作为 XQuery 运行,则必须将它括在大括号中 {...} :

for $p in distinct-values(//price)
return <price group="{$p}">{
for $b in //book[price = $p]
return <book year="{$b/@year}"/>
}</price>

仅在您的代码中 {$b/@year}被解释为 XQuery,而封闭的 FLWOR 表达式是一个 XML 文本节点。这就是查询处理器提示 undefined variable 的原因。

关于xml - XQuery 嵌套返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929045/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com